45

I have a server and within that server I have an sql script in the /var/www/ directory. That script is to create tables and databases. I need to know how to execute this sql script from terminal or some client while keeping the file on the server.

PS- I don't know much of the terminology for this topic so I will look into other suggestions for my approach.

0

5 Answers 5

50

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"
Sign up to request clarification or add additional context in comments.

6 Comments

What if my sql script needs to create a database? How, then, would I have a database name to use for that command line?
Try this: mysql -uyour_username -pyour_password < filename.sql
Second way does not work for me, the argument to -e is a valid SQL statement, not a path. E.g.: mysql --host=127.0.0.1 --port=3306 --user=root --password=your_password -e "select 1;"
My MySQL doesn't respond to $ mysql -h "server-name" -u "your_username" -p "your_password" "database-name" < "filename.sql", do you have any ideas?
@Tomislav I agree with your answer but writing password in the command line together is a bad idea. If you leave just -p and not writing the password then the system will ask you to enter the password.
|
41

Try this:

mysql -u your_username -p your_password

use db_name

source <path_to_sql_file>/file.sql

1 Comment

I selected a databse before "source" command: " use <databe_name>". And then "source <path_to_sql_file>/file.sql"
7

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

Comments

1

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.

Comments

0
  1. create a new database.
  2. then use the below command

mysql -u {username} -p {databasename}

  1. enter password

  2. then add file path or filename

\. { filename } | source { filename }

file must have .sql extension

1 Comment

Can you explain how this answer is different than the other existing answers? The accepted answer already tells them to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.