0

How can i run a mysql query from a script via the ubuntu command line using the mysql client? I know i can use the following command:

mysql -uroot -psecret -e "select * ..."

But my query is too big and would like to be read from a script instead. I tried the following and did not work:

mysql -uroot -psecret < /path/to/script.sql

i get the error:

ERROR 1046 (3D000) at line 1: No database selected
2
  • 3
    You need to specify the database name: mysql -uroot -psecret yourDatabaseName < /path/to/script.sql Commented Sep 7, 2016 at 10:05
  • The other way is to add use xxxx; inside of your script.sql Commented May 22, 2017 at 4:47

2 Answers 2

1

Just specify the database name

mysql -D yourdbname -uroot -pYourpassword < /path/to/script.sql

-D, --database=name Database to use.

-p, --password[=name] Password to use when connecting to server. -h, --host=name Connect to host.

Sign up to request clarification or add additional context in comments.

Comments

0

I think this way you should try.

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

1 Comment

I think Tim Biegeleisen solution also works

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.