还可以使用percona-toolkit的pt-slave-restart命令。
percona-toolkit-2.2.8-1.noarch.rpm 需要安装一些依赖包。
- yum install perl perl-DBI perl-DBD-MySQL perl-IO-Socket-SSL perl-Time-HiRes
模拟异常关机的处理1.JAVA程序不断写入Master
-
import java.sql.Connection
;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- publicclassTest{
- publicstatic void main(String[]args)throws ClassNotFoundException,
- SQLException{
- long start=System.currentTimeMillis();
- Class.forName("com.mysql.jdbc.Driver");
- Connection connection=DriverManager.getConnection(
- "jdbc:mysql://192.168.1.70:3306/xx","xx","xx");
- connection.setAutoCommit(true);
- PreparedStatement cmd=connection
- .prepareStatement("insert into test(name) values(?)");
- for(inti=0;i
- cmd.setString(1,"test");
- cmd.executeUpdate();
- }
- connection.commit();
- cmd.close();
- connection.close();
- longend=System.currentTimeMillis();
- System.out.println(end-start);
- }
- }
2.关闭Slave虚拟机
在JAVA程序运行当中,直接强制关闭Slave虚拟机。然后重新启动Slave

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

这个表除了自增的ID,就是name字段,默认都是test
现在在slave上修改name字段为‘mysql’
update test set name='mysql';
3.使用
pt-slave-restart
命令修复错误。
- [root@mysql2 ~]# pt-slave-restart --user=root --password=xxxxxxx--error-numbers=1062
- 2014-06-16T01:17:47 p=...,u=root mysql2-relay-bin.000003 312116 1062
- 2014-06-16T01:17:47 p=...,u=root mysql2-relay-bin.000003 312373 1062
- 2014-06-16T01:17:47 p=...,u=root mysql2-relay-bin.000003 312630 1062
- 2014-06-16T01:17:47 p=...,u=root mysql2-relay-bin.000003 312887 1062
- 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 313144 1062
- 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 313401 1062
- 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 313658 1062
- 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 313915 1062
- 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 314172 1062
- 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 314429 1062
- 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 314686 1062
- 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 314943 1062
- 2014-06-16T01:17:48 p=...,u=root mysql2-relay-bin.000003 315200 1594
- 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’。


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.

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.

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

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 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.

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.

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.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor