由于各种原因,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=123456
select
*
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
申明:本教程内容由威凡网编辑整理并提供IT程序员分享学习,如文中有侵权行为,请与站长联系(QQ:254677821)!