What does --single-transaction do in mysqldump?
IntermediateConceptMySQL
Answer
Short answer
It makes mysqldump start a transaction with REPEATABLE READ isolation and dump everything from that one consistent snapshot — so you get a consistent backup of InnoDB tables without locking them, while the application keeps reading and writing.
Example
mysqldump -u backup -p --single-transaction --routines --triggers --events shop | gzip > shop_$(date +%F).sql.gz
Without it
mysqldump's default (--lock-tables) locks tables while dumping each database — writes are blocked, which can stall a busy production app. Or, if you disable locking, the dump may be inconsistent (orders referencing customers that weren't dumped yet).
Caveats
- Works only for InnoDB (MyISAM tables aren't transactional).
- Don't run DDL (
ALTER TABLE) during the dump — it can break the snapshot. - For point-in-time recovery, add
--source-data=2(older:--master-data=2) to record the binlog position.
What to say in the interview
"--single-transaction dumps all InnoDB tables from one consistent MVCC snapshot without blocking writes, so it's safe on a live production database. I combine it with routines, triggers and the binlog position for point-in-time recovery."
More on MySQL
How do you set up MySQL and log in to it?BeginnerHow do you create a database, a table and see data in MySQL?BeginnerHow do you create a MySQL user for an application and give it permissions?BeginnerHow do you back up and restore a MySQL database?BeginnerWhat is a relational database?BeginnerWhat is the difference between a database, a table, a row and a column?Beginner