How do I import a .sql file via the command-line?

If you have enabled SSH access on your hosting package, then you can import a MySQL database easily using command-line tools. This method is quick, reliable and often used for database migrations or backups.

To load a .sql file you have to use the command of MySQL in the terminal.

Basic Import Command

The usual command to import a database is:

mysql -h hostname -u username -p username < databasefile.sql

This command will connect to your MySQL database and will import the SQL file directly into it.

Understanding the command.

In the command above:

Note

You should upload the .sql file first or make sure that it is available in your server directory before running the command.

Best Practice

Before you import, make sure:

If the file is compressed (e.g. .zip or .gz), it should be unzipped before running the import command.

Conclusion

One of the most efficient ways to restore or migrate databases is to import a .sql file using command-line. It offers you direct control, faster execution and is suitable for both small and large databases when used right.