主从同步配置
主库准备:
配置文件
~# vim /etc/mysql.conf
~# 开启log-bin = /data/3306/mysql-bin 从库要是不做级联就关闭
~# server-id = 1
查看状态
mysql> show variables like '%log_bin%'; #log_bin = on 开启binlog日志,供从库使用
+---------------------------------+--------------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------------+
| log_bin | ON |
| log_bin_basename | /data/mysql/3306/binlog/binlog |
| log_bin_index | /data/mysql/3306/binlog/binlog.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+--------------------------------------+
6 rows in set (0.00 sec)
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000025 | 154 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
授权slave用户权限
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replication'@'10.0.0.2' identified by 'replication';
从库准备:
配置文件
~# vim mysql.conf
~# server-id = 2
查看一下主库bin_log日志位置
~# mysql -uroot -p123456 -e "show master status;"
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000025 | 154 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
从库执行
在从库里执行change master info
mysql> change master to master_host='10.0.0.1',master_port=3306,master_user='replication',master_password='replication',master_log_file='binlog.000025',master_log_pos=154;
已添加的,修改master 信息可以修改单条信息
mysql> change master to MASTER_LOG_POS=909407385;
启动从库并查看slave 状态是否为yes
Slave_IO_Running: Yes 取log
Slave_SQL_Running: Yes 读relay-bin(中继日志)写到realy-info到本地库中
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.1
Master_User: replication
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: binlog.000025
Read_Master_Log_Pos: 279267392
Relay_Log_File: mysqld-relay-bin.001
Relay_Log_Pos: 001
Relay_Master_Log_File: binlog.001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 001
Relay_Log_Space: 001
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 83
Master_UUID: 6666666666666666666666666666666666
Master_Info_File: /data/mysql/3307/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
如果从库不需要写,只开启只读模式
查看是否开启
mysql> show global variables like "%read_only%";
开启只读模式开启
mysql> set global read_only=1;