利用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.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 . 查看結果輸出

   wKiom1aYpwfhBlbJAADfkZDUtRQ608.png 由圖可看出,在表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..

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章