设为首页 收藏本站
查看: 1092|回复: 0

[经验分享] 利用percona-toolkit 工具来检测mysql 主从数据库同步以及实现同步

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2016-1-18 09:56:19 | 显示全部楼层 |阅读模式
环境:
  OS: Cenos6.5_x64 , 主:192.168.100.164 ,从:192.168.100.176
  软件: percona-toolkit 、 mysql56-community
  同步的库: dj1 , cnhd , shanhu

备要信息:要尽量保证主从mysql的版本相同,因为5.6以上的版本支持了Gtid的特性,与低版本做从主时,会产生不可以预计的问题。

一、安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 . 主库的配置,   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    # *** 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>
1
2
3


   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 的同步情况:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
       [iyunv@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 . 查看结果输出
    QQ截图20160118095601.jpg 由图可看出,在表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..

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-165894-1-1.html 上篇帖子: pt-heartbeat监控mysql主从复制延迟 下篇帖子: MySQL主从复制--MySQL5.5异步、半同步配置以及复制过滤实现 数据库同步 mysql 检测
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表