利用percona-toolkit 工具来检测mysql 主从数据库同步以及实现同步
环境:
OS: Cenos6.5_x64 , 主:192.168.100.164 ,从:192.168.100.176
软件: percona-toolkit 、 mysql56-community
同步的库: dj1 , cnhd , shanhu
备要信息:要尽量保证主从mysql的版本相同,因为5.6以上的版本支持了Gtid的特性,与低版本做从主时,会产生不可以预计的问题。
一、安装:
Quick Install -------------perl Makefile.PLmakemake testmake installDetailed Install ----------------Extract the tarball and cd to the resulting directory:tar zxvf percona-toolkit-<version>.tar.gzcd percona-toolkit-<version>Generate the Makefile, which will check Perl module dependencies and so forth:perl Makefile.PLBuild the tools' man pages and prep for test and install:makeTest that the tools can run:make testAll tests should pass. If not, then your system may be missing a Perl module required by a tool. The tests are not comprehensive; they only test that the tools can be executed by Perl and Bash.Finally, install all tools and their man pages:make install注:可指定安装目录: perl Makefile.PL PREFIX=${HOME}
二、mysql 主从配置
1 . 主库的配置,
# *** Replication related settings *** server-id = 164binlog-format = mixedbinlog-cache-size = 32Kmax-binlog-size = 512Msync-binlog = 1log-bin = mysql-binlog-bin-index = mysql-bin.indexexpire-logs-days = 14binlog-do-db=shanhu #指定要同步的库binlog-do-db=ordbinlog-do-db=cnhdbinlog-do-db=dj1 #指定不要同步的库binlog-ignore-db=mysqlbinlog-ignore-db=testbinlog-checksum=none
2 . 从库的配置
server-id=176
binlog-format = mixed
binlog-cache-size = 32K
max-binlog-size = 512M
sync-binlog = 1
log-bin = mysql-bin
log-bin-index = mysql-bin.index
relay-log=mysqld-relay-bin
expire-logs-days = 14
binlog-do-db=shanhu
binlog-do-db=ord
binlog-do-db=cnhd
binlog-do-db=dj1
binlog-ignore-db=mysql
binlog-ignore-db=test
report_host=192.168.100.176 #指定向主库报告的slave host
report_port=3306 #端口
注: 如果不添加最后两行的report*的配置,执行pt-table-checksum时会出现Diffs cannot be detected because no slaves were found. Please read the –recursion-method documentation for information.
因为pt-table-checksum是以show processlist 或show slave hosts得到的slave主机进行主比较,如果不填写,会提示找不到slave 库.
3 . 添加有权限进行主从库都复制的帐号
GRANT SELECT, PROCESS, SUPER, REPLICATION SLAVE ON *.* TO 'req'@'192.168.100.164' IDENTIFIED BY '123456';GRANT SELECT, PROCESS, SUPER, REPLICATION SLAVE ON *.* TO 'req'@'192.168.100.176' IDENTIFIED BY '123456';
二、 启动mysql ,使数据库达到主从同步,过程略.结果如下,
master :
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000067
Position: 35736
Binlog_Do_DB: shanhu,ord,cnhd,dj1
Binlog_Ignore_DB: mysql,test
Executed_Gtid_Set:
1 row in set (0.01 sec)
ERROR:
No query specified
mysql>
slave :
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.164
Master_User: req
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000067
Read_Master_Log_Pos: 35736
Relay_Log_File: mysqld-relay-bin.000017
Relay_Log_Pos: 35895
Relay_Master_Log_File: mysql-bin.000067
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 35736
Relay_Log_Space: 36224
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 164
Master_UUID: e800cc9c-7791-11e5-9938-000c29a4b121
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
三、 使用pt-table-checksum检查数据库的同步情况。
1 . 在从库上检查库dj1 的同步情况:
[root@centos-work percona-toolkit-2.2.16]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --databases=dj1 h=192.168.100.164,u=req,p=123456,P=3306 --recursion-method=processlistTS ERRORS DIFFS ROWS CHUNKS SKIPPED TIME TABLE01-15T23:50:04 0 0 0 1 0 0.084 dj1.auth_group01-15T23:50:04 0 0 0 1 0 0.017 dj1.auth_group_permissions01-15T23:50:04 0 0 24 1 0 0.021 dj1.auth_permission01-15T23:50:04 0 0 1 1 0 0.035 dj1.auth_user01-15T23:50:04 0 0 0 1 0 0.020 dj1.auth_user_groups01-15T23:50:04 0 0 0 1 0 0.017 dj1.auth_user_user_permissions01-15T23:50:04 0 0 5 1 0 0.019 dj1.dj1_article01-15T23:50:04 0 0 5 1 0 0.041 dj1.dj1_article_art_type01-15T23:50:04 0 0 2 1 0 0.018 dj1.dj1_arttype01-15T23:50:05 0 0 13 1 0 0.020 dj1.django_admin_log01-15T23:50:05 0 0 8 1 0 0.019 dj1.django_content_type01-15T23:50:05 0 0 10 1 0 0.017 dj1.django_migrations01-15T23:50:05 0 0 3 1 0 0.017 dj1.django_session
输出字段说明:
TS :完成检查的时间。 ERRORS :检查时候发生错误和警告的数量。DIFFS :0表示一致,1表示不一致。当指定--no-replicate-check时,会一直为0,当指定--replicate-check-only会显示不同的信息。ROWS :表的行数。CHUNKS :被划分到表中的块的数目。SKIPPED :由于错误或警告或过大,则跳过块的数目。TIME :执行的时间。TABLE :被检查的表名。
命令参数说明:
--nocheck-replication-filters :不检查复制过滤器,建议启用。后面可以用--databases来指定需要检查的数据库。--no-check-binlog-format : 不检查复制的binlog模式,要是binlog模式是ROW,则会报错。--replicate-check-only :只显示不同步的信息。--replicate= :把checksum的信息写入到指定表中,建议直接写到被检查的数据库当中。 --databases= :指定需要被检查的数据库,多个则用逗号隔开。--tables= :指定需要被检查的表,多个用逗号隔开h=192.168.100.164 :Master的地址u=req :用户名p=123456 :密码P=3306 :端口
2 . 查看结果输出
由图可看出,在表dj1_arttype 上从库表主库多了一条纪录.
master :
mysql> select * from dj1_arttype;
+----+--------+
| id | name |
+----+--------+
| 1 | 新闻 |
| 2 | 文章 |
+----+--------+
2 rows in set (0.00 sec)
slave :
mysql> select * from dj1_arttype;
+----+--------+
| id | name |
+----+--------+
| 1 | 新闻 |
| 2 | 文章 |
| 3 | news |
+----+--------+
2 rows in set (0.00 sec)
三 、 修复MySQL主从不一致的数据,让他们保持一致性
pt-table-sync: 高效的同步MySQL表之间的数据,他可以做单向和双向同步的表数据。他可以同步单个表,也可以同步整个库。它不同步表结构、索引、或任何其他模式对象。所以在修复一致性之前需要保证他们表存在。
先master的ip,用户,密码,然后是slave的ip,用户,密码
#pt-table-sync h=192.168.100.164,u=req,p=123456,P=3306 h=192.168.100.176,u=req,p=123456,P=3306 --databases=dj1 --print
参数的意义:
--replicate= :指定通过pt-table-checksum得到的表,这2个工具差不多都会一直用。
--databases= : 指定执行同步的数据库,多个用逗号隔开。
--tables= :指定执行同步的表,多个用逗号隔开。
--sync-to-master :指定一个DSN,即从的IP,他会通过show processlist或show slave status 去自动的找主。
h=127.0.0.1 :服务器地址,命令里有2个ip,第一次出现的是Master的地址,第2次是Slave的地址。
u=root :帐号。
p=123456 :密码。
--print :打印,但不执行命令。
--execute :执行命令。
命令介绍完了,一起解释下执行的效果:通过(--print)打印出来了修复数据的sql语句,可以手动的去从行执行,让他们数据保持一致性。那能否直接执行?当然可以,通过(--execute)
# pt-table-sync h=127.0.0.1,u=root,p=123456 h=192.168.0.20,u=root,p=123456 --execute
注意:要是表中没有唯一索引或则主键则会报错:Can't make changes on the master because no unique index exists at /usr/local/bin/pt-table-sync line 10684..
转载于:https://blog.51cto.com/plong/1735371
利用percona-toolkit 工具来检测mysql 主从数据库同步以及实现同步相关推荐
- Percona Toolkit工具简介
系列文章目录 第一章:sql_mode模式 第二章:optimize table.analyze table.alter table.gh-ost 第三章:InnoDB MVCC原理 第四章:sql语 ...
- 如何监测mysql主从复制状态_如何实时检测mysql主从状态,并做邮件告警?
实时检测mysql主从状态,并做邮件告警 需求分析: 当我们做完主从后,主从成功的话,Slave_IO_Running和Slave_SQL_Running两个进程的状态就为yes,但是在做主从同步时也 ...
- MySQL主从数据库同步延迟问题解决
MySQL主从数据库同步延迟问题 摘要: MySQL的主从同步是一个很成熟的架构,优点为:①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;②在从主服务器进行备份,避免备份期间影响 ...
- MySQL主从数据库配置和常见问题
转载自 MySQL主从数据库配置和常见问题 本篇主要介绍MySQL的主从数据配置方法. 公司运维能力不是太好,数据库最近出了一次问题,导致丢失了一天的数据,并且某个服务宕机一晚上.为了避免再次出现 ...
- mysql数据库主从不同步_MySQL数据库之mysql主从数据库不同步的2种解决方法
本文主要向大家介绍了MySQL数据库之mysql主从数据库不同步的2种解决方法 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 今天发现Mysql的主从数据库没有同步 先上Mas ...
- pt mysql_pt(Percona Toolkit)工具详解:(二)工具介绍
之所以先写介绍,那是因为太多工具,要查起来非常麻烦,要先做个汇总介绍,方便查找. 介绍 1.找出重复的索引和外键 pt-duplicate-key-checker 这个工具会将重复的索引和外键都列出来 ...
- pt mysql_pt(Percona Toolkit)工具介绍
例子:[root@node1 ~]# pt-show-grants --user=root --password='abc123'实际意义不大,主要是比较mysql权限以及进行版本控制5.在多台服务器 ...
- linux mysql 主从数据库_Linux下安装MySQL及MySQL主从同步配置
从零开始说起,先说在Linux安装MySQL再说主从配置,MySQL的版本为5.7 一.mysql安装 1.使用命令 rpm -qa|grep mariadb 查看是否存在mariadb 2. 使用 ...
- mysql主从数据库含义_(转)Mysql数据库主从心得整理
管理mysql主从有2年多了,管理过200多组mysql主从,几乎涉及到各个版本的主从,本博文属于总结性的,有一部分是摘自网络,大部分是根据自己管理的心得和经验所写,整理了一下,分享给各位同行,希望对 ...
最新文章
- 查找算法常见的五大面试知识点与两类实战!
- CentOS修改忘记密码
- KC shop 开发历程------注册界面
- 【JOURNAL】搭配/ IKEA Match
- machine learning (6)---how to choose features, polynomial regression
- MDK 的编译过程及文件类型全解
- 【转】VS2005 CTP 版本这个CTP是什么意思
- matlab 贝叶斯工具箱,matlab的BNT贝叶斯工具箱错误求教
- python如何合并两个字典
- java rome_Rome使用入门
- 随想录(由自定义打印函数想到的)
- app.vue 跳转页面_【在线教学】第8章 网站页面布局和模块设计
- C++ STL map 中insert函数返回值问题
- UNITY TUIO的使用
- 组策略怎么禁用计算机管理,如何有效禁止组策略的运行
- Matlab画图中怎么打希腊字母,Matlab图形中输入希腊字母
- c语言中最小公倍数算法,常见算法:C语言求最小公倍数和最大公约数三种算法...
- 如何将图片存进SQL数据库中以及从数据库读取照片(解决办法)
- 西门子PLC1200-组态王跟Access数据库--⑪查询数据库
- linux配置文件如何排序,Linux系统中sort排序命令的使用教程