How to manage MySQL Databases via SSH

Exporting MySQL Data

This example shows you how to export a database. It is a good idea to export your data often as a backup.

  1. Using SSH, execute the following command:
            
            mysqldump -p -u username database_name > dbname.sql
    
  2. You will be prompted for a password, type in the password for the username and press Enter. Replace username, password and database_name with your MySQL username, password and database name.

The file dbname.sql now holds a backup of your database and is ready for download to your computer.

To export a single table from your database you would use the following command:

mysqldump -p --user=username database_name tableName > tableName.sql

Again you would need to replace the username, database and tableName with the correct information.

Once done the table specified would then be saved to your account as tableName.sql

Import A MySQL Database

The file must be in .sql format. It can not be compressed in a .zip or .tar.gz file.

  1. Start by uploading the .sql file onto the server
  2. If you haven’t already done so, create the MySQL database via the cpanel.
  3. Using SSH, navigate to the directory where your .sql file is.
  4. Next run this command:
     mysql -p -u username database_name < file.sql 

To import a single table into an existing database you would use the following command:

mysql -u username -p -D database_name < tableName.sql