MySQL - How to Export and Import tables with the Command Line

2 commands I use often throughout the day is importing and exporting large databases into MySQL VIA the command line. Here is how I do it where "USERNAME" is your username, "PASSWORD" is your password and "DATABASE" is your database name.

To Export:

 mysqldump -uUSERNAME -pPASSWORD DATABASE | gzip -c > ~/dump_2010-06-14.sql.gz 

To Import:

 mysql -uUSERNAME -pPASSWORD DATABASE < database.sql 

Also, you may have a large database that may give you errors while importing. You can use this command to force the import without warnings or errors:

 mysql -f -uUSERNAME -pPASSWORD DATABASE < database.sql 

2 comments for MySQL - How to Export and Import tables with the Command Line

Carl's picture

Great! Definitely going to...

Great! Definitely going to keep this in my toolbox!

Brian's picture

Thanks for this short but...

Thanks for this short but incredibly useful piece of code!

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.