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.

how to import and export mysql database in linux

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 them 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 anything you’d like, keep the .sql at the end.

2. Confirm the export by entering:

head –n 5 dump_file.sql

how to check mysql dump file

The system should display the first five lines of the dump file you 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:

  1. Log in to cPanel
  2. In the Databases section, select phpMyAdmin

launch phpmyadmin in cpanel

  1. In the column on the left, choose the database to export
  2. Once it loads, click the tab on top to export
  3. The export method should be Quick
  4. The format should be SQL
  5. 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 that a blank database is set up first.

1. To create a blank database, launch the MySQL shell by entering:

mysql –u root –p

launching the mysql shell

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;

creating a new mysql database

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

displaying no output

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:

USE new_db_name

use a mysql database example

8. Display the contents of the database:

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 need to create a blank database first:

  1. Log into cPanel
  2. Under Databases navigate to MySQL Databases
  3. Enter the name of the new database in the Create New Database field, then select Create Database

create mysql database in cpanel

  1. Once it finishes, click Go Back, then click Add User to Database
  2. Select the correct user in the User box, select the new database in the Database list box, then Add
  3. Select All Privileges (unless you have a reason or policy that specifies account privileges)
  4. Click Make Changes

Step 2: Import MySQL Database with phpMyAdmin

To import a dump file into the new database using the phpMyAdmin web interface:

    1. Log into cPanel
    2. In the Databases section, select phpMyAdmin

launch phpmyadmin in cpanel

  1. In the left column, select the newly-created database as seen in the image below

select a database in phpmyadmin

  1. Click the Import tab (next to the Export tab)
  2. 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. However, if you already have another user account set up, you can use it instead. Make sure that account has the correct privileges. Or, you may add a new user account to MySQL if you prefer.


Conclusion

This guide provides two different methods for exporting and importing 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.