Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql script no database selected

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?

like image 940
Jeff Storey Avatar asked Nov 29 '22 02:11

Jeff Storey


2 Answers

add the parameter -D like this

mysql -u<username> -p<password -D<database> < script.sql
like image 172
Matei Suica Avatar answered Dec 05 '22 18:12

Matei Suica


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

like image 32
Desislav Kamenov Avatar answered Dec 05 '22 18:12

Desislav Kamenov