运行环境:
Master:10.168.1.216
Slave:10.168.1.217
监控mysql复制延迟
1.1、创建一个后台进程定期更新主上的test库的heartbeat表()默认是1s,可以–interval指定,执行后会成一个heartbeat表,ismarthome库为我监控的同步库
pt-heartbeat -D ismarthome --update --user=root --password=123456 -h10.168.1.216 --create-table –daemonize
1.2、监控复制在slave上的落后程度(会一直监控)
pt-heartbeat -D ismarthome --monitor --user=root --password=daqi-123456 -h10.168.1.217
1.3、监控复制在slave上的落后程度(监控一次退出)
pt-heartbeat -D ismarthome --check --user=root --password=daqi-123456 -h10.168.1.217
查找和打印mysql所有从服务器复制层级关系
2.1、查找主服务器的mysql有所有从的层级关系
pt-slave-find --user=root --password=123456--host=10.168.1.216
监视mysql复制错误,并尝试重启mysql复制当复制停止的时候
3.1、监视从,跳过1个错误
pt-slave-restart --user=root --password=daqi-123456 --host=10.168.1.217 --skip-count=1
3.2、监视从,跳过错误代码为1062的错误
pt-slave-restart --user=root --password=daqi-123456 --host=10.168.1.217 --error-numbers=1062
检查数据是否一致(在主库执行)
4.1、比较test数据库同步是否一致,结果显示所有的表
pt-table-checksum --nocheck-replication-filters --databases=testDb --replicate=testDb.checksums --create-replicate-table --host=10.168.1.216 --port 3306 -uroot -p123456
参数说明:第一次运行的时候需要添加–create-replicate-table参数,如果不加这个就需要手工运行添加表结构的SQL,表结构SQL如下:
CREATE TABLE checksums (
db char(64) NOT NULL,
tbl char(64) NOT NULL,
chunk int NOT NULL,
chunk_time float NULL,
chunk_index varchar(200) NULL,
lower_boundary text NULL,
upper_boundary text NULL,
this_crc char(40) NOT NULL,
this_cnt int NOT NULL,
master_crc char(40) NULL,
master_cnt int NULL,
ts timestamp NOT NULL,
PRIMARY KEY (db, tbl, chunk),
INDEX ts_db_tbl (ts, db, tbl)
) ENGINE=InnoDB
–replicate-check-only :只显示不同步的信息。(注意:要谨慎使用,此参数不会生成新的checksums数据,只会根据checksums表已经有的数据来显示。)
运行结果行显示的结果参数说明:
备注:
(pt-table-checksum 其工作原理是通过计算每个表的散列值 并将计算过程在服务器上重放 从而拿到主从各自的散列值做比较,但是pt-table-checksum 不是直接计算整个表的散列值,而是分块计算避免服务器长时间延时 因此在计算散列过程重放时是基与statement 不能基于row)
高效同步mysql表的数据
原理:总是在主上执行数据的更改,再同步到从上,不会直接更改成从的数据,在主上执行更改是基于主上现在的数据,不会更改主上的数据。注意使用之前先备份你的数据,避免造成数据的丢失.执行execute之前最好先换成–print或–dry-run查看一下会变更哪些数据。
5.1、同步Master(10.168.1.216)上a表数据到Slave(10.168.1.217),在执行之前可以用–execute参数换成–print来查看会变更什么东西,命令里有2个ip,第一次出现的是M的地址,第2次是Slave的地址
查看:pt-table-sync –print –user=root –password=123456 h=10.168.1.216,D=testDb,t=aaa h=192.168.3.92
同步:pt-table-sync –execute –user=root –password=123456 h=10.168.1.216,D=testDb,t=aaa h=192.168.3.92
5.2、同步Master(10.168.1.216)上数据到Slave(10.168.1.217)
pt-table-sync --execute --sync-to-master --user=root --password=123456 h=10.168.1.217 --database testDb
5.3、只同步指定的a表
pt-table-sync --execute --sync-to-master --user=root --password=123456 h=10.168.1.217,D=testDb,t=a
5.4、根据pt-table-checksum的结果进行数据同步
pt-table-sync --execute --replicate testDb.checksums --user=root --password=123456 h=10.168.1.216
5.5、根据pt-table-checksum使从的数据和主的数据一致
pt-table-sync --execute --replicate test.checksums --user=root --password=123456 --sync-to-master h=10.168.1.217,D=testDb,t=a