search
HomeDatabaseMysql TutorialHow does MySQL ensure the consistency of backup data?

Preface

For data security, the database needs to be backed up regularly. Everyone knows this. However, when backing up the database, you are most afraid of write operations, because this is the most likely to cause data inconsistency. Brother Song gives a simple example Let’s take a look at an example:

Assume that during the database backup period, a user places an order, then the following problems may occur:

  • Inventory table deduction inventory.

  • Back up the inventory table.

  • Back up order table data.

  • Add orders to the order table.

  • The user table deducts the account balance.

  • Back up the user table.

If you follow the above logic, the order table in the backup file will be missing a record. If you use this backup file to restore data, one record will be missing, resulting in data inconsistency.

In order to solve this problem, MySQL provides many solutions. Let us explain them one by one and analyze their advantages and disadvantages.

1. Full database read-only

To solve this problem, the easiest way we can think of is to set the database to be read-only during database backup and cannot be written, so that we don’t have to worry about data inconsistency. The method to set the whole database to be read-only is also very simple. First, we execute the following SQL and look at the values ​​of the corresponding variables:

show variables like 'read_only';

How does MySQL ensure the consistency of backup data?

You can see that by default, read_only is OFF, that is, closed state. We first change it to ON and execute the following SQL:

set global read_only=1;

1 means ON, 0 means OFF, and the execution result is as follows:

How does MySQL ensure the consistency of backup data?

Thisread_only is not valid for the super user, so after the setting is completed, we exit this session, then create a user without super permissions, log in with the new user, log in After success, execute an insert SQL and the result is as follows:

How does MySQL ensure the consistency of backup data?

As you can see, this error message says that the current MySQL is read-only (can only query) and cannot Execute the current SQL.

With the read-only attribute, you don’t have to worry about data inconsistency during backup.

Butread_only We usually use it to identify whether a MySQL instance is the master or slave library:

  • read_only=0, indicating that the instance is the master library. The database administrator DBA may write some non-business data to the instance every once in a while to determine whether the main library is writable and available. This is a common way to detect whether the main library instance is alive.

  • read_only=1, indicating that the instance is a slave library. Usually, when regularly exploring the slave library, only some read operations will be performed, such as executing statements such as "select 1;".

So, the read_only attribute is not suitable for backup, and if the read_only attribute is used, the entire library will be set to readonly After that, if an exception occurs on the client, the database will remain in the readonly state, which will cause the entire library to be in an unwritable state for a long time, which is a high risk.

Therefore this solution is not qualified.

2. Global lock

Global lock, as the name suggests, locks the entire library. The locked library cannot be added, deleted or modified, only read.

Then let’s see how to use global locks. MySQL provides a method to increase the global read lock. The command is flush tables with read lock (FTWRL). When you need to make the entire library in a read-only state, you can use this command, and then operations such as additions, deletions, and modifications by other threads will be blocked.

How does MySQL ensure the consistency of backup data?

As you can see from the picture, use the flush tables with read lock; command to lock the table; use unlock tables; command can complete the unlocking operation (it will also be automatically unlocked when the session is disconnected).

Compared with the solution in the first section, FTWRL has made some progress, that is: if the client disconnects abnormally after executing the FTWRL command, MySQL will automatically release the global lock, and the entire library can be updated normally. status instead of remaining in read-only status.

but! ! !

Adding a global lock means that the entire database is in a read-only state during the backup period, so the business can only be stopped during the database backup period.

So this method is not the best solution.

3. Transactions

I don’t know if you friends still remember the isolation level of the database that Brother Song shared with you before. One of the four isolation levels is repeatable read ( REPEATABLE READ), which is also the default isolation level of MySQL.

If a user executes the same SELECT statement multiple times in another transaction under this isolation level, the result will always be the same. (Because the data changes caused by the executing transaction cannot be seen by the outside world).

换言之,在 InnoDB 这种支持事务的存储引擎中,那么我们就可以在备份数据库之前先开启事务,此时会先创建一致性视图,然后整个事务执行期间都在用这个一致性视图,而且由于 MVCC 的支持,备份期间业务依然可以对数据进行更新操作,并且这些更新操作不会被当前事务看到。

在可重复读的隔离级别下,即使其他事务更新了表数据,也不会影响备份数据库的事务读取结果,这就是事务四大特性中的隔离性,这样备份期间备份的数据一直是在开启事务时的数据。

具体操作也很简单,使用 mysqldump 备份数据库的时候,加上 -–single-transaction 参数即可。

为了看到 -–single-transaction 参数的作用,我们可以先开启 general_loggeneral_log 即 General Query Log,它记录了 MySQL 服务器的操作。当客户端连接、断开连接、接收到客户端的 SQL 语句时,会向 general_log 中写入日志,开启 general_log 会损失一定的性能,但是在开发、测试环境下开启日志,可以帮忙我们加快排查出现的问题。

通过如下查询我们可以看到,默认情况下 general_log 并没有开启:

How does MySQL ensure the consistency of backup data?

我们可以通过修改配置文件 my.cnf(Linux)/my.ini(Windows),在 mysqld 下面增加或修改(如已存在配置项)general_log 的值为1,修改后重启 MySQL 服务即可生效。

也可以通过在 MySQL 终端执行 set global general_log = ON 来开启 general log,此方法可以不用重启 MySQL

How does MySQL ensure the consistency of backup data?

开启之后,默认日志的目录是 mysql 的 data 目录,文件名默认为 主机名.log

接下来,我们先来执行一个不带 -–single-transaction 参数的备份,如下:

mysqldump -h localhost -uroot -p123 test08 > test08.sql

How does MySQL ensure the consistency of backup data?

大家注意默认的 general_log 的位置。

接下来我们再来加上 -–single-transaction 参数看看:

mysqldump -h localhost -uroot -p123 --single-transaction test08 > test08.sql

How does MySQL ensure the consistency of backup data?

大家看我蓝色选中的部分,可以看到,确实先开启了事务,然后才开始备份的,对比不加 -–single-transaction 参数的日志,多了开启事务这一部分。

The above is the detailed content of How does MySQL ensure the consistency of backup data?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools