How to Import and Export MySQL Databases in Linux

April 25, 2019

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 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
how to check mysql dump file

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:

  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 setting up a blank database 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, enter:

USE new_db_name
use a mysql database example

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:

  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. 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.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How To Set Up MySQL Master Slave Replication
May 29, 2019

In cloud computing, master-slave data replication refers to storing the same information on multiple servers...
Read more
How to Rename a MySQL Database
May 6, 2019

As an administrator, you may need to rename a database. However, for security, the command to rename a...
Read more
How to Install MySQL 8.0 in Ubuntu 18.04
December 12, 2018

MySQL is an open-source relational database server tool for Linux operating systems. It is widely used in...
Read more
How to Reset or Change MySQL Root Password on Linux or Windows
October 21, 2018

MySQL is a database management system. It's operated under the Open Source software model and has become a...
Read more