Ubuntu Linux

Backup and Restore MySQL database with mysqldump

Richard
Written by
Richard
Feb 23, 2025 Updated Mar 20, 2026 3 min read
Backup and Restore MySQL database with mysqldump

This article explains how to backup and restore MySQL databases using the mysqldump tool on Ubuntu 24.04

mysqldump is a command-line utility used to create a backup of MySQL databases. It lets you create backups of your databases to prevent data loss in case of hardware failure, data corruption, or accidental deletion.

The SQL output generated by mysqldump is standard SQL, making it compatible with other database management systems that can interpret SQL.

Backup MySQL database

Before backing up your MySQL database, you should lock all the database tables.

Run the command below to back up all databases while locking all tables to prevent writing and reading during backup.

🐧Bash / Shell
sudo mysqldump --lock-all-tables --all-databases --events > mysql_backup.sql

You can back up all the databases with the transaction option to maintain data integrity without locking the tables.

🐧Bash / Shell
sudo mysqldump --single-transaction --all-databases --events > mysql_backup.sql

If you want to backup a specific database, run the command below.

🐧Bash / Shell
sudo mysqldump database_name --single-transaction --events > mysql_backup.sql

Restore MySQL database

After backing up your database, you can restore it using the command below.

If you backed up all the databases, run the command below to restore.

🐧Bash / Shell
sudo mysql < mysql_backup.sql

To restore a specific database, create an empty database, then restore.

🐧Bash / Shell
sudo mysql database_name < mysql_backup.sql

That should do it!

Conclusion:

Backing up and restoring MySQL databases using mysqldump is a straightforward process that can save you from potential data loss. By following the commands outlined, you can ensure the safety and integrity of your database. Here are some key takeaways:

  • Regular Backups: Schedule regular backups to avoid data loss due to unexpected issues.
  • Use Locking Wisely: Choose between locking tables and single-transaction methods based on your needs.
  • Specific Database Backups: If required, you can focus on specific databases rather than backing up all databases.
  • Test Restores: Regularly test your backup and restore process to ensure reliability.
  • Monitor Backup Files: Monitor backup file sizes and retention policies to manage storage efficiently.

Following these practices will help maintain the health of your MySQL databases and ensure you are prepared for any unforeseen circumstances.

Frequently Asked Questions

How do I backup my MySQL database using mysqldump?

You can backup your MySQL database by using the command 'sudo mysqldump --single-transaction --all-databases --events > mysql_backup.sql' for all databases, or 'sudo mysqldump database_name --single-transaction --events > mysql_backup.sql' for a specific database.

What is the purpose of using --lock-all-tables in mysqldump?

The --lock-all-tables option locks all tables in the database during the backup process to prevent any changes or reads, ensuring data consistency and integrity.

How can I restore my MySQL database from a backup?

To restore your MySQL database, use the command 'sudo mysql < mysql_backup.sql' for all databases, or create an empty database and run 'sudo mysql database_name < mysql_backup.sql' for a specific database.

Is it necessary to lock tables when backing up MySQL databases?

Locking tables is not always necessary; you can use the --single-transaction option to create a backup without locking, which is useful for maintaining data integrity while allowing reads and writes.

How often should I backup my MySQL database?

It's recommended to schedule regular backups based on your data update frequency and business needs, ensuring that you minimize data loss in case of unexpected issues.

Was this guide helpful?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, the owner and lead writer at Geek Rewind, is a tech enthusiast passionate about simplifying complex IT topics. His years of hands-on experience in system administration and enterprise IT operations have honed his ability to provide practical insights and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

2464 articles → Twitter

📚 Related Tutorials

How to Backup and Restore Files in Windows 11
Windows How to Backup and Restore Files in Windows 11
How to Install NetData on Ubuntu 24.04
Ubuntu Linux How to Install NetData on Ubuntu 24.04
How to Install Emby Media Server on Ubuntu 24.04
Ubuntu Linux How to Install Emby Media Server on Ubuntu 24.04

One response to “Backup and Restore MySQL database with mysqldump”

Leave a Reply

Your email address will not be published. Required fields are marked *