Home MySQL How To Import and Export MySQL Databases in Linux

How To Import and Export MySQL Databases in Linux

Importing and exporting MySQL or MariaDB databases is a regular task in system administration. You can use data dumps to back up and restore your databases or migrate them to a new server.

[ You might also like: How to Backup All MySQL Databases from Command Line ]

In this article, you will learn how to export a MySQL or MariaDB database and then import that database from the dump file in Linux.

Exporting a MySQL or MariaDB Database

To export a database, you need to use a mysqldump client utility that creates the logical backup of the databases to SQL text files, which makes it easier to transfer files from one server to another.

Run the mysqldump command to export your database:

$ mysqldump -u username -p database_name > linuxshelltips.sql
  • username – The username used to log in to the database server.
  • database_name – The name of the database to export.
  • linuxshelltips.sql – The filename used to stores the database output in the current directory.

The above command doesn’t print any visual output, but you can investigate the contents of the SQL dump file using the following command.

$ head -n 5 linuxshelltips.sql

The beginning of the file should look similar to this, showing a MySQL dump for a database named linuxshelltips.

Export MySQL Database
Export MySQL Database

If any errors fall during the export procedure, mysqldump will show them to the screen.

Importing a MySQL or MariaDB Database

To import an existing database, you first need to create a new database in your MySQL or MariaDB server using a root user or another user with adequate privileges.

$ mysql -u root -p

Once you connected to the MySQL shell, you need to create a new database with the following command.

MariaDB [(none)]> CREATE DATABASE linuxshelltips;
Create MySQL Database
Create MySQL Database

Next exit the MySQL shell by typing quit or hitting CTRL+D. From the normal command line, you can import the dump file with the following command:

$ mysql -u username -p linuxshelltips < linuxshelltips.sql

Once the database has been imported, you can check the database by log in to the MySQL shell and run the following commands.

$ mysql -u root -p
MariaDB [(none)]> USE linuxshelltips;
MariaDB [(none)]> SHOW TABLES;
Check MySQL Database
Check MySQL Database

In this article, we have seen how to export and import MySQL or MariaDB databases in Linux.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

2 thoughts on “How To Import and Export MySQL Databases in Linux”

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.