5

I'm trying to run some mysql scripts from the command line. One is autogenerated and it does not select the database before hand. Right now I have two scripts:

mysql -u <user> -p<password> < script1.sql
mysql -u <user> -p<password> < script2.sql

the last line of script1 is USE mydatabase; but when I run script2 it says there is no database selected. Is there a way to specify what database to use for script2?

4 Answers 4

20

add the parameter -D like this

mysql -u<username> -p<password -D<database> < script.sql
Sign up to request clarification or add additional context in comments.

Comments

5
mysql -u <user> -p<password> my_database_name < script2.sql

In your case happens because these are 2 separate processes and the queries from the first are not connected the ones from the second.

Another solution is to put USE database_name as first line in script2.sql

Comments

1

The solution by @Desislav would work (other solution is to add 'use database' command at top of script2.sql), but to clarify your issue

the last line of script1 is USE mydatabase; This would not help for script2, since script2 would be executed in a different process, you have to specify the database again.

Comments

0

Under *nix (Unix, Linux, etc) you can use the cat command to combine the two scripts.

cat script1.sql script2.sql | mysql -u <user> -p<password>

Under DOS/Windows you can a little known feature of the copy command

copy script1.sql+script2.sql | mysql -u <user> -p<password>

Comments

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.