How to Restore Database From SQL File in MySQL
- Restore Database From SQL File Using MySQL Workbench
- Restore Database From SQL File Using Command Prompt
This tutorial demonstrates restoring the database from a sql
file in MySQL.
Restoring the database from a .sql
is an easy operation and can be achieved using the MySQL workbench or command line. This tutorial demonstrates both methods of restoring the database from a SQL file.
Restore Database From SQL File Using MySQL Workbench
The MySQL Workbench can be used to restore the database from a .sql
file. The operation is very simple and can be done by following a few steps.
-
First, create an empty database on your server where you want to restore the database from the
SQL
file; if you want to restore it in the previous database, that also can be done, but you will lose changes. -
Now, on the left side, you can find the option
Data Import/Restore
. Click the option. -
Now, in the
Administration – Data Import/Restore
, you can find the optionImport from the Self-Contained File
; click it and browse to yoursql
file. -
Then, right under the
Import from the Self-Contained File
, you will find the optionDefault Target Schema
, from which you have to select the schema where you want to restore your database.
-
Now, at the bottom of this page, select
Dump Structure and Data
from the dropdown box. -
After completing the above steps, switch to the
Import Progress
tab on theAdministration – Data Import/Restore
Page. -
Final step is to click the
Import
button and wait for the database to be restored; it will take a few seconds.
Your database is now successfully restored using the .sql
file in MySQL Workbench.
Restore Database From SQL File Using Command Prompt
We can restore a database by using just one command in the command line. This command takes a few parameters, which are described below the command.
MySQL -u [Server User_Name] –p [database_name] < [RestoreFileName.sql]
Where:
- The
-u [Server User_Name]
is the user name for MySQL. In our case, it is theroot
. -p
is the password for the username we used above.database_name
is a database where we want to restore our database file, i.e., the target database.RestoreFileName.sql
is the name of the database file which will be restored. It should be with the complete file path.
Let’s try an example based on the above command.
MySQL --host=localhost --user=root --port=3306 -p newsakila < C:/Users/Sheeraz/OneDrive/Desktop/New folder/sakila.sql
Where the host will be our server name, and in our case, it is localhost
. The above command will restore the database newsakila
from the file sakila.sql
.
Let’s check our database now by using the following commands:
use newsakila
SHOW TABLES;
The above command will show that the sakila
SQL file is imported into the newsakila
database schema, which is the restoration. The output for the above commands is below.
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook