How to import a database over SSH

November 5, 2024 / Security and Backup

How to Import a Database Over SSH

Importing a database over SSH is faster and more reliable for large files. It bypasses web-based upload size limits and gives you secure, direct control over the process, which makes it ideal for large or sensitive databases.

If the phpMyAdmin import limit on your server (this varies by host) is too small for your database, you can import it through SSH with these steps.

1. Preparation

1. Upload the database file: Use FTP, SFTP or the file manager to upload the database file to your home directory. Do not leave it in “public_html”, because files there can be downloaded by anyone.
2. Prepare the database: Create a new database, or empty an existing one, for the import. Make sure a database user is assigned to it with full privileges.
3. Verify the database password: If you are unsure of the password, reset it to make sure it is correct.

 

2. Connect through SSH

1. Log in to your hosting account through SSH.
2. Go to the folder where you uploaded the file. For your home directory, run:

cd ~

3. Confirm the backup file is there by running:

ls

If the file is missing, check where you uploaded it.

 

3. Import the database

1. Extract the file if it is compressed (for example, a .sql.gz file), run:

gzip -d file_name.sql.gz

Replace file_name with the actual name of your backup file. This creates file_name.sql.

2. Run the import command:

mysql -u database_username -p database_name < file_name.sql

For example:

mysql -u u123456789_admin -p u123456789_database < database.sql

You will be asked for your database password. Type it (it will not be shown on screen) and press Enter. The import starts. When it finishes, you will see the command prompt again (for example, -bash-4.2$) with no error message.

Note: Import time depends on the size of the database.

3. Delete the backup file once the import is complete, so that it does not stay on the server:

rm file_name.sql

 

4. Update the configuration file

To make sure your website connects to the new database:

1. Open your website’s configuration file.
2. Update the database name, username, password and host as needed. The exact steps depend on your CMS, so check your CMS documentation if necessary.

This is how you import a database over SSH. If you have any difficulty, please contact our support team.

Learn more: How to Import a New File in cPanel File Manager

Spread the love