I have a bash that creates a database, assign user rigths to it and then load a dump. I do this from a bash in opensuse. A parameter with database name is used = $1.
This is the part of my script that handles this:
echo "creating database"
#(This is Line 22)create new database and configure grants
mysql -hlocalhost -uroot -e "create database $1;"
echo "select db, use"
mysql -hlocalhost -uroot -e "use $1;"
echo "granting rights"
mysql -hlocalhost -uroot -e "grant all on $1.* to 'smic_db_root'@'localhost' identified by
'sm_for_all_987';"
echo "loading dump"
#load the database dump
mysql -h localhost -uroot $1</opt/otrsadm/otrs_template.sql
And this is from my error log:
creating database
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
select db, use
ERROR at line 1: USE must be followed by a database name
granting rights
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* to 'smic_db_root'@'localhost' identified by
'sm_for_all_987'' at line 1
loading dump
ERROR 1046 (3D000) at line 22: No database selected
How do I do in order to chose the database in the bash and when do I need to do it. Since the error message is refering to Line 22, I need to do it somewhere inside. I tried adding the use $1; like this:
mysql -hlocalhost -uroot -e "create database $1; **use $1;** grant all on $1.* to 'smic_db_root'@'localhost' identified by 'sm_for_all_987';"
No difference in behaviour.
create/grant commands don’t need a database to be selected. They explicitly work at the global level (create) or on the mysql database (grant). Try putting in some debugging to see eactly where the error(s) occuring?
Right now the error messages do not tell you WHAT failed, just that something did. With the echoes added as above, you’ll at least know which stage caused which error(s).