1.前置条件
a)主库版本:5.1.73
b)从库版本:5.7.17
2.修改主库my.cnf文件
log-bin=mysql-bin #[必须]启用二进制日志
server-id=1
3.修改从库my.cnf文件
log-bin=mysql-bin #[不是必须]启用二进制日志
server-id=102
4.重启两台服务器的mysql
/etc/init.d/mysql restart
或者service mysqld restart
5.在主服务器上建立帐户并授权slave
mysql>GRANT REPLICATION SLAVE ON . to ‘sync’@’%’ identified by ‘sync’;
6.登录主库mysql,查询master的状态
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 160 | | |
+------------------+----------+--------------+------------------+
7.配置从服务器Slave
mysql>change master to master_host='121.40.56.87',master_user='sync',
master_password='sync',master_log_file='mysql-bin.000001',master_log_pos=160;
mysql>start slave; #启动从服务器复制功能
关闭slave服务器: stop slave/start slave
重置slave服务:reset slave
8.检查从服务器复制功能状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 121.40.56.87
Master_User: sync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1494
Relay_Log_File: hechaojie-PC-relay-bin.000018
Relay_Log_Pos: 295
Relay_Master_Log_File: mysql-bin.000002
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: 1494
Relay_Log_Space: 646
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:
Master_Info_File: C:\mysql-5.7.17-winx64\data\master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more up
dates
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)
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
因此可以写shell脚本监控Slave_IO及Slave_SQL进程状态,判断主从复制是否有问题。