Home Database Mysql Tutorial 使用pt-table-checksum与pt-table-sync修复mysql主从数据不一致

使用pt-table-checksum与pt-table-sync修复mysql主从数据不一致

Jun 07, 2016 pm 05:36 PM
mysql repair how Influence Record

在MySql主从复制架构的应用中,长时间运行的DB出现各种异常之后,可能会有主备不一致的情况产生,偶尔会在同步时报错,影响正常的业务访问。那么当我们遇到这种

       在MySql主从复制架构的应用中,长时间运行的DB出现各种异常之后,可能会有主备不一致的情况产生,偶尔会在同步时报错,影响正常的业务访问。那么当我们遇到这种主从数据不一致的情况时如何去处理这种状况,关于mysql的主从数据校验的工具也不少,但是我常用的是percona-toolkit提供的一些工具,顺便简单的做下记录。


一、关于pt-table-checksum与pt-table-sync?

       pt-table-checksum与pt-table-sync是percona 为mysql DBA 提供一些非常有用的性能诊断工具,此外还有我们经常在备份用到的XtraBackup,用这款基于物理备份工具为mysql的innodb提供热备、myisam提供温备。

pt-table-checksum用于校验主从数据的一致性,而pt-table-sync用于同步MySQL表之间的数据,它可以同步单个表,也可以同步整个库,但是不同步表结构、索引、或任何其他模式对象。


二、安装percona-toolkit:

       # wget

        # cd ~/

       # zxvf percona-toolkit_2.2.6.tar.gz

       # cd percona-toolkit-2.2.6/

       # perl Makefile.PL

       # make

       # make test

       # make install


三、使用pt-table-checksum检查数据是否一致(在主库执行):

       使用方法:pt-table-checksum [OPTIONS] [DSN]

       pt-table-checksum:用于校验主从数据的一致性,在主上通过执行校验的查询对复制的一致性进行检查,对比主从的校验值,从而产生结果。DSN指向的是主的地址,不制定任何参数,会直接对本地的所有数据库的表进行检查。

     

      例如:pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --databases=db_test1 --replicate=db_test1.checksums --host=172.16.5.100 --port=3306 --user=root --password=123456

       

       --nocheck-replication-filters :不检查复制过滤器,建议启用。后面可以用--databases来指定需要检查的数据库。

       --no-check-binlog-format      :不检查复制的binlog模式,要是binlog模式是ROW,则会报错。


       --replicate-check-only          :只显示不同步的信息。(注意:要谨慎使用,此参数不会生成新的checksums数据,只会根据checksums表已经有的数据来显示。)

       --replicate=                           :把checksum的信息写入到指定表中,建议直接写到被检查的数据库当中。

       --databases=                         :指定需要被检查的数据库,多个则用逗号隔开。

       --tables=                                 :指定需要被检查的表,多个用逗号隔开

       h=127.0.0.1                           :Master的地址

       u=root                                     :用户名

       p=123456                              :密码

       P=3306                                  :端口


       执行时显示的结果参数说明:

       TS                   :完成检查的时间。

       ERRORS       :检查时候发生错误和警告的数量。

       DIFFS             :0表示一致,1表示不一致。当指定--no-replicate-check时,会一直为0,当指定--replicate-check-only会显示不同的信息。

       ROWS            :表的行数。

       CHUNKS       :被划分到表中的块的数目。

       SKIPPED       :由于错误或警告或过大,则跳过块的数目。

       TIME               :执行的时间。

       TABLE            :被检查的表名。


四、使用pt-table-sync修复不一致数据:

       使用方法:pt-table-sync [OPTIONS] DSN [DSN]

       pt-table-sync:高效的同步MySQL表之间的数据,他可以做单向和双向同步的表数据。他可以同步单个表,也可以同步整个库。它不同步表结构、索引、或任何其他模式对象。所以在修复一致性之前需要保证他们表存在。


       例如:pt-table-sync --replicate=db_test1.checksums --databases=db_test1 --tables=table1,table2,table3,... --charset=utf8 h=127.0.0.1,u=root,p=123456 h=172.16.5.38,u=root,p=123 --print   (建议先打印检查执行的语句,再复制语句执行或再执行execute)

       

       --replicate=          :指定通过pt-table-checksum得到的表,这2个工具差不多都会一直用。

       --databases=       :指定执行同步的数据库,多个用逗号隔开。

       --tables=               :指定执行同步的表,多个用逗号隔开。

       --sync-to-master  :指定一个DSN,即从的IP,他会通过show processlist或show slave status 去自动的找主。

       h=127.0.0.1          :服务器地址,命令里有2个ip,第一次出现的是M的地址,第2次是Slave的地址。

       u=root                    :帐号。

       p=123456             :密码。


       --print                    :打印,,但不执行命令。

       --execute              :执行命令。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

This Apple ID is not yet in use in the iTunes Store: Fix This Apple ID is not yet in use in the iTunes Store: Fix Jun 10, 2024 pm 05:42 PM

When logging into iTunesStore using AppleID, this error saying "This AppleID has not been used in iTunesStore" may be thrown on the screen. There are no error messages to worry about, you can fix them by following these solution sets. Fix 1 – Change Shipping Address The main reason why this prompt appears in iTunes Store is that you don’t have the correct address in your AppleID profile. Step 1 – First, open iPhone Settings on your iPhone. Step 2 – AppleID should be on top of all other settings. So, open it. Step 3 – Once there, open the “Payment & Shipping” option. Step 4 – Verify your access using Face ID. step

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

iPhone stuck in airplane mode: How to fix it iPhone stuck in airplane mode: How to fix it Jun 15, 2024 pm 02:16 PM

Airplane mode is very convenient in some situations. However, the same airplane mode may give you a headache if your iPhone suddenly gets stuck on it. In this article, we have designed this set of solutions to get your iPhone out of airplane mode. Quick fix – 1. Try disabling Airplane Mode directly from Control Center. 2. If you are unable to disable Airplane Mode from Control Center, you can disable Airplane Mode directly from the Settings tab – If these tips don’t work, follow the fixes below to resolve the issue. Fix 1 – Force Restart Your Device The process of force restarting your device is very simple. All you have to do is follow these step-by-step instructions. Step 1 – You can start the process by pressing and releasing the Volume Up button. step

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

See all articles