MySQL主从同步错误解决方法

主从复制工作(过程)原理
1.从库执行change master to 命令(主库的连接信息+复制的起点)
2.从库会将以上信息,记录到master.info文件
3.从库执行 start slave 命令,立即开启IO_T和SQL_T
4. 从库 IO_T,读取master.info文件中的信息
获取到IP,PORT,User,Pass,binlog的位置信息
5. 从库IO_T请求连接主库,主库专门提供一个DUMP_T,负责和IO_T交互
6. IO_T根据binlog的位置信息(mysql-bin.000004 , 444),请求主库新的binlog
7. 主库通过DUMP_T将最新的binlog,通过网络TP给从库的IO_T
8. IO_T接收到新的binlog日志,存储到TCP/IP缓存,立即返回ACK给主库,并更新master.info
9.IO_T将TCP/IP缓存中数据,转储到磁盘relaylog中.
10. SQL_T读取relay.info中的信息,获取到上次已经应用过的relaylog的位置信息
11. SQL_T会按照上次的位置点回放最新的relaylog,再次更新relay.info信息
12. 从库会自动purge应用过relay进行定期清理
补充说明:
一旦主从复制构建成功,主库当中发生了新的变化,都会通过dump_T发送信号给IO_T,增强了主从复制的实时性.

主从复制监控 ****
命令:
show slave status \G

主库有关的信息(master.info):
Master_Host: 10.0.0.51
Master_User: repl
Master_Port: 3307
Connect_Retry: 10


Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 609


从库relay应用信息有关的(relay.info):
Relay_Log_File: db01-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000004

从库线程运行状态(排错)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:

过滤复制有关的信息:
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:

从库延时主库的时间(秒):
Seconds_Behind_Master: 0

延时从库:
SQL_Delay: 0
SQL_Remaining_Delay: NULL

GTID复制有关的状态信息
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0

主从复制故障 *****
IO 线程故障
(1) 连接主库: connecting

网络,连接信息错误或变更了,防火墙,连接数上线
排查思路:

  1. 使用复制用户手工登录
    [root@db01 data]# mysql -urepl -p12321321 -h 10.0.0.51 -P 3307
    mysql: [Warning] Using a password on the command line interface can be insecure.
    ERROR 1045 (28000): Access denied for user ‘repl’@‘db01’ (using password: YES)
    [root@db01 data]# mysql -urep -p123 -h 10.0.0.51 -P 3307
    mysql: [Warning] Using a password on the command line interface can be insecure.
    ERROR 1045 (28000): Access denied for user ‘rep’@‘db01’ (using password: YES)
    [root@db01 data]# mysql -urepl -p123 -h 10.0.0.52 -P 3307
    mysql: [Warning] Using a password on the command line interface can be insecure.
    ERROR 2003 (HY000): Can’t connect to MySQL server on ‘10.0.0.52’ (113)
    [root@db01 data]# mysql -urepl -p123 -h 10.0.0.51 -P 3309
    mysql: [Warning] Using a password on the command line interface can be insecure.
    ERROR 2003 (HY000): Can’t connect to MySQL server on ‘10.0.0.51’ (111)
    [root@db01 data]#

解决:

  1. stop slave
  2. reset slave all;
  3. change master to
  4. start slave

(2) 请求Binlog

binlog 没开
binlog 损坏,不存在

主库 reset master 处理:
从库
stop slave ;
reset slave all;
CHANGE MASTER TO
MASTER_HOST=‘10.0.0.51’,
MASTER_USER=‘repl’,
MASTER_PASSWORD=‘123’,
MASTER_PORT=3307,
MASTER_LOG_FILE=‘mysql-bin.000001’,
MASTER_LOG_POS=154,
MASTER_CONNECT_RETRY=10;
start slave;

(3) 存储binlog到relaylog

SQL线程故障
relay-log损坏
回放relaylog
研究一条SQL语句为什么执行失败?
insert delete update —> t1 表 不存在
create table oldboy —> oldboy 已存在
约束冲突(主键,唯一键,非空…)

合理处理方法:
把握一个原则,一切以主库为准进行解决.
如果出现问题,尽量进行反操作
最直接稳妥办法,重新构建主从

暴力的解决方法
方法一:
错误代码1007 数据库已存在无法创建
从库操作
解决方法:stop slave;
set global sql_slave_skip_counter = 1;
start slave;
注:sql_slave_skip_counter = 1; 让从库复制的时候,在realy-info中指针向下跳一条,忽略上一条操作。

方法2 根据错误号跳过指定错误
slave-skip-errors = 1032,1062,1007
编辑从库my.cnf添加下面
slave-skip-errors = 1032,1062,1007

常见错误代码:
1007:对象已存在
1032:无法执行DML
1062:主键冲突,或约束冲突

但是,以上操作有时是有风险的,最安全的做法就是重新构建主从。把握一个原则,一切以主库为主.

为了很程度的避免SQL线程故障
(1) 从库只读
vim /etc/my.cnf
read_only
super_read_only
(2) 使用读写分离中间件
atlas
mycat
ProxySQL
MaxScale

你可能感兴趣的:(MySQL)