时间:2020-08-09来源:www.pcxitongcheng.com作者:电脑系统城
之前搭建了主从,但没有设置读写分离,从库也能写数据。于是想测试下在从库写数据会导致同步怎么样。 结果发现,slave_sql_running为no,slava_IO_running仍然为yes.
由于从库写数据,导致主从数据不一致,如果在主库写入和从库同样的数据,会导致sql线程终止,查看mysql错误日志如下:
2020-08-01T10:58:19.623077Z 135 [ERROR] Slave SQL for channel '': Could not execute Write_rows event on table shy_dep.zp_test; , Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000001, end_log_pos 882496, Error_code: 1062
2020-08-01T10:58:19.623101Z 135 [Warning] Slave: Error_code: 1062
2020-08-01T10:58:19.623110Z 135 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000001' position 882218
CHANGE MASTER TO master_host = '192.168.164.84',
MASTER_PORT = 3306,
master_user = 'root',
master_password = 'root',
master_log_file = 'mysql-bin.000001',
master_log_pos = 902262;#这里记录master最新的position
通过以上步骤,可以实现主从重新开始同步。
PS: 这里在重新启动从库同步时,假设主库没有进行写操作。因为如果进行了写操作,则刚才记录的主库position位置可能会变。
所以一般需要把主库临时加锁不让写。
在从库执行以下命令:
stop slave;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
SHOW SLAVE STATUS
经测试,以上方法也可以。
用解决方法一存在一个问题。比如在从库写入一条数据11, 在主库写入一条数据12,我们知道由于主从不同步会导致slave_sql_running停了。如果通过第一种方法重新连接启动后,再把12这条数据删除,会报以下错误:
2020-08-01T11:00:38.853703Z 17564 [ERROR] Slave SQL for channel '': Could not execute Delete_rows event on table shy_dep.zp_test; Unknown error 1032, Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000001, end_log_pos 883098, Error_code: 1032
2020-08-01T11:00:38.853717Z 17564 [Warning] Slave: Unknown error 1032 Error_code: 1032
2020-08-01T11:00:38.853721Z 17564 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000001' position 882828
从库由于找不到12这条记录进行删除从而会终止slave_sql_running这个线程,需要再重新连接主库的binlog最新位置进行同步。
而解决方法二,即使删除了12这条记录,仍然会保持同步。所以这里给我感觉是,第二种方式要好一些。
set global sql_slave_skip_counter=N #这里的N是指跳过N个event
官方解释:
This statement skips the next N events from the master. This is useful for recovering from replication stops caused by a statement.
个人理解,就是跳过当前从master中不能执行的事件
2023-03-19
Centos 7.9安装MySQL8.0.32的详细教程2023-03-19
一步步教你利用Mysql存储过程造百万级数据2023-03-19
利用Mysql定时+存储过程创建临时表统计数据的过程问题描述:MYSQL version 5.6.8command 表结构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 CREATE TABLE command ( ID INT NOT NULL, NAME VARCHAR(16), D...
2023-03-19
一、Galera Cluster 二、基础环境搭建 三、加入配置参数启动集群 四、 测试 五、ProxySql...
2023-03-17