Percona Toolkit解决MySQL主从不同步问题

一、简单介绍

percona-toolkit工具中最主要的三个组件分别是:
1)pt-table-checksum 负责监测mysql主从数据一致性
2)pt-table-sync 负责当主从数据不一致时修复数据,让它们保存数据的一致性
3)pt-heartbeat 负责监控mysql主从同步延迟

二、主机关系

主库:192.168.1.158:3306
从库:192.168.1.159:3306
  • 主从关系
root@localhost:[(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.158
                  Master_User: dts
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000219
          Read_Master_Log_Pos: 137487355
               Relay_Log_File: mysql-relay-bin.000058
                Relay_Log_Pos: 137487562
        Relay_Master_Log_File: binlog.000219
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table: www_test_com.%
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 137487355
              Relay_Log_Space: 137487813
  • 安装ptool工具(建议主从库都安装),此次实验只在从库安装
yum -y install perl-Time-HiRes 
wget http://www.percona.com/downloads/percona-toolkit/2.2.13/tarball/percona-toolkit-2.2.13.tar.gz
tar -zxvpf percona-toolkit-2.2.13.tar.gz 
cd percona-toolkit-2.2.13 
perl Makefile.PL 
make 
make install

或者rpm 安装

https://downloads.percona.com/downloads/percona-toolkit/3.2.1/binary/redhat/7/x86_64/percona-toolkit-3.2.1-1.el7.x86_64.rpm

三、授权(最好主从全部授权):

GRANT SELECT, PROCESS, SUPER, REPLICATION SLAVE,CREATE,DELETE,INSERT,UPDATE ON *.* TO 'ptcheck'@'%' identified by '123456';

四、创建测试数据

  • 1、主库创建表,执行sql语句
use www_test_com
#create table t2 (id int primary key,name varchar(100) not null,salary int); 
create table t2(id int auto_increment primary key,name varchar(20) not null,salary int);
insert into t2 values(null,'will',100);
insert into t2 values(null,'jim',200);
insert into t2 values(null,'tom',300);
insert into t2 values(null,'lili',400);
insert into t2 values(null,'luci',500);
  • 2 从库删除t2表的记录
01:  root@localhost:[www_test_com]> select* from t2;
+----+------+--------+
| id | name | salary |
+----+------+--------+
|  1 | will |    100 |
|  2 | jim  |    200 |
|  3 | tom  |    300 |
|  4 | lili |    400 |
|  5 | luci |    500 |
+----+------+--------+
5 rows inset (0.00 sec)

02:  root@localhost:[(none)]> use www_test_com;
Database changed
02:  root@localhost:[www_test_com]> delete from t2 where id="5";
Query OK, 1 row affected (0.00 sec)

26:  root@localhost:[www_test_com]> select* from t2;
+----+------+--------+
| id | name | salary |
+----+------+--------+
|  1 | will |    100 |
|  2 | jim  |    200 |
|  3 | tom  |    300 |
|  4 | lili |    400 |
+----+------+--------+
4 rows inset (0.00 sec)
  • 3、pt-table-checksum主从一致性检查,在从库上执行pt-table-checksum检查(主库主机ip:h=192.168.1.158,u=ptcheck)
[root@slave ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=www_test_com.checksums --create-replicate-table --databases=www_test_com --tables=t2 h=192.168.1.158,u=ptcheck,p=123456,P=3306
            TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
12-30T17:02:38      0      1        5       1       0   0.249 www_test_com.t2
  • pt-table-chesum 参数解释
常用参数解释:
--nocheck-replication-filters :不检查复制过滤器,建议启用。后面可以用--databases来指定需要检查的数据库。
--no-check-binlog-format : 不检查复制的binlog模式,要是binlog模式是ROW,则会报错。
--replicate-check-only :只显示不同步的信息。
--replicate= :把checksum的信息写入到指定表中,建议直接写到被检查的数据库当中。
--databases= :指定需要被检查的数据库,多个则用逗号隔开。
--tables= :指定需要被检查的表,多个用逗号隔开
h= :Master的地址
u= :用户名
p=:密码
P= :端口
  • 4、pt-table-sync 工具进行数据重新同步backup
pt-table-sync --replicate=www_test_com.checksums h=192.168.1.158,P=3306,u=ptcheck,p=123456 h=192.168.1.159,u=ptcheck,p=123456 --print
#pt-table-sync --print --replicate=www_test_com.checksums --sync-to-master h=slaveip,P=3306,u=ptcheck,p=123456
  • 4-1 打印语句信息(查看确少的语句信息)
[root@slave ~]# pt-table-sync --replicate=www_test_com.checksums h=192.168.1.158,P=3306,u=ptcheck,p=123456 h=192.168.1.159,P=3306,u=ptcheck,p=123456 --print
  • 4-2 执行主从同步语句
pt-table-sync --replicate=www_test_com.checksums h=192.168.1.158,P=3306,u=ptcheck,p=123456 h=192.168.1.159,P=3306,u=ptcheck,p=123456 --execute
  • pt-table-sync参数解释
参数解释:
--replicate= :指定通过pt-table-checksum得到的表,这2个工具差不多都会一直用。
--databases= : 指定执行同步的数据库。
--tables= :指定执行同步的表,多个用逗号隔开。
--sync-to-master :指定一个DSN,即从的IP,他会通过show processlist或show slave status 去自动的找主。
h= :服务器地址,命令里有2个ip,第一次出现的是Master的地址,第2次是Slave的地址。
u= :帐号。
p= :密码。
--print :打印,但不执行命令。
--execute :执行命令。

posted @ 2021-03-04 14:41  看萝卜在飘  阅读(154)  评论(0编辑  收藏  举报