How to import database in MySQL

dbForge Studio for MySQL has advanced functionality that simplifies the data export process. For example, it's not necessary to export an entire table. You can select to export all rows, selected rows only, or export a certain range of rows. This feature makes data transfer easier and saves your precious time.

You can specify error processing behavior and logging options: prompt a user for an action, ignore all errors or abort at the first error. You can also create a log file with a report and allocate
a path to it.

dbForge Studio for MySQL allows you to save templates for recurring export scenarios. This helps you avoid wasting time configuring options repeatedly.

You can set up the table grid options for exported data. You can configure header text color and background, the width and color of borders, and the text color and background of rows. For your convenience, you can make even and odd rows look different.

By setting up data format representation, you can encourage and deliver unified data standards within your organization.

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

head –n 5 dump_file.sql
0 with the name of the database. You can rename
head –n 5 dump_file.sql
1 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

head –n 5 dump_file.sql
2 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.