Threat Picture
Latest Cybersecurity News
How to Import and Export MySQL Databases

How to Import and Export MySQL Databases

mysqldump out, mysql in. The command-line steps for backing up and restoring databases and single tables.
By Charles Joseph · Updated
Share
Share
Copy URL

Importing and exporting MySQL databases is a critical skill for any developer or database administrator.

This guide will walk you through the process of exporting and importing MySQL databases and tables using simple command-line instructions.

Exporting a MySQL Database:

When you need to back up your entire MySQL database, exporting it is the first step. To do this, use the mysqldump command:

$ sudo mysqldump -u [username] -p [dbname] > [path/to/your/filename.sql]

Replace [username] with your MySQL username, [dbname] with the name of your database and [path/to/your/filename.sql] with the desired path and filename for your exported database.

Sponsored

Example

$ sudo mysqldump -u root -p mydatabase > /home/user/dbexport.sql

Exporting a Specific Table from a Database:

Sometimes, you only need to export a specific table within a database. This can be done using the MySQL SELECT INTO OUTFILE command:

SELECT * FROM [table_name] INTO OUTFILE '[path/to/your/filename.txt]';

Replace [table_name] with the name of your table and [path/to/your/filename.txt] with the path and filename where you want to export the table.

Example

SELECT * FROM wp_posts INTO OUTFILE '/tmp/posts_export.txt';

Sponsored

Importing a Database:

To import a database back into MySQL, use the mysql command. This is particularly useful when restoring a database from a backup:

$ sudo mysql -u [username] -p [dbname] < [path/to/your/filename.sql]

Replace [username] with your MySQL username, [dbname] with the database name where you want to import and [path/to/your/filename.sql] with the path to your database export file.

Example

$ sudo mysql -u root -p mydatabase < /home/user/dbexport.sql

Conclusion

Mastering these commands for exporting and importing MySQL databases will make managing your databases much more straightforward. Whether it’s for backup, migration, or replication purposes, these skills are essential for any database professional.

Sponsored

Related Posts:

QUOTE:

"Amateurs hack systems, professionals hack people."