How to Restore a MySQL Database from a Dump?

MySQL is one of the most popular relational databases, used to store information in many web applications. Sometimes it becomes necessary to restore a database from a dump – a backup copy created in case of system failures or data loss. In this article, we will look at how to restore a MySQL database from a dump using the command line.

Step 1: Preparing the Database Dump

The first step is to have a ready-made database dump that will be used for restoration. To create a dump, you can use the command:

mysqldump -u username -p database_name > dump.sql

Where username is your MySQL username, database_name is the name of the database, and dump.sql is the name of the file to which the dump will be saved.

Make sure the database dump is created successfully before proceeding to the next step.

Step 2: Deleting the Existing Database

Before restoring the database from a dump, you need to delete the existing database. To do this, use the command:

mysql -u username -p -e "DROP DATABASE database_name;"

Where username is your MySQL username, and database_name is the name of the database you want to delete.

Step 3: Restoring the Database from the Dump

The final step is to restore the database from the previously created dump. To do this, use the command:

mysql -u username -p database_name < dump.sql

Where username is your MySQL username, database_name is the name of the database, and dump.sql is the dump file that you want to restore.

After completing all the steps, the database should be successfully restored from the dump and ready to use.

Thus, restoring a MySQL database from a dump is a relatively simple process, but it requires attention and precise execution of commands. By following these steps, you can quickly and easily restore your database and continue working on your project.