How to Import/Export a MySQL Database via SSH 💾
When working with large databases (typically 128 MB or more), managing them via SSH is a faster and more reliable method. If you need help setting up an SSH connection, feel free to check out our related guides: Download PuTTYIn this guide, I’ll explain step-by-step how to export and import a MySQL database via SSH. Don’t forget to replace the variables in the commands below with your own information:
- USERNAME: Your MySQL username.
- DATABASE: The name of your MySQL database.
Exporting a MySQL Database
To export a MySQL database, we use themysqldump command. Here’s the full command you’ll need:
Bash:
mysqldump -u USERNAME -p DATABASE > backup.sql
After running this command, you’ll be prompted to enter a password — this is the password for your MySQL user. Once entered, the database will be exported to a file named “backup.sql” in your current directory.
Importing a MySQL Database
To import a MySQL database, themysql command is used. Here is the full command for that process:
Bash:
mysql -u USERNAME -p DATABASE < backup.sql
Again, after executing the command, you’ll be asked to enter your MySQL user password.
The
backup.sql part of the command refers to the file you want to import into your database. Make sure that file exists in the directory where you run the command.