How to Import a SQL File into a MySQL Database

The ability to easily import SQL files into Database is a key task for anyone running a stack based web application which includes managed WordPress hosting. The ability to perform this task enables easy database restoration in the event of failure for a relatively low time cost. In most cases databases can be imported directly from tools like phpMyAdmin, but in some instances it is not able to import databases via its interface. This is usually due to exceeding the set file limit and will require manual intervention with the command line, learn how to access and import SQL files into your MySQL or MariaDB database.

Import SQL with CLI

1. First off, you’ll need to have the SQL file you want to import ready within your “accessible area” this could be in your /home/user/ directory or at a specific destination (for example /home/user/public_html).

2. First start off by opening your SSH client, If you don’t already have SSH configured please refer to the guide linked above. Using the window connect & login to the shared hosting server using the hostname or IP address, your username and configured SSH key.

PuTTY Connection
PuTTY Connection

3. Afterwards, to make the the import more straightforward using the cd command switch to the directory with the SQL file. An example command is shown below.

cd public_html

4. Now log into MySQL and simultaneously import the SQL file into your existing database with the following command. Replace database to match your database and mysql_file.sql to the name of your SQL file. When prompted enter your password, this is the database user password set in cPanel or Plesk.

mysql -u username -p database < mysql_file.sql