How to backup and restore a MySQL database.

The mysqldump command creates a text version of the database. Specifically, it creates a list of SQL statements which can be used to restore/recreate the original database.
The syntax is:
# mysqldump -u root -p[root_password] [database_name] > backup.sql – backup.
# mysql -u root -p[root_password] [database_name] < backup.sql - restore.

Back up a single database:
The mysqldump command will backup the mydb database and dumps the output to the mydb.sql file

# mysqldump -u root -pmysqlpassword mydb > mydb.sql

Actually, if you don't provide a password at this stage, later you will be prompted for it.

Back up the multiple databases:

# mysqldump -u root -pmysqlpassword --databases mydb somedb > mydb_somedb.sql

Back up all of the databases:

# mysqldump -u root -pmysqlpassword --all-databases > all_databases.sql

Back up a specific table:
In this example, we takes a back up only of the users table from the mydb database.

# mysqldump -u root -pmysqlpassword mydb users > mysb_table_users.sql

To restore the MySQL database from a backup:
Execute the mysql command with "<" as shown below. If you are going to restore the mydb.sql to a different server, the mydb database must be created on this server.
# mysql -u root -pmysqlpassword

mysql> create database mydb;
Query OK, 1 row affected (0.02 sec)

# mysql -u root -pmysqlpassword mydb < mydb.sql

If you are trying to restore a single database from dump of all the databases, you have to let mysql know like this:

mysql --one-database database_name < all_databases.sql

Leave a Reply

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