search
HomeDatabaseMysql TutorialMySQL Slave异常关机的处理 (pt-slave-restart)_MySQL

 

还可以使用percona-toolkit的pt-slave-restart命令。

percona-toolkit-2.2.8-1.noarch.rpm 需要安装一些依赖包。

 

  1. yum install perl perl-DBI perl-DBD-MySQL perl-IO-Socket-SSL perl-Time-HiRes

模拟异常关机的处理1.JAVA程序不断写入Master

  1. import java.sql.Connection

    ;

     

  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. publicclassTest{
  6. publicstatic void main(String[]args)throws ClassNotFoundException,
  7. SQLException{
  8. long start=System.currentTimeMillis();
  9. Class.forName("com.mysql.jdbc.Driver");
  10. Connection connection=DriverManager.getConnection(
  11. "jdbc:mysql://192.168.1.70:3306/xx","xx","xx");
  12. connection.setAutoCommit(true);
  13. PreparedStatement cmd=connection
  14. .prepareStatement("insert into test(name) values(?)");
  15. for(inti=0;i
  16. cmd.setString(1,"test");
  17. cmd.executeUpdate();
  18. }
  19. connection.commit();
  20. cmd.close();
  21. connection.close();
  22. longend=System.currentTimeMillis();
  23. System.out.println(end-start);
  24. }
  25. }

2.关闭Slave虚拟机

在JAVA程序运行当中,直接强制关闭Slave虚拟机。然后重新启动Slave

启动之后,发现复制出现异常。(Show slave status/G)

这个表除了自增的ID,就是name字段,默认都是test

现在在slave上修改name字段为‘mysql’

update test set name='mysql';

3.使用

pt-slave-restart

命令修复错误。

  1. [root@mysql2 ~]# pt-slave-restart --user=root --password=xxxxxxx--error-numbers=1062
  2. 2014-06-16T01:17:47 p=...,u=root mysql2-relay-bin.000003 312116 1062
  3. 2014-06-16T01:17:47 p=...,u=root mysql2-relay-bin.000003 312373 1062
  4. 2014-06-16T01:17:47 p=...,u=root mysql2-relay-bin.000003 312630 1062
  5. 2014-06-16T01:17:47 p=...,u=root mysql2-relay-bin.000003 312887 1062
  6. 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 313144 1062
  7. 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 313401 1062
  8. 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 313658 1062
  9. 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 313915 1062
  10. 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 314172 1062
  11. 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 314429 1062
  12. 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 314686 1062
  13. 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 314943 1062
  14. 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 315200 1594
  15. Error 1594 is not in --error-numbers.

发现还有1594的错误

重新连接Master

参考:http://www.linuxidc.com/Linux/2014-07/104235.htm

最后查看复制的情况,已经正常运行了。

我们在Master输入的name都是test,而在Slave故障之后,将name都改为了mysql。

一般来说,处理主键重复无外乎两种方式(我能想到的)

1.忽略Master发来的重复的binlog事件

2.使用Master的binlog事件更新slave数据

如果是第一种方式,slave的name应该都是mysql

如果是第二种方式,应该前部分是mysql,后一部分是test

测试结果如下,可以初步表明使用的是第二种方式。

下面302条name为'test'的记录都是异常关机时,已经执行却没有将pos写入文件的那部分数据。

pt-slave-restart将这部分数据的binlog重新执行,所以它们的name为'test’。

Statement
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
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 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.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

See all articles

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

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor