Introduction
MySQL is a popular Linux-based database program. As a database, MySQL is a versatile application. It can be used for something as simple as a product database, or as complex as a WordPress website.
This tutorial will walk you through how to export a MySQL database and import it from a dump file in MySQL.
Prerequisites
- A system with MySQL installed and configured
- An existing database (a test or empty database will work)
- A root user account and password for the MySQL database
- A terminal window or command-line
- (optional) the phpMyAdmin utility
How to Export MySQL Database
Option 1: Use mysqldump Tool
Exporting a database puts it in a dump file that can be transferred to another system.
The MySQL database application includes a command mysqldump
to create a dump file of your database. This file can be used as a backup or copied to another system.
1. To export your MySQL database to a dump file, enter the following in a terminal window:
mysqldump –u username –p db_name > dump_file.sql
Replace username
with the actual username for the MySQL database, and db_name
with the name of the database. You can rename dump_file.sql
to anything you’d like but keep the .sql extension.
2. Confirm the export by entering:
head –n 5 dump_file.sql
The system should display the first five lines of the dump file you have just created. You should recognize the database name that’s listed.
Note: The dump file is saved to the working directory you run it from. This is typically the home directory for the system’s current user account.
Option 2: Use phpMyAdmin
PhpMyAdmin is a handy utility for managing web servers. It includes powerful tools for graphically managing files.
To export your MySQL database using phpMyAdmin:
- Log in to cPanel
- In the Databases section, select phpMyAdmin
- In the column on the left, choose the database to export
- Once it loads, click the tab on top to export
- The export method should be Quick
- The format should be SQL
- Click Go, then use the Save File dialog to save a copy of the exported database.
How to Import MySQL Database
Option 1: Import MySQL Using mysqldump Tool
Importing a database requires setting up a blank database first.
1. To create a blank database, launch the MySQL shell by entering:
mysql –u root –p
2. Enter the root password when prompted. The command prompt should change to show that you’re working in MySQL.
3. Next, create a new database by entering the following:
CREATE DATABASE new_db_name;
The system should return with Query OK, 1 row affected (0.00 sec). Note: The command won’t run if the semicolon isn’t entered at the end of the command.
4. Exit the MySQL shell by pressing Ctrl-D. The command prompt should return to normal.
5. Enter the following command to import the dump file:
mysql –u username –p new_db_name < dump_file.sql
You’ll only see a response if there are errors. A successful import won’t display any comments on the screen.
6. To check the database, log back into the MySQL shell:
mysql –u root –p
7. To load the database, enter:
USE new_db_name
8. Display the contents of the database by typing:
SHOW TABLES;
Option 2: Importing with phpMyAdmin
Step 1: Create New MySQL Database and Assign User
Before you can import a database using phpMyAdmin, you’ll need to create a blank database first:
- Log into cPanel
- Under Databases navigate to MySQL Databases
- Enter the name of the new database in the Create New Database field, then select Create Database
- Once it finishes, click Go Back, then click Add User to Database
- Select the correct user in the User box, select the new database in the Database list box, then Add
- Select All Privileges (unless you have a reason or policy that specifies account privileges)
- Click Make Changes
Step 2: Import MySQL Database with phpMyAdmin
To import a dump file into the new database using the phpMyAdmin web interface:
- Log into cPanel
- In the Databases section, select phpMyAdmin
- In the left column, select the newly-created database as seen in the image below:
- Click the Import tab (next to the Export tab)
- Next to File to Import, select Browse, then choose the file you downloaded from the export operation and select Go
Note: These commands can all be run with a root
database user account. If you already have another user account set up, you can use it instead. Before you begin, make sure that the account has the correct privileges. Or, you may add a new MySQL user account if you prefer.
Conclusion
In reading this guide, you now know two ways to export and import a MySQL database.
One uses a command line to create a local dump file, which you can copy or transfer to a new system to import. The other uses a graphical utility to generate the dump file.
Also, check out our article on how to easily export from MySQL database table to CSV and how to import CSV into MySQL table.