How do you back up and restore a MySQL database?
BeginnerHands-onMySQL
Answer
Short answer
Use mysqldump to export a database into a .sql file, and restore it by feeding that file back into the mysql client.
Step by step
# Back up one database
mysqldump -u root -p --single-transaction --routines shop > shop-$(date +%F).sql
# Back up all databases
mysqldump -u root -p --all-databases --single-transaction > all-$(date +%F).sql
# Compress it
mysqldump -u root -p --single-transaction shop | gzip > shop-$(date +%F).sql.gz
# Restore into a database
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS shop"
mysql -u root -p shop < shop-2026-11-26.sql
gunzip < shop-2026-11-26.sql.gz | mysql -u root -p shop
--single-transactiontakes a consistent backup without locking InnoDB tables, so the app keeps working during the backup.--routinesalso saves stored procedures and functions.
Example — a nightly backup with cron
# /etc/cron.d/mysql-backup — every night at 2 a.m., upload to S3
0 2 * * * root mysqldump --single-transaction shop | gzip > /backup/shop-$(date +\%F).sql.gz && aws s3 cp /backup/shop-$(date +\%F).sql.gz s3://company-db-backups/
The golden rule: a backup you've never restored is not a backup. Test restores regularly.
What to say in the interview
"For logical backups I use mysqldump with --single-transaction so InnoDB tables aren't locked, compress the output and copy it to S3 on a schedule. Restore is just piping the file into the mysql client. On RDS we rely on automated backups and snapshots, with point-in-time recovery. And we test restores regularly, because an untested backup can't be trusted."
Follow-up questions you may get
- Is mysqldump good for very large databases? It's slow for hundreds of GB; physical backup tools like Percona XtraBackup, or RDS snapshots, are faster.
- What is point-in-time recovery? Restoring to any second in time using a full backup plus the binary logs.
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?BeginnerWhat is a relational database?BeginnerWhat is the difference between a database, a table, a row and a column?BeginnerHow do you check whether MySQL is running and which port it listens on?Beginner