How to export MySQL database using Command Line

Taking the backup of the database is necessary whether it small or large projects. You can easily revert the database if something went wrong.

Using phpMyAdmin you can easily export your MySQL database but it got cancel if it exceeds the max defined time.

You can also Command-Line to export selected MySQL database.

In this tutorial, I am using XAMPP in windows.

How to export MySQL database using Command Line


Contents

  1. Export Database
  2. Export Table
  3. Conclusion

1. Export Database

  • I am exporting tutorial database using Command-Line.

View Database thats needs to export in phpMyAdmin

  • For this open Command Prompt.
  • Navigate to xampp/mysql/bin folder.

Navigate to mysql/bin using Command Prompt.

  • Execute the following command –

Syntax – 

mysqldump -u username -p database-name > export-file-name.sql

username – MySQL username.

database-name – Database name which you want to export.

export-file-name.sql – Specify you export file name.

Example –

mysqldump -u root -p tutorial > tutorial.sql

Here, I am exporting tutorial database and set export file name – tutorial.sql.

It will ask for a password.

Run export command in Command Prompt

Enter the user password if you have defined otherwise press enter.

A new SQL file will create in the xampp/mysql/bin/ folder.

View SQL file of exported MySQL database in xampp/mysql/bin


2. Export table

If you want to export specific tables instead of whole database then you can use below command –

Syntax –

mysqldump -p --user=[username] [databasename] tablename1 tablename2 ... > export-file-name.sql

Example (Export single table) – 

mysqldump -p --user=root webinar tutorial country > tutorial.sql
  • Here, I am exporting country table from tutorial database, and set export file name – tutorial.sql.
  • It will ask for a password.
  • Enter the user password if you have defined otherwise press enter.
  • A new SQL file will create in the xampp/mysql/bin/ folder.

Example (Export multiple tables) –

mysqldump -p --user=root webinar tutorial country userinfo > tutorial.sql
  • Here, I am exporting country and userinfo tables from tutorial database, and and set export file name – tutorial.sql.

3. Conclusion

You can either export your MySQL database using phpMyAdmin or Command-Line.

The SQL file stored in xampp/mysql/bin/ folder if you have used the command line.

With Command-Line you can also import SQL file in your MySQL database.

If you found this tutorial helpful then don't forget to share.

Leave a Comment