Introduction
When moving your website to a new hosting provider, the migration process involves more than simply transferring your website files. You also need to migrate the database associated with your website. While transferring website files is usually straightforward, moving a database can become more challenging, especially when dealing with a large database.
When attempting to import a large database using phpMyAdmin, you may encounter limitations related to the maximum file upload size or server configuration settings. These restrictions can cause the import process to fail or stop before it is completed. In such cases, you need an alternative method that allows you to work with the database directly on the server without relying on the phpMyAdmin interface.
This is where SSH comes in. SSH provides a more flexible and efficient way to manage large databases, allowing you to perform the import directly on the server using a single command. The server reads the database file and imports its contents directly into the database, eliminating the need to use phpMyAdmin.
SSH is not limited to database imports. You can also use it to export a database just as easily. By running the appropriate export command, the server creates a backup of your database and saves it as a file that you can transfer or use later. This makes working with large databases faster and easier while providing website owners and developers with an efficient way to manage databases and migrate them between different servers and hosting providers.
If you are unfamiliar with SSH or have never used it to import and export databases, don't worry. In this article, we will explain how to import and export a database using SSH step by step. We will start by explaining what SSH is, how it works, and how to connect to your hosting server. Then, we will walk you through the commands required to import and export your database and show you how to verify that the process was completed successfully.
How Does SSH Work?
To understand how SSH works, let's start with what happens when you request a connection to a remote server using an SSH client. You typically begin by running a connection command that includes the username and server address, such as:
ssh username@server-ip
When you execute this command, your device attempts to establish a connection with the SSH service running on the remote server. The server then verifies your identity using the configured authentication method, such as a password or an SSH key.
Once authentication is successful, you are granted access to the server and an SSH session is established. Through this session, you can interact directly with the server and perform various tasks, such as managing files and directories, compressing and extracting files, working with databases, and running or managing services.
In other words, SSH provides a secure way to control a remote server from your own device. You enter commands on your computer, but those commands are executed on the remote server, and the results are sent back to you through an encrypted and secure connection.
When you finish your work, you can simply terminate the SSH session. Any changes you made during the session remain saved on the server.
Now that you have a clear understanding of how SSH works, let's move on to the next step and learn how to connect to your hosting server using SSH step by step.
How to Connect to Your Server via SSH
Now that we have introduced SSH, understood its importance, and learned how it works, it is time to move on to the practical side and learn how to connect to your hosting server using SSH.
1. Enable SSH on Your Hosting Account
SSH may be disabled by default on some hosting accounts for security reasons. Therefore, you need to enable it first through your hosting account or the hosting provider's control panel, as shown in the following image.
2. Get Your SSH Connection Details
After enabling SSH, you will need the connection details provided by your hosting provider. These usually include the Hostname or IP Address, Username, Port, and Password if the server uses password-based authentication.
For Hostinger users, SSH connection details can be found on the SSH page in the hosting control panel. There, you will find the required connection information, such as the username, server address, and port, as shown in the following image.
3. Connect to the Server Using SSH
Once you have your SSH connection details ready, you can proceed to connect to your hosting server via SSH.
If you are using Windows 11, you usually do not need to install any additional software. You can use Command Prompt (CMD) to run SSH commands. On Linux, you can use the Terminal to establish an SSH connection.
After opening the appropriate command-line interface for your operating system, you can connect to your hosting server using the following SSH command:
ssh username@server-ip
Replace username with your actual username and server-ip with your server's IP address. For example:
If your server uses a port other than the default SSH port, you can specify the port number by using the -p option:
ssh -p 2222 username@server-ip
After running the command, SSH will attempt to establish a connection with the server. When connecting to the server for the first time, you may see a message asking you to verify the server's identity. After confirming it, you will be prompted to enter your password or use the authentication method configured on the server.
Once the SSH connection has been established successfully, you are ready to move on to the next step: importing your database using SSH.
How to Import a Database Using SSH
Now that we have covered what SSH is and how to connect to your hosting server, it is time to move on to the practical side and learn how to import a database using SSH.
First: Create a New Database
Before starting the import process, you need to create a new database on the hosting server where you want to migrate your website.
After creating the database, keep the database connection details handy, as you will need them later when running the import command:
- Database Name
- Database Username
- Database Password
Make sure the database credentials are correct and that the database user has the necessary permissions to access and modify the database.
Second: Upload the Database File to the Server
After creating the database, upload the previously exported database file in SQL format to your hosting server.
You can upload the file using any available method, such as FTP or the File Manager provided by your hosting control panel. It is recommended to upload the database file to a directory associated with your website so that you can easily access it through your SSH connection.
Third: Navigate to the Database File Location
After uploading the database file, navigate to the directory containing your database.sql file.
To find out which directory you are currently in, you can use the following command:
pwd
The pwd command displays the full path of your current working directory.
Next, use the cd command to navigate to the directory where you uploaded the database file. For example:
cd /path/to/database
To make sure the file exists in the correct directory, use the ls command:
ls
If database.sql appears in the list of files, you are in the correct directory and can proceed with the database import.
Fourth: Run the Database Import Command
Once you have confirmed that the database file is located in the correct directory, you can start the import process using the following command:
mysql -u username -p mydatabase < database.sql
In this command:
usernameis the database username.mydatabaseis the name of the database you created on the new hosting server.-ptells MySQL to prompt you for the database password.<is a redirection operator that passes the contents of the SQL file to themysqlcommand so they can be imported into the database.database.sqlis the name of the database file you uploaded to the server.
If the connection details are correct and the SQL file is valid, MySQL will begin executing the commands contained in the file and importing the database contents into the new database.
Note: If no error message appears after running the command, the import has most likely completed successfully. You can then verify the database using phpMyAdmin or MySQL commands to make sure that the expected tables and data are present.
Using this method, you can import large databases directly through SSH without having to upload the SQL file through phpMyAdmin. This can help you bypass file-size limitations imposed by traditional web-based database import interfaces.
Now that we have covered how to import a database using SSH, it is time to move on to the other side of the process: exporting the database.
In this context, exporting means creating a copy of the existing database and saving it as an SQL file. You can then keep this file as a backup or use it to migrate the database to another hosting provider or server when needed.
How to Export a Database Using SSH
Exporting a database via SSH is very similar to the import process we covered earlier. However, instead of importing an SQL file into a database, we will create an SQL file containing a copy of the existing database.
You can then keep this file as a backup or use it to migrate the database to another hosting provider or server.
To export the database, we will use mysqldump, a command-line utility designed for MySQL and MariaDB databases. It creates a database dump and saves it as a file that can be transferred, stored as a backup, or imported into another database later.
Below are the steps for exporting a database using SSH.
First: Check Your Database Credentials
Before starting the export process, you will need the credentials for the database you want to export:
- Database Name
- Database Username
- Database Password
Make sure the database user has the necessary permissions to access and export the database.
Second: Navigate to the Directory Where You Want to Save the Backup
Since the export process will create an SQL file on the server, it is best to determine where you want to save the database backup.
You can check your current working directory using:
pwd
To view the files and directories within your current location, use:
ls
If you want to save the database file inside a specific directory, you can navigate to it using the cd command. For example:
cd /path/to/backup
Third: Run the Database Export Command
Once you have confirmed that you are in the directory where you want to save the database file, you can run the following command to export the database:
mysqldump -u username -p mydatabase > database.sql
In this command:
mysqldumpis the command used to export a MySQL database and create a database dump.-u usernamespecifies the database username.-ptells the system to prompt you for the database password.mydatabaseis the name of the database you want to export.>redirects the output of the export process to a file.database.sqlis the name of the file where the database dump will be saved.
For example, if the database name is mydatabase and the database username is admin, you can use:
mysqldump -u admin -p mydatabase > database.sql
After running the command, the system will prompt you to enter the database user's password. Enter the password and press Enter to begin the export process.
Once the command finishes, the exported database will be saved as database.sql in the directory you selected. You can then download the file, keep it as a backup, or transfer it to another server for a future database migration.
Fourth: Verify That the Database File Was Created
After the export process is complete, you can use the ls command to display the files in the current directory:
ls
If you see the database.sql file in the list, this means that the database backup was created and saved successfully.
You can then download the file to your computer using FTP or File Manager, or transfer it directly to another server if you are using it to migrate your website.
Note: If the database is large, creating the SQL file may take some time. The required time depends on the size of the database and the server's performance. Therefore, do not close your SSH session while the export process is running.
With this method, you can create a complete copy of your database directly on the server using SSH without having to export it through phpMyAdmin. This makes SSH a practical and efficient solution when working with large databases.
Essential SSH Commands You Should Know
Now that we have covered how to import and export a database using SSH, it is time to look at some of the most important basic SSH commands you may need when working with your server.
These commands allow you to navigate between directories, view files, check your current location, and perform various tasks easily from the command line.
| Command | Usage | Example |
|---|---|---|
pwd |
Displays the current working directory. | pwd |
ls |
Lists the files and directories in the current directory. | ls |
cd |
Navigates between directories. | cd public_html |
mkdir |
Creates a new directory. | mkdir backups |
touch |
Creates a new file. | touch example.txt |
cp |
Copies files and directories. | cp config.php config-backup.php |
mv |
Moves or renames files and directories. | mv example.txt new-example.txt |
rm |
Deletes files and directories. | rm example.txt |
cat |
Displays the contents of a file in the terminal. | cat config.txt |
clear |
Clears the commands and output displayed on the screen. | clear |
history |
Displays previously executed commands. | history |
find |
Searches for files and directories by name or location. | find . -name 'config.php' |
du |
Shows the disk space used by files and directories. | du -sh public_html |
df |
Displays the available and used storage space on the server. | df -h |
Essential Database Commands for SSH
Now that we have covered the most important basic SSH commands, let's move on to the commands most closely related to the main topic of this article: managing databases through SSH.
These commands can be used to import and export databases, create backups, connect to database systems, execute queries, and manage database data directly from the command line.
| Command | Usage | Example |
|---|---|---|
mysql |
Imports MySQL and MariaDB databases and executes SQL queries. | mysql -u username -p database_name < backup.sql |
mysqldump |
Exports MySQL and MariaDB databases and creates database backups. | mysqldump -u username -p database_name > backup.sql |
psql |
Connects to PostgreSQL databases, executes queries, and imports SQL files. | psql -U username -d database_name -f backup.sql |
pg_dump |
Exports PostgreSQL databases and creates database backups. | pg_dump database_name > backup.sql |
mongodump |
Creates a backup of MongoDB databases. | mongodump --db database_name --out backup |
mongorestore |
Restores MongoDB databases from a backup. | mongorestore --db database_name backup/database_name |
These commands give you greater control over your server and databases, allowing you to perform many common database management tasks without relying on graphical tools such as phpMyAdmin.
How to End an SSH Connection
After completing the required tasks on your server, it is recommended to end your SSH session rather than leaving the connection open unnecessarily.
You can easily terminate the SSH session by using the exit command:
exit
Running this command closes the current SSH session and returns you to the command line on your local device.
Ending the SSH session does not shut down the server or undo any of the operations you have performed. It simply closes the SSH connection between your device and the remote server.
For additional security, if your hosting account allows you to control the SSH service, it is a good practice to disable SSH after you finish using it if you do not need continuous SSH access. This can help reduce unnecessary access to your server.
Conclusion
With that, we have reached the end of this guide. Throughout the article, we covered how to use SSH to manage databases, starting with enabling SSH and connecting to your hosting server, then moving through the most important basic commands, and finally learning how to import and export databases directly from the command line.
We also covered how to use commands such as mysql and mysqldump to work with MySQL databases, as well as how to navigate files and directories and verify that your database file exists before starting an import or export process.
We hope this guide has helped you understand how to manage databases using SSH and that you can now perform database imports and exports and manage your databases more easily and confidently.

