视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Mysql主从不同步问题处理
2020-11-09 13:33:19 责编:小采
文档

由于各种原因,mysql主从架构经常会出现数据不一致的情况出现,大致归结为如下几类1:备库写数据2:执行non-deterministicquery3:回滚掺杂事务表和非事务表的事务4

一:安装percona-toolkit

# yum -y install perl-Time-HiRes # wget # tar -zxvpf percona-toolkit-2.2.13.tar.gz # cd percona-toolkit-2.2.13 # perl Makefile.PL # make # make install



1. 先校验

# pt-table-checksum --user=root --password=123456 \ --host=192.168.1.205 --port=3306 \ --databases=test --tables=t2 --recursion-method=processlist \ --no-check-binlog-format --nocheck-replication-filters \ --replicate=test.checksums# pt-table-sync --execute --replicate \ test.checksums --sync-to-master h=192.168.1.207,P=3306,u=root,p=123456SELECT * FROM test.checksums WHERE master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)

1: 主库上建表,插入测试数据

mysql> create table t2 (id int primary key,name varchar(100) not null,salary int); mysql> CREATE PROCEDURE test_insert () BEGIN DECLARE i INT DEFAULT 0; WHILE i<10000 DO INSERT INTO t2 VALUES (i,CONCAT('员工',i), i); SET i=i+1; END WHILE ; END;; mysql> CALL test_insert();

从库上校验当前数据的同步情况为正常。

从库上删除一半的数据

mysql> delete from t2 where id > 5000; Query OK, 4999 rows affected (0.14 sec) mysql> select count(*) from t2; +----------+ | count(*) | +----------+ | 5001 | +----------+ 1 row in set (0.01 sec)

进行校验:

# pt-table-checksum --user=root --password=123456 \ --host=192.168.1.205 --port=3306 \ --databases=test --tables=t2 --recursion-method=processlist \ --no-check-binlog-format --nocheck-replication-filters \ --replicate=test.checksums

mysql> SELECT * FROM test.checksums WHERE master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)

# pt-table-sync --execute --replicate \ test.checksums --sync-to-master h=192.168.1.207,P=3306,u=root,p=123456

主从库my.cnf文件添加如下配置项后重启数据库实例

character_set_client=utf8 character_set_server=utf8

# pt-table-sync --execute --replicate \ test.checksums --charset=utf8 \ --sync-to-master h=192.168.1.207,P=3306,u=root,p=123456

本文出自 “斩月” 博客,谢绝转载!

下载本文
显示全文
专题