Import A Database¶
This article will give information on how to import a database file via the CLI, PHPMyAdmin and MySQL Workbench. It will also discuss prerequisites that will help the import go smoothly.
Before The Import¶
Warning
Ensure the .sql
file does not contain USE
or ‘CREATE DATABASE’ statements as it can produce unexpected outcomes.
Use the following grep command to check for USE
and CREATE
statements:
egrep "^USE|^CREATE DATABASE" example_db.sql
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `example_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `example_db`;
Theses lines can be deleted manually, or by using the following sed
commands:
sed -i '/^USE/d' example_db.sql
sed -i '/^CREATE DATABASE/d' example_db.sql
Note
You will see multiple USE
or CREATE DATABASE
satements if the .sql
file contains more than one database. It’s recommended to import databases individually to limit risk of overwriting or currupting data. This guide assumes you are importing a single database.
Now when you run the grep again, there should be no returned values:
egrep "^USE|^CREATE DATABASE" example_db.sql
Once you have confirmed that there are no USE
and CREATE
statements in your .sql
file you are now ready to import the contents of the file into the database.
MySQL CLI¶
If the database you wish to import does not already exist, you will need to create it before the import:
MariaDB [(none)]> CREATE DATABASE example_db;
If the database already exists, this will import on top of the existing database. If this is not the desired effect, create a new database and update the application to point to the new database name.
To import the database, run the following command:
mysql -u root -p example_db < example_db.sql
PHPMyAdmin¶
Log in to the
PHPMyAdmin
console.On the left-had side, select the database you wish to import data into.
Select the
Import
tab.Select
Browse
underFile to import
and select the.sql
you wish to import.Hit
Go
to start the import of the database file.When successful, a message should appear
Import has been successfully finished
MySQL Workbench¶
Log in to the
MySQL Workbench
console and and connect to the database.On the left-had side, under Management select
Data Import/Restore
.Select the
Data Import
tab.Select the option “Import from Self-Contained File” and browse to the
.sql
file you wish to import.Select an existing database or the option to choose a new database to import into.
Hit
Start Import
the import of the database file.When successful, a message should appear
Import of /root/exampl_db.sql has finished