ANS Documentation

Improve This Doc
  • Cloud
  • Domains and DNS management
  • Backup and High Availability
  • eCommerce Stacks
  • Security
  • Email
  • Monitoring and usage management
  • Networking
  • Operating systems
    • Linux
      • Basics
      • Apache
      • Control Panels
      • FTP
      • Magento
      • Miscellaneous
      • MySQL
      • NGINX
      • PHP
      • PHP-FPM
      • Setting up Websites
      • NFS
      • VPN
      • Percona
      • SSH
      • SSL
      • Git
      • Redis
      • Elasticsearch
      • MongoDB
      • Ncdu
    • VMware ESXi
    • Windows
  • Webcelerator
  • MyUKFast
  • Home >
  • Operating systems >
  • Linux >
  • MySQL >
  • Import A Database

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¶

  1. Log in to the PHPMyAdmin console.

  2. On the left-had side, select the database you wish to import data into.

  3. Select the Import tab.

  4. Select Browse under File to import and select the .sql you wish to import.

  5. Hit Go to start the import of the database file.

  6. When successful, a message should appear Import has been successfully finished

PHPMyAdmin

MySQL Workbench¶

  1. Log in to the MySQL Workbench console and and connect to the database.

  2. On the left-had side, under Management select Data Import/Restore.

  3. Select the Data Import tab.

  4. Select the option “Import from Self-Contained File” and browse to the .sql file you wish to import.

  5. Select an existing database or the option to choose a new database to import into.

  6. Hit Start Import the import of the database file.

  7. When successful, a message should appear Import of /root/exampl_db.sql has finished

MySQL Workbench

Next Article > Upgrading your MySQL/MariaDB Version

  • Useful Links
  • SMB
  • Enterprise
  • Channel
  • Public Sector
  • ANS Data Centres
  • About ANS
  • Careers
  • Blog
  • Get in touch
  •  
  • Sales 0800 458 4545
  • Support 0800 230 0032
  • Get in touch

© ANS Group Limited | Terms and Conditions | Corporate Guidance | Sitemap
ANS Group Limited, registered in England and Wales, company registration number 03176761, registered office 1 Archway, Birley Fields, Manchester M15 5QJ