一、从库的关键配置:
change master to master_host='1.1.1.1',master_user='user11',master_port=23306,master_password='123456',master_log_file='master-bin.000883', master_log_pos=154;
master_log_file 对应导出数据时的binlog文件名,master_log_pos从主库导出文件时的binlog位置。
主从忽略某些报错,在my.cnf下配置
[mysqld]
slave-skip-errors=1032
忽略某些表不同步
replicate-wild-ignore-table=database1.table1
二、在从库查看主从状态的方法
bash
mysql>change master to master_host='192.168.8.10',master_user='reply',master_password='123456',MASTER_PORT=23306,master_log_file='bin.000004', master_log_pos=154;
mysql>start slave;
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.8.10
Master_User: reply
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: bin.000022
Read_Master_Log_Pos: 423
Relay_Log_File: host2-relay-bin.000005
Relay_Log_Pos: 624
Relay_Master_Log_File: bin.000023
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: 423
Relay_Log_Space: 991
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: 11
Master_UUID: a730e3e0-cf9c-11ed-8845-000c29085fd5
Master_Info_File: /Lcdmp3_mysqldata/mysqldata/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:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
1.基本要求以下两个参数为yes,如果有不是yes的主从肯定有问题
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
- 对比 Master_Log_File和 Relay_Master_Log_File 是否一致
对比 Read_Master_Log_Pos 和 Relay_Log_Pos 是否接近,数据是否更新
Master_Log_File: bin.000022
Read_Master_Log_Pos: 423
Relay_Log_File: host2-relay-bin.000005
Relay_Log_Pos: 624
Relay_Master_Log_File: bin.000023
3.查看从库落后主库的时间,如果时间越来越大需要具体排查问题。
Seconds_Behind_Master: 0
4.查看是否有明显的错误码
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
5、查看exec_master_log_pos这个位置与read_master_log_pos这两个偏移量是否相等,不相等则不同步,相等则同步了。
三、查看主库binlog日志
1.利用mysqlbinlog 查看对应的pos前后10条记录
mysqlbinlog -v --base64-output=decode-rows master-bin.000883 |grep -A 10 -B10 13369780
2.连接mysql数据库查看 如果看不出来做了什么操作,可以看前几个pos做了什么,结合主从报错一起分析。
bash
mysql> show binlog events in 'master-bin.000883' from 13369780 limit 2\G
*************************** 1. row ***************************
Log_name: master-bin.000883
Pos: 13369780
Event_type: Xid
Server_id: 10
End_log_pos: 13369811
Info: COMMIT /* xid=3504626158 */
*************************** 2. row ***************************
Log_name: master-bin.000883
Pos: 13369811
Event_type: Anonymous_Gtid
Server_id: 10
End_log_pos: 13369876
Info: SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
2 rows in set (0.00 sec)
三、主从同步常见错误码
二、mysql主从库同步错误:1032/1062/1060 error
1032错误:主从数据不一致 比如主库要删除修改某一条数据,但是从库没有,解决办法就是要么按照主库创建一条数据。如果是删除可以忽略,如果是更新建议按照主库数据重建一条。
1062错误:主键冲突错误
1060错误:从库ddl,又在主库进行了ddl,同步时是同样的ddl,从库出现1060错误
解决思路:严重的错误可以重新搭建从库,如果不严重,可以跳过错误。