How to import SQL dump file to database via SSH

Post Reply
User avatar
Site Admin
Posts: 748
Joined: Fri Jun 19, 2020 9:59 am

As root, run:

Code: Select all

mysql user_database_name < dump_of_database.sql
That's it!

Before importing, check if your SQL dump contains 'CREATE DATABASE' :

Code: Select all

grep 'CREATE DATABASE' dump_of_database.sql
In case that 'grep' command gave you any output, then edit your SQL file and delete the whole SQL statement:

Code: Select all

and also delete statements that begin with

Code: Select all

USE ...

Before importing the SQL dump file, please check if it contains a dump of 'mysql' database (at the end or the top of SQL dump file).
The easiest way to check if it contains 'DROP DATABASE' is:

Code: Select all

grep 'DROP DATABASE' dump_of_database.sql
(first grep command for 'CREATE DATABASE' would probably be enough, because it will give you a hint that SQL dump would try to do something with 'mysql' database)

If you see something like "CREATE DATABASE `mysql`" or "DROP DATABASE `mysql`" then you must delete whole that part (related to 'mysql' database) from your SQL dump file.
Otherwise, you will rewrite your own 'mysql' database, which would lead to losing all your mysql users - which means that all databases on the server will instantly become unavailable.

Post Reply