Import & Export Databases In MySQL Ubuntu CLI - Part 5

sohail
sohail

Table of Contents

In this article of MySQL series, we’ll learn to import & export DBs. When you work with SQL, you will often be taking a backup of your databases. You will also be importing previously taken backups. All of this is an integral part of learning MySQL.

Exporting a Database

We first need a database that can be exported. You can see in the image below that I have a database named theitstuff. We will be exporting it here. To export the database we need to open our command line and type the following command.

 mysqldump -uroot -proot theitstuff > theitstuff.sql; 

This will export out database into a file called theitstuff.sql in the same folder where the command line is opened from. The file that was created can be opened in some text editor of your choice and it should look like this,

database export file in mysql

You can see that the contents of this file are nothing but SQL codes. We will now see how to import a previously exported database.

mysql import database ubuntu cli

Importing a database

In the previous example, we exported a database. Now we will try to import it. Firstly we need to delete all tables from our current database; You can see in the image below that there are no tables in our current database.

mysql -uroot -proot theitstuff < theitstuff.sql;

You can see that the arrow sign has changed since this time data is being copied from the file to the database. Once you are done with this command you can see that the import has successfully worked.

mysql list databases ubuntu cli

Conclusion

Database imports and exports are very important concepts in MySQL. We have learned how to import and export a given database using the MySQL command line client. We also saw the export file is basically a big list of SQL commands and nothing else. I hope you understood this topic. If you have any queries regarding this topic, do let me know in the comment section below.

MySQL seriesUncategorizedhow-to