# mysqldump --single-transaction: what is and when to use it
One of the options that mysqldump supports is --single-transaction flag. In this article, we will explain what this option does and when to use it.
# What is --single-transaction
The --single-transaction option allows mysqldump to backup a database using a single transaction. This means that the entire backup process will be executed within a single transaction, ensuring that the database remains in a consistent state throughout the backup.
# Why use --single-transaction
Using the --single-transaction option is recommended for databases that are frequently updated or have large amounts of data. This is because it ensures that the database remains in a consistent state throughout the backup, which is important if you need to restore the database later.
# Which engines have support for single transactions?
It's important to note that not all storage engines support transactions. If your database uses a storage engine that doesn't support transactions, using the --single-transaction option will not have any effect. Only the following MySQL engines have support for single transactions:
InnoDBNDB
# How to use --single-transaction
The option can be used by passing the --single-transaction flag as in the example below:
mysqldump --single-transaction my_database > my_database.sql
# Using it together with --skip-lock-tables
Using the --single-transaction and --skip-lock-tables options together can provide several benefits:
- consistent backup: the
--single-transactionoption ensures a consistent backup of your database by wrapping the entire mysqldump operation in a single transaction. This guarantees that the backup reflects a consistent state of your database at the time the transaction started - avoids locking tables: the
--skip-lock-tablesoption avoids locking tables during the backup process, which can prevent downtime and disruption to active operations
Overall, using both options together provides a consistent, low-impact backup of your MySQL database, making it an ideal choice for environments where minimizing downtime and disruption is a priority.