#将percona下载到两台主机上
wget https://www.percona.com/downloads/percona-toolkit/3.1.0/binary/redhat/7/x86_64/percona-toolkit-3.1.0-2.el7.x86_64.rpm
主库创建一张heartbeat表,表中有个时间戳字段。主库上pt-heartbeat的update线程会在指定时间间隔更新时间戳。
从库上的pt-heartbeat的monitor线程会检查复制的心跳记录,这个记录就是主库修改的时间戳。然后和当前系统时间进行对比,得出时间上的差异,差异值就是延迟的时间大小。由于heartbeat表中有server_id字段,在监控某个从库的延迟时指定参考主库的server_id即可。
1,准备2台主机,配置mysql
#在主机A上配置mysql主
#关闭防火墙和selinux
[root@kafka01 ~] systemctl stop firewalld.service
[root@kafka01 ~] setenforce 0
[root@kafka01 ~] yum -y install mariadb mariadb-server
[root@kafka01 ~] vim /etc/my.cnf #开启二进制日志
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignoreql
[root@kafka01 ~] systemctl start mariadb #启动
[root@kafka01 ~] mysql
MariaDB [(none)]> grant all on *.* to 'slave'@'192.168.59.111' identified by '123456'; #创建授权用户
MariaDB [(none)]> flush privileges; #刷新
MariaDB [(none)]> show master status; #查看二进制日志
+------------------+----------+--------------+---------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB
+------------------+----------+--------------+---------------------------------------------+
|mysql-bin.000003| 462 | | information_schema,performance_schema,mysql
+------------------+----------+--------------+---------------------------------------------+
#在主机B上配置mysql从
#关闭防火墙和selinux
[root@kafka02 ~] systemctl stop firewalld.service
[root@kafka02 ~] setenforce 0
[root@kafka02 ~] yum -y install mariadb mariadb-server
[root@kafka02 ~] vim /etc/my.cnf
[mysqld]
server-id=2
[root@kafka02 ~] systemctl start mariadb
[root@kafka02 ~] mysql
MariaDB [(none)]> change master to master_host='192.168.59.110',master_user='slave',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=462;
MariaDB [(none)]> slave start;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.59.110
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 462
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#可以在mysql主上建库建表,在mysql从上查看同步
2,分别在A,B主机上安装percona-toolkit和sysbench
#在主机A,master主上,做压测前准备
[root@kafka01 src] yum -y install percona-toolkit-3.1.0-2.el7.x86_64.rpm
[root@kafka01 src] yum -y install sysbench
#建库
MariaDB [none]> create database sbtest;
#授权
MariaDB [none]> grant all on *.* to 'sbtest'@'192.168.59.110' identified by '123456';
MariaDB [none]> flush privileges;
#prepare:准备压测数据 在sbtest库里建10张表,每张表里插入50w条数据
#run:执行压测
#cleanup:清除压测数据
#建heartbeat心跳表,通过update执行更新时间戳
[root@kafka01 src] pt-heartbeat -S /var/lib/mysql/mysql.sock --database sbtest --update --create-table --daemonize
#查看是否创建
[root@kafka01 src] mysql -e "use sbtest;show tables"
+------------------+
| Tables_in_sbtest |
+------------------+
| heartbeat |
+------------------+
#利用sysbench模拟数据操作
[root@kafka01 src]# sysbench /usr/share/sysbench/oltp_read_write.lua \
> --mysql-host='192.168.59.110' \
> --mysql-user=sbtest \
> --mysql-password=123456 \
> --mysql-port=3306 \
> --db-driver=mysql \
> --mysql-db=sbtest \
> --tables=10 \
> --table-size=500000 \
> --report-interval=10 \
> --threads=128 \
> --time=120 \
> prepare #回车
sysbench 1.0.17 (using system LuaJIT 2.0.4)
Initializing worker threads...
Creating table 'sbtest3'...
Creating table 'sbtest5'...
Creating table 'sbtest2'...
Creating table 'sbtest7'...
Creating table 'sbtest9'...
Creating table 'sbtest1'...
Creating table 'sbtest10'...
Creating table 'sbtest6'...
Creating table 'sbtest8'...
Creating table 'sbtest4'...
Inserting 500000 records into 'sbtest3'
Inserting 500000 records into 'sbtest6'
Inserting 500000 records into 'sbtest5'
Inserting 500000 records into 'sbtest2'
Inserting 500000 records into 'sbtest9'
Inserting 500000 records into 'sbtest10'
Inserting 500000 records into 'sbtest7'
Inserting 500000 records into 'sbtest4'
Inserting 500000 records into 'sbtest8'
Inserting 500000 records into 'sbtest1'
[root@kafka01 ~] mysql -e "use sbtest;show tables"
+------------------+
| Tables_in_sbtest |
+------------------+
| heartbeat |
| sbtest1 |
| sbtest10 |
| sbtest2 |
| sbtest3 |
| sbtest4 |
| sbtest5 |
| sbtest6 |
| sbtest7 |
| sbtest8 |
| sbtest9 |
+------------------+
#在主机B,slave上,做压测前准备
[root@kafka01 src] yum -y install percona-toolkit-3.1.0-2.el7.x86_64.rpm
[root@kafka01 src] yum -y install sysbench
#在主机B,slave上监测master主库
[root@kafka02 src] pt-heartbeat --master-server-id=1 --monitor --database sbtest
#在主机A,master上执行压测
[root@kafka01 src] sysbench /usr/share/sysbench/oltp_read_write.lua \
> --mysql-host='192.168.59.110' \
> --mysql-user=sbtest \
> --mysql-password=123456 \
> --mysql-port=3306 \
> --db-driver=mysql \
> --mysql-db=sbtest \
> --tables=10 \
> --table-size=500000 \
> --report-interval=10 \
> --threads=128 \
> --time=120 \
> run #回车
sysbench 1.0.17 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 128
Report intermediate results every 10 second(s)
Initializing random number generator from current time
Initializing worker threads...
Threads started!
[ 10s ] thds: 128 tps: 303.75 qps: 6234.98 (r/w/o: 4386.30/1228.99/619.69) lat (ms,95%): 694.45 err/s: 0.00 reconn/s: 0.00 #显示数据
#压测过程中会产生如下
[root@kafka01 ~] mysql -e "use sbtest;show tables"
+------------------+
| Tables_in_sbtest |
+------------------+
| heartbeat |
| sbtest1 |
| sbtest10 |
| sbtest2 |
| sbtest3 |
| sbtest4 |
| sbtest5 |
| sbtest6 |
| sbtest7 |
| sbtest8 |
| sbtest9 |
+------------------+
[root@kafka01 src] sysbench /usr/share/sysbench/oltp_read_write.lua \
> --mysql-host='192.168.59.110' \
> --mysql-user=sbtest \
> --mysql-password=123456 \
> --mysql-port=3306 \
> --db-driver=mysql \
> --mysql-db=sbtest \
> --tables=10 \
> --table-size=500000 \
> --report-interval=10 \
> --threads=128 \
> --time=120 \
> cleanup #回车后会清除那是10张表
sysbench 1.0.17 (using system LuaJIT 2.0.4)
Dropping table 'sbtest1'...
Dropping table 'sbtest2'...
Dropping table 'sbtest3'...
Dropping table 'sbtest4'...
Dropping table 'sbtest5'...
Dropping table 'sbtest6'...
Dropping table 'sbtest7'...
Dropping table 'sbtest8'...
Dropping table 'sbtest9'...
Dropping table 'sbtest10'...
pt-slave-restart是一个可以跳过特定错误并自动重启slave的工具。
pt-slave-restart监控一个或者多个MySQL复制slave,试图跳过引起错误的语句。它以指数变化的睡眠时间职能地检查slave。你可以指定要跳过的错误然后运行slave一直到一个确定的binlog位置。
pt-slave-restart一旦检测到slave有错误就会打印一行。默认情况下该打印行为:时间戳、连接信息、relay_log_file,relay_log_pos,以及last_errno。你可以使用--verbose选项添加更多信息,也可以使用--quiet选项阻止所有输出。
1,在主机A,master中sbtest库中建测试表。在主机B,slave中插入一条数据
MariaDB [sbtest]> create table z1(id int not null,uname varchar(32),primary key(id));
MariaDB [sbtest]> show tables;
+------------------+
| Tables_in_sbtest |
+------------------+
| heartbeat |
| z1 |
+------------------+
#在主机B
MariaDB [sbtest]> insert into z1 values(3,'python');
MariaDB [sbtest]> select * from z1;
+----+--------+
| id | uname |
+----+--------+
| 3 | python |
+----+--------+
2,在主机A,master中插入数据。并在主机B,slave中查看
MariaDB [sbtest]> insert into z1 values(2,'mysql');
MariaDB [sbtest]> insert into z1 values(3,'java');
MariaDB [sbtest]> select * from z1;
+----+-------+
| id | uname |
+----+-------+
| 2 | mysql |
| 3 | java |
+----+-------+
#主机B
MariaDB [sbtest]> select * from z1;
+----+--------+
| id | uname |
+----+--------+
| 2 | mysql |
| 3 | python |
+----+--------+
#master与slave中的数据会不同
3,查看主机B,slave状态会出现报错
MariaDB [sbtest]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.59.110
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 763
Relay_Log_File: mariadb-relay-bin.000012
Relay_Log_Pos: 854
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1062
Last_Error: Error 'Duplicate entry '3' for key 'PRIMARY'' on query. Default database: 'sbtest'. Query: 'insert into z1 values(3,'java')'
#跳过1062错误并重启
[root@kafka02 src] pt-slave-restart --error-numbers=1062
2020-04-05T20:11:47 mariadb-relay-bin.000012 854 1062
#在查看slave库的状态
MariaDB [sbtest]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.59.110
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 763
Relay_Log_File: mariadb-relay-bin.000012
Relay_Log_Pos: 1047
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#但是只是跳过错误,并未达到主从同步
pt-table-checksum用于校验主从数据的一致性,该命令在主库上执行校验,然后对复制的一致性进行检查,来对比主从之间的校验值,并输出对比结果
1,在主机A上,执行操作
[root@kafka01 src] pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=test.checsums --create-replicate-table --databases=sbtest --tables=z1 h=192.168.59.110,u=sbtest,p=123456,P=3306
# A software update is available:
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
04-05T20:30:11 0 1 2 0 1 0 0.023 sbtest.z1
【注意】:
1)根据测试,需要一个即能登录主库,也能登录从库的账号;sbtest 123456
2)只能指定一个host,必须为主库的IP;
3)在检查时会向表加S锁;
4)运行之前需要从库的同步IO和SQL进程是YES状态。
4 pt-ta
pt-table-sync高效的同步MySQL表之间的数据,他可以做单向和双向同步的表数据。他可以同步单个表,也可以同步整个库。它不同步表结构、索引、或任何其他模式对象。所以在修复一致性之前需要保证他们表存在。
1,在主机B,slave库中建立授权用户
MariaDB [(none)]> grant all on *.* to 'sbtest'@'192.168.59.111' identified by '123456';
MariaDB [(none)]> flush privileges;
2,在主机A,master上执行要修复语句打印出来
[root@kafka01 src] pt-table-sync --replicate=test.checksums h=192.168.59.110,u=sbtest,p=123456,P=3306 h=192.168.59.111,u=sbtest,p=123456,P=3306 --print
REPLACE INTO `test`.`z1`(`id`, `uname`) VALUES ('3', 'java') /*percona-toolkit src_db:test src_tbl:z1 src_dsn:P=3306,h=192.168.59.110,p=...,u=sbtest dst_db:test dst_tbl:z1 dst_dsn:P=3306,h=192.168.59.111,p=...,u=sbtest lock:1 transaction:1 changing_src:test.checksums replicate:test.checksums bidirectional:0 pid:7027 user:root host:kafka01*/;
3,使用pt-table-checksum命令验证
[root@kafka01 src] pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=test.checsums --create-replicate-table --databases=sbtest --tables=z1 h=192.168.59.110,u=sbtest,p=123456,P=3306
# A software update is available:
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
04-05T20:30:11 0 1 2 0 1 0 0.023 sbtest.z1