Master 服务器配置
[mysqld]# 开启binloglog-bin=mysql-bin# 指定服务器ID,默认为1server-id=1# 每次执行写入就与硬盘同步sync_binlog=1# 需要同步的二进制数据库名称#binlog-do-db=ms_master# 设置binlog日志过期时效expire_logs_days=7# 不进行备份的数据库binlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=mysqlbinlog-ignore-db=sys
查看Master服务器状态
mysql> SHOW MASTER STATUS\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 154
Binlog_Do_DB: ms_master
Binlog_Ignore_DB: information_schema,performance_schema,mysql,sys
Executed_Gtid_Set:
1 row in set (0.00 sec)
Slave 服务器配置
[mysqld]
# 服务id
server-id=2
# 配置中继日志
relay-log=mysql-relay-bin
# 只读
read-only=1
# 开启从服务器二进制日志,如果不需要同步到其他服务器,该配置可以不开启
# log-bin=mysql-bin
# 使更新的数据写进二进制日志中
log-slave-updates=1
# 主从同步的库名相同
#replicate-do-db=ms_master
# 主从同步的库名不同,需做映射
#replicate-rewrite-db=ms_master -> ms_slave
# 指定需要同步的表,默认同步全部
#replicate-wild-do-table=ms_slave.t_order
#replicate-wild-do-table=ms_slave.t_order_item
创建同步账号
create user 'msuser'@'192.168.%.%' identified by 'ABC123@cn';
grant replication slave,replication client on *.* to msuser@'192.168.%.%' identified by 'ABC123@cn';
flush privileges;
启动复制线程
CHANGE MASTER TO
MASTER_HOST='192.168.56.51',
MASTER_USER="msuser"
MASTER_PASSWORD="ABC123@cn",
MASTER_LOG_FILE="mysql-bin.000003",
MASTER_LOG_POS=154;
start slave;
查看从服务器状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.51
Master_User: msuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
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: 154
Relay_Log_Space: 527
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: 0
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: 1
Master_UUID: c0bd14a0-d4fb-11ec-9a45-080027c6456e
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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: (ms_master,ms_slave)
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
Slave_SQL_Running: No 的解决方法
Slave 服务器
mysql > SHOW SLAVE STATUS\G;
Slave_IO_Running: Yes
Slave_SQL_Running: No
该状态表示 slave 不同步
解决办法
1,锁主表
mysql>FLUSH TABLE WITH READ LOCK;
2,备份数据
mysql>mysqldump -uroot -p --opt -R database_name > database_name.sql
3,查看主库信息
mysql> SHOW MASTER STATUS\G;
*************************** 1. row ***************************
File: mysql-bin.000006
Position: 328
Binlog_Do_DB:
Binlog_Ignore_DB: information_schema,performance_schema,mysql,sys
Executed_Gtid_Set:
1 row in set (0.01 sec)
4,停止 Slave
mysql>stop slave;
5,导入备份数据
mysql> source database_name.sql
6,重置 Slave
mysql> reset slave;
7,设置同步节点
CHANGE MASTER TO
MASTER_HOST='192.168.56.51',
MASTER_USER="msuser",
MASTER_PASSWORD="ABC123@cn",
MASTER_LOG_FILE="mysql-bin.000006",
MASTER_LOG_POS=328;
8,开启 Slave
mysql> start slave;
9,查看Slave
mysql> SHOW SLAVE STATUS\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
10,主库解锁
mysql> unlock tables;
