JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

How to use mysqldump to backup a live MySQL database without locking the tables

Author:JIYIK Last Updated:2025/04/24 Views:

Backing up MySQL isn't hard—you just run mysqldump and output to a file —but it's also not really designed for a production environment.

At first, running mysqldumptook only a few seconds so everything being locked wasn't a big deal, but over time our backups grew so large that if we ran a backup the whole site would be down for half an hour. Even with lots of caching this always meant someone would get an error page every day. The problem was that mysqldumplocked the database tables before exporting so no new content would be inserted during the export.

The solution is to use --single-transactionthe -lte parameter, which will give us a consistent backup without locking everything. What actually happens is that mysqldump will start a new SQL transaction, flush all pending writes, and then complete the backup as part of the transaction, which does not prevent other updates from occurring.

Note: The only thing to note is that your database tables should use InnoDBinstead of MyISAM. Since this has been the default for some time, we should be fine.

So now that we have that figured out, it's just a matter of adding the parameter to your normal backup routine like this:

$ mysqldump -uUser -pPass -hHost --single-transaction database > backup.bak

Now our backups will be more reliable.


Related Reading

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Two ways to install mysql-5.5.47 on Linux system and manage mysql

Publish Date:2025/04/26 Views:140 Category:MySQL

We know that there are generally two ways to install software on the Linux system. One is to use rpm or yum to install, which is convenient and fast; the other is to use the compiled source package. Although this method is more troublesome

Mysql master-slave replication simple configuration

Publish Date:2025/04/26 Views:120 Category:MySQL

I'm learning about MySQL master-slave replication recently and want to apply it to a project at work. Since I'm new to it, I don't understand it very well, so I can only share how to make a simple configuration. At the beginning, we configu

MySQL stored procedure details

Publish Date:2025/04/26 Views:163 Category:MySQL

A stored procedure can be thought of as encapsulating a SQL statement that we need to process specially into a function. When needed, we only need to call this function to achieve the desired operation. This process can be called a stored p

How many of these MySQL statement tags have you used?

Publish Date:2025/04/26 Views:122 Category:MySQL

In the article "A Peek into MySQL Stored Procedure Details" , we briefly introduced the use of stored procedures. The syntax for creating stored procedures includes BEGIN...END. In addition to BEGIN...END, the following statement tags can b

Back up the MySQL database to a file

Publish Date:2025/04/26 Views:166 Category:MySQL

Backing up your database is a very important system administration task that should usually cron be run from a job at scheduled intervals. We will use mysqldump the dump utility that comes with mysql to dump the contents of the database to

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial