Informational Website publish

Blog

News about our company, tutorials about IT and much more you will find in this page.

How to import and export a database via SSH

Janeiro 30th, 2018

In this article we will show you how to import and export a MySQL database via SSH. Managing your databases via SSH is especially useful when dealing with large databases (over 50Mb).

1. Importing a MySQL database

To import a MySQl database, you need to use the mysql command. Here is the full command for importing a MySQL dump into a database:

  • mysql -u dbusername -pdbpassword dbname < /path/to/file/file.sql

Note: No space between -p key and the password if you enter it in the command.

dbusername: the name of a database user assigned to this database, or your cPanel username.
dbpassword: the database user password or your cPanel password (if you use your cPanel username as a username).
dbname: the name of your database you are importing to. You must use an existing database.
/path/to/file/file.sql: the path to the mysql dump that you are importing from. Here you can use both relative and absolute path formats. 
If you are in the folder with the file, you can just type the file name with the extension.

If you do not receive any error or notification after pressing Enter, it means that your chosen database has been imported into your destination database in MySQL.

2. Exporting a MySQL database

To export a MySQL database, you need to use the mysqldump command. Here is the full command for exporting your database:

  • mysqldump -u dbusername -pdbpassword dbname > /path/to/file/file.sql

Note: No space between -p key and the password if you enter it in the command.

dbusername: the name of a database user assigned to this database, or your cPanel username.
dbpassword: the database user password or your cPanel password (if you use your cPanel username as a username).
dbname:the name of your database you are exporting from. You must use an existing database.
/path/to/file/file.sql: the path to the mysql dump that you are exporting to (a new file will be created). 
Here you can use both relative and absolute path formats. If you are in the folder with the file, you can just type the file name with the extension.

If you do not receive any error or notification after pressing Enter, it means that the database that you selected in the command will now be exported to the specified path.

That’s it!