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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1656
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles