In this guide we’ll show you how to export and import MySQL databases with SSH. Managing your database through SSH is both secure and, when dealing with large databases, faster than using PHPMyAdmin.
Export/Backup a MySQL database
We’ll export your MySQL database using mysqldump. Here’s an example of the command you’ll want to run:
mysqldump -uUSERNAME -pPASSWORD DATABASENAME > db-backup.sql
Please replace USERNAME, PASSWORD and DATABASENAME with your MySQL database details. Also, the filename database-backup.sql can be changed to anything you like.
Press the Enter key to run the command.
Import/Restore a MySQL database
To import a MySQL database we’ll use the mysql command. Here’s an example of the full command to import your database:
mysql -uUSERNAME -pPASSWORD DATABASENAME < db-backup.sql
Please replace USERNAME, PASSWORD and DATABASENAME with your MySQL database details. The database has to exist for the import to run.
If you receive the error below
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
You can resolve this by adding
-h 127.0.0.1 to your MySQL commands.