Run sql file in database from terminal

If you want to run an SQL file for a specific database, then select the database first:

mysql -u your_username -p your_password

use db_name

source <path_to_sql_file>/file.sql

I presume that it is MYSQL. To run it from Unix / Linux environment you must do this:

$ mysql -h "server-name" -u "your_username" -p "your_password" "database-name" < "filename.sql"

There is another way:

mysql --host=localhost --user=your_username --password=your_password  -e "filename.sql"

The above answer of @Tomislav is correct but I do not like to write the password with the command-line option.

  1. If you did not create the new database and want to create it with a .SQL file then use the below statement.

    mysql -h {YOUR_HOST} -u {USERNAME} -p < {FILENAME.sql}

then MySQL CLI will ask for the password (The best way to prevent the password from anyone). Write your password and hit Enter

  1. If you have already created a database then use the below command.

    mysql -h {YOUR_HOST} -u {USERNAME} -p {DATABASE_NAME} < {FILENAME.sql}

then MySQL CLI will ask for the password. Write your password and hit Enter

Usually, the .SQL file doesn't have any print statements. So you have to enter and check with your MySQL CLI.


Try this:

mysql -u your_username -p your_password

use db_name

source <path_to_sql_file>/file.sql