您的位置:首页 > 数据库 > MySQL

利用percona-toolkit 工具来检测mysql 主从数据库同步以及实现同步

2016-01-15 16:16 856 查看
环境:
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.PL
make
make test
make install

Detailed Install
----------------

Extract the tarball and cd to the resulting directory:

tar zxvf percona-toolkit-<version>.tar.gz
cd percona-toolkit-<version>

Generate the Makefile, which will check Perl module dependencies and
so forth:

perl Makefile.PL

Build the tools' man pages and prep for test and install:

make

Test that the tools can run:

make test

All 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 = 164
binlog-format = mixed
binlog-cache-size = 32K
max-binlog-size = 512M
sync-binlog = 1
log-bin = mysql-bin
log-bin-index = mysql-bin.index
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
binlog-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=processlist
TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
01-15T23:50:04      0      0        0       1       0   0.084 dj1.auth_group
01-15T23:50:04      0      0        0       1       0   0.017 dj1.auth_group_permissions
01-15T23:50:04      0      0       24       1       0   0.021 dj1.auth_permission
01-15T23:50:04      0      0        1       1       0   0.035 dj1.auth_user
01-15T23:50:04      0      0        0       1       0   0.020 dj1.auth_user_groups
01-15T23:50:04      0      0        0       1       0   0.017 dj1.auth_user_user_permissions
01-15T23:50:04      0      0        5       1       0   0.019 dj1.dj1_article
01-15T23:50:04      0      0        5       1       0   0.041 dj1.dj1_article_art_type
01-15T23:50:04      0      0        2       1       0   0.018 dj1.dj1_arttype
01-15T23:50:05      0      0       13       1       0   0.020 dj1.django_admin_log
01-15T23:50:05      0      0        8       1       0   0.019 dj1.django_content_type
01-15T23:50:05      0      0       10       1       0   0.017 dj1.django_migrations
01-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 [b] --print[/b]

参数的意义:

--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..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息