由于各种原因,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 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

本文出自 “斩月” 博客,谢绝转载!
,
Stellungnahme:Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn