How to Import and Export a Database via SSH

You can easily import/export your database using the phpMyAdmin menu in cPanel, with this guideline. However, it only allows the import and export of databases that do not exceed 1 GB. If your database is bigger, this is where you will need to use SSH commands.

Based on default, SSH Shell access is disabled on our Shared hosting plans. To use the shell access terminal, please contact us by opening a ticket with the subject: ” SSH Access“.

It is important to note that before performing an export or import, you will need to assign a database to a user. You can do it in cPanel > MySQL Databases menu > Add a User to a Database option.

WebManager.NG Website Images

Ensure you enable all privileges

webmanager.ng website image

To import a database, use the following command:

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

You can also use:

mysql -u dbusername -p’dbpassword’ dbname < /path/to/file/file.sql

or

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

To export a database, use this command:

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

The variables in italics are the following:

  • dbusername: the name of a database user assigned to this database (it should be in the format of CPANELUSERNAME_DATABASEUSER), or your cPanel username
  • dbpassword: the database user password (if you use a database user as a username) or your cPanel password (if you use your cPanel username as a username)

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

  • dbname: the name of your database you are importing to or exporting from. It should be created in your cPanel. The format is the following: CPANELUSERNAME_DATABASENAME
  • /path/to/file/file.sql: the path to the mysql dump that you are importing to (then a new file will be created) or exporting from your cPanel account. 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. With the absolute path, for example, it will be the following: /home/CPANEL_USERNAME/public_html/database.sql

If you do not receive any error or notification after pressing Enter, it means that the command was performed successfully.

We will use the following settings as example:

dbusername: nctests_user (as a database user), nctests (as a cPanel username)
dbpassword: 123qwe
dbname: nctests_database
path to the file: /home/nctests/public_html/databases/db_file.sql

Database Import

mysql -u nctests_user -p123qwe nctests_database < /home/nctests/public_html/databases/db_file.sql

WebManager.NG Website Images

Alternatively, you can use the following command:

mysql -u nctests_user -p nctests_database < /home/nctests/public_html/databases/db_file.sql

This way, you need to enter the password in the next line:

WebManager.NG Website Images

Database Export

mysqldump -u nctests_user -p123qwe nctests_database > /home/nctests/public_html/databases/db_file.sql

WebManager.NG Website Images

NOTE: You can also use the following command:

mysqldump -u nctests_user -p nctests_database > /home/nctests/public_html/databases/db_file.sql

This way, you need to enter the password in the next line:

WebManager.NG Website Images

With this guide, you should be able to import and export a database easily.

Contact us if you need any help.