MySQL Export and Import SQL Command Tutorial

Tech Sharing 2017-12-19
MySQL Export and Import SQL Command Tutorial

Use mysqldump to export a whole database, a single table, or just the structure, then import with the source command — a quick reference for MySQL backup and restore.

1. Export the whole database

mysqldump -u USER -p DBNAME > OUTPUT.sql
e.g. mysqldump -u root -p dbname > dbnamebak.sql

2. Export a single table

mysqldump -u USER -p DBNAME TABLE > OUTPUT.sql
e.g. mysqldump -u root -p dbname member > dbname_member.sql

3. Export structure only

mysqldump -u root -p -d --add-drop-table dbname > d:/dbname_db.sql

Note: -d exports no data; --add-drop-table adds a DROP TABLE before each CREATE.

4. Import (commonly via source)

Enter the MySQL console:

mysql -u root -p
mysql> use DBNAME
mysql> source d:/dbnamebak.sql

The argument after source is the script file (here a .sql file).