How to Import and Export Databases in MySQL or MariaDB

Published on April 15, 2024

There are numerous instances when you may need to import or export databases in MySQL or MariaDB. As a developer or programmer, knowing how to import and export databases will save you time and help enhance your efficiency when working with databases.

For instance, if you’ve been working on a database on your local server, how can you migrate it to another server if you don’t know how to export and import it? Now that you have this thought in mind, read on. This post will give a practical example of exporting a database in MySQL or MariaDB and importing the exported database. Let’s get started!

How to Export a Database in MySQL or MariaDB

MySQL and MariaDB are the same. The steps to import or export the databases are the same. When working with a database, you can generate a data dump. The database dump will contain all the associated data for that database. That way, you can easily migrate the database to another location and import the data dump.

We’ve displayed the existing tables in our database, and the output shows that we only have two tables. The database containing these tables is named “students,” and it’s the one we will export.

To create the data dump, exit your MySQL shell after confirming which database you want to export. MySQL or MariaDB offers the mysqldump to help create the data dump for the exported database.

Run your command as follows, replacing ‘students’ with your database and ‘root’ with your database user.

Copy

Once you have the password for the associated database user, your data dump will be created and saved in your current directory. We’ve named our database dump ‘data.sql.’

You can verify the existence of the created data dump using the ls command. Moreover, you can read the contents of the data dump using commands such as ‘cat’ or ‘head’ to check its contents, as we’ve done below.

Copy
Copy

At this point, you are done with exporting the database.

How to Import Databases in MySQL or MariaDB

Importing your data dump is easy. The first step is to access your MySQL or MariaDB database and create a new one to hold the exported database’s contents.

For our case, we’ve logged into our database using our root user.

Copy

Once you access the shell, proceed to create a database. For our case, we’ve created the ‘linuxmeta’ database.

You can then exit the shell by typing ‘exit.’

Importing the database follows the same procedure as exporting it. One key thing to note is the direction of the redirection arrow. We used (>) for the export, but we reversed the direction for the import.

Again, you must specify the user account to use when accessing the database to export the data dump, followed by the database you created earlier.

Use the syntax below.

Copy

Once you enter your password, the database will automatically get imported, but you won’t get any output on your terminal.

However, access your database and run the command below to check that your database is there.

Copy

Next, select the database you want to use and display the available tables. Your output should be the same tables that you exported earlier.

Copy
Copy

You’ve Successfully Imported And Exported Databases In MySQL Or MariaDB.

Conclusion

Exporting and importing databases in MySQL or MariaDB is easy. First, confirm which database you want to export, then use the mysqldump command to export that database to a data dump. Next, access your server or where you want to import the database and create a new database to hold the contents of the exported database. Then, import the data dump to the created database. This post discusses all the steps alongside an example.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price