This recipe shows you how to back up your MySQL databases using mysqldump
. The utility connects to the MySQL server, queries the structure of the database and its data, and outputs the data in the form of SQL statements. The backup can then be used to restore the database or populate a new database with the data.
This recipe requires a running MySQL server and access to either MySQL's root
user or another user with the necessary privileges to perform the backup.
Follow these steps to make a backup of a MySQL database:
Connect to the MySQL database you want to back up:
mysql -u root -p packt
Execute a
FLUSH TABLES
statement to set the database's tables read-only:FLUSH TABLES WITH READ LOCK;
Open a second terminal, leaving the first one active with the
mysql
client still running.In the new terminal, use
mysqldump
to export the table definitions and data:mysqldump -u root -p packt > backup.sql
Return to the first terminal...