How can I escape backticks in gnu make makefile - mysql

I have this make file:
createuser:
$(MYSQL) -e " grant SELECT, USAGE on `app\_%`.* to 'user.name'#'%' IDENTIFIED BY '$(USER_PASS)'" && \
$(MYSQL) -e "FLUSH PRIVILEGES;"
This command works fine if I execute it directly in a MySQL query window, but I want to execute it from terminal.

You don't need FLUSH PRIVILEGES with GRANT or CREATE USER
statements
For future compatibility - CREATE USER [IF NOT EXISTS]
before doing the GRANT
Within a Makefile you can escape using \
before both backticks.

Related

Running a single mysql command with the 'runuser' command

For some strange reason, I can't find a way to make the runuser command work. I know it is possible to achieve this with sudo -u mysql mysql -e "$DB_SETUP but since I want to do this inside a script that already runs with sudo I find this not very pretty.
Here is what I am trying to do:
DB_SETUP="CREATE USER IF NOT EXISTS $DB_USER#$BASEURL IDENTIFIED BY '$DB_PASSWORD';CREATE DATABASE IF NOT EXISTS $DB_NAME;GRANT ALL PRIVILEGES ON $DB_NAME.* TO $DB_USER#$BASEURL IDENTIFIED BY '$DB_PASSWORD';FLUSH PRIVILEGES;"
sudo runuser -u mysql "mysql -e \"$DB_SETUP\"" # does not work
It gives me this error:
runuser: failed to execute mysql -e "CREATE USER IF NOT EXISTS db_user#baseurl IDENTIFIED BY 'db_password';CREATE DATABASE IF NOT EXISTS db_name;GRANT ALL PRIVILEGES ON db_name.* TO db_user#baseurl IDENTIFIED BY 'password';": No such file or directory
As commented above, I got it working with:
sudo runuser -u mysql mysql <<< $DB_SETUP
No quotation marks at all!

how do i create mysql user and database in bash script with prefix

Is there anyway to do the same with prefix underscore with same username.
For example :-
#!/bin/bash
db_user=devdb
db_pass=`openssl rand -hex 16`
mysql -u root <<-EOF
CREATE USER '$db_user'#'localhost' IDENTIFIED BY '$db_pass';
GRANT ALL PRIVILEGES ON `$db_user\_%` . * TO '$db_user'#'localhost';
FLUSH PRIVILEGES;
EOF
In above script GRANT ALL PRIVILEGES giving error and i want solution for that.
If i am using below command without variables directory in mysql root then no error.
GRANT ALL PRIVILEGES ON `devdb\_%` . * TO 'devdb'#'localhost';
Please reply if anyone have solution to this.
Try using double quotes instead of single quotes for variables.
Refer: Difference between single and double quotes in Bash

How to disable dot replacement in bash?

I want to run MySQL query from bash script to create new user:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser#localhost IDENTIFIED BY 'somepass'"
Where $BUILD variable is set to a number, for example 15, so final query should look like this:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_15.* TO appuser#localhost IDENTIFIED BY 'somepass'"
But... in directory where script is and is run there is a tar archive with same name as database, so query becomes like this:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_15.tar.gz TO appuser#localhost IDENTIFIED BY 'somepass'"
I guess this happens because there is a match, but changing file name is not an option. Moving script to other directory is not allowed too.
I tried to escape dot with backslashes (appdb_${BUILD}\.*) - nothing changed.
How I can fix this?
There is nothing wrong with your approach and it is working in my case. See below. Can you tell us what is the error that you are getting so that we can help you?
[root#cloud mysql]# ls
appdb_15.tar.gz test.sh
[root#cloud mysql]# cat test.sh
#!/bin/bash
BUILD=15
# First Method
mysql -u root -ptest -e "GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser#localhost IDENTIFIED BY 'somepass';"
# Second Method
mysql -u root -ptest <<EOF
GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser1#localhost IDENTIFIED BY 'somepass';
select user, host from mysql.user where user like 'appuser%';
EOF
[root#cloud mysql]# ./test.sh
user host
appuser localhost
appuser1 localhost
EDIT 1:
I forgot to mention that this is definitely not a bash issue.
EDIT 2: Changed the build variable to 15 from 5.

Special chars issue while granting priviliges on mysql database

I came across with an issue while I was trying to grant privileges to a database for a mysql user via t. I think it's because of the special chars in the database name.
This one is working:
/usr/bin/mysql -uroot -pXz5eaCqwvsT0pAr0gsf0tg1a -e "GRANT ALL PRIVILEGES ON dev_52000_nycny.* TO 'wp_j-5-1-5_nycny'#localhost"
That one is not working:
/usr/bin/mysql -uroot -pXz5eaCqwvsT0pAr0gsf0tg1a -e "GRANT ALL PRIVILEGES ON dev_j-5-1-5_nycny.* TO 'wp_j-5-1-5_nycny'#localhost"
I have tried few combinations but no luck:
/usr/bin/mysql -uroot -pXz5eaCqwvsT0pAr0gsf0tg1a -e "GRANT ALL PRIVILEGES ON 'dev_j-5-1-5_nycny'.* TO 'wp_j-5-1-5_nycny'#localhost"
/usr/bin/mysql -uroot -pXz5eaCqwvsT0pAr0gsf0tg1a -e "GRANT ALL PRIVILEGES ON dev_j\-5\-1\-5_nycny.* TO 'wp_j-5-1-5_nycny'#localhost"
/usr/bin/mysql -uroot -pXz5eaCqwvsT0pAr0gsf0tg1a -e "GRANT ALL PRIVILEGES ON 'dev_j\-5\-1\-5_nycny'.* TO 'wp_j-5-1-5_nycny'#localhost"
What should I do ?
MySQL uses backticks to quote database, table, and column names that contain special characters. See When to use single quotes, double quotes, and backticks in MySQL
/usr/bin/mysql -uroot -pXz5eaCqwvsT0pAr0gsf0tg1a -e 'GRANT ALL PRIVILEGES ON `dev_j-5-1-5_nycny`.* TO "wp_j-5-1-5_nycny"#localhost`
Make sure you use single quotes around the -e argument, because backticks have special meaning to the shell when they're inside double quoted strings. I also changed the quotes around the username to double quotes to accomodate using single quotes around the whole query.

How can I insert MySQL in a bash script

I would like to insert MySQL into my bash script.
How can I insert the following lines into bash code?
This is how I would enter MySQL...
mysql -uroot -p
and use this MySQL code...
GRANT SELECT, INSERT, UPDATE, DELETE
ON example.*
TO 'yourUser'#'localhost'
IDENTIFIED BY 'yourUserPassword';
\q
You don't need to use bash to do this, simply use the -e flag:
mysql -uroot -p<password> -e"GRANT SELECT, INSERT, UPDATE, DELETE ON example.* TO 'yourUser'#'localhost' IDENTIFIED BY 'yourUserPassword'"
Notice that you might have to flush the privileges after running the above:
FLUSH PRIVILEGES;
From the documentation:
Reloads the privileges from the grant tables in the mysql database
To do this from a bash script you create a sh file with the following content:
#!/bin/bash
mysql -uroot -p<password> -e"GRANT SELECT, INSERT, UPDATE, DELETE ON example.* TO 'yourUser'#'localhost' IDENTIFIED BY 'yourUserPassword'"
mysql -uroot -p<password> -e"FLUSH PRIVILEGES"
Don't forget that you must set execute permission on the file:
chmod +x <filename>.sh
Then you should be able to execute the script using:
./<filename>.sh