Bash script unable to send instructions to mysql - mysql

This is part of a Bash script, and the inside looks like this:
createdbcmd="docker exec $1 mysql -v -uroot -e \"GRANT ALL PRIVILEGES ON $dbname.* TO $2#$4 IDENTIFIED BY '$3'\""
echo $createdbcmd
$createdbcmd
the echo looks like this:
docker exec mysql_test_no mysql -v -uroot -e "GRANT ALL PRIVILEGES ON wordpress.* TO user#172.17.0.63 IDENTIFIED BY 'changeme'"
Running this exact command works just fine. via the bash-script it acts as if i'm just running 'mysql'
and i get the default help information with no error.

Try this way:
createdbcmd="docker exec $1 mysql -v -uroot -e \"GRANT ALL PRIVILEGES ON $dbname.* TO $2#$4 IDENTIFIED BY '$3'\""
echo $createdbcmd
eval $createdbcmd
Without the eval, the quoted expression is not interpreted correctly.

Related

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.

need to "build" a mysql command and exec it in a shell script

I'm new to writing shell scripts.
I am attempting to create a database using a shell script. Here's the script:
#!/bin/bash
#create a new db
a="mysql -uuser -ppassword -e'create database $1;'"
exec $a
The command exec mysql -uuser -ppassword -e'create database databaseName;' works in a shell, but when I sh the script, I get the mysql help open...
I think the problem is in the quotes, the simple quote prevent the variable expansion.
You can simply do like this in your script:
#!/bin/bash
#create a new db
mysql -u user -p password -e "create database $1;"
Or you can try to place all your mysql commands in a file, let's say "dbname.sql".
And do this:
#!/bin/bash
#create a new db
mysql -u user -p password "$1" < "$1.sql"
if you like use exec to run commands this can be a possible solution
#!/bin/bash
#create a new db
programm="mysql"
parameter[0]="-ppassword"
parameter[1]="-uuser"
parameter[2]="-ecreate database $1;"
exec "$programm" "${parameter[#]}"
exec parameter are
exec [-a NAME] [-cl] [COMMAND] [ARG...] [REDIRECTION...]
command is $programm and the array parameter is the argument list.
Sounds like you need to use the 'cat' command and a pipe instead of using 'exec' .
cat /path/to/my/file | mysql -h localhost -u root -padmin

How does MySQL take variables in bash?

Making a script to print out data from a MySQL db via bash, I met the following problem:
While I try to log in, it uses the password as the database to log in to.
Script is like this:
#!/bin/bash
echo $1
db=$1
pasx=$2
CMD="use $db; select * from job_log;"
mysql -u sqluser -p "${pasx}" -e "$CMD"
If I'm going to run the script with the command
User#server:/path/with/file$ sh sql.sh ok hobo
MySQL returns the following:
User#server:/path/with/file$ sh sql.sh ok hobo
ok
Enter password: ERROR 1049 (42000): Unknown database 'hobo'
I might have fully misunderstood something, but I can't put my finger on what it might be.
You need to remove the space after the -p parameter. See the mysql man page. You also need to specify the database in the command (remove it from the query)
mysql -u sqluser -p$pasx -e "$CMD" $db
Or maybe more clear:
mysql --user=sqluser --password=$pasx --execute="$CMD" $db
Try this:
mysql -u sqluser --password=${pasx} -e "$CMD" $db

How to grant MySQL privileges in a bash script?

I need to grant privileges to a database from within a bash script. Some parameters are in the form of variables, say, the username and the password. The command in a mysql shell would look like this:
GRANT ALL ON *.* TO "$user"#localhost IDENTIFIED BY "$password";
...Except that $user and $password would be replaced by their values.
Is there a way to perform such a command from within a bash script?
Thank you!
There you go :)
#!/bin/bash
MYSQL=`which mysql`
EXPECTED_ARGS=3
Q1="USE $1;"
Q2="GRANT ALL ON *.* TO '$1'#'localhost' IDENTIFIED BY '$2';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: $0 dbname dbuser dbpass"
exit $E_BADARGS
fi
$MYSQL -uroot -p -e "$SQL"
If we donĀ“t know the password we can get it with:
cat /etc/psa/.psa.shadow
So we can get into mysql without prompt password like:
mysql -uadmin -p`cat /etc/psa/.psa.shadow`

mysql create database and user script

Question Rewritten:
HOMEDIR="ftpuser"
REMOTEIP="1.1.1.1"
MYSQLPASS="password"
Q1="DROP USER "$HOMEDIR"_shop;"
Q2="DROP DATABASE "$HOMEDIR"_shop;"
Q3="CREATE DATABASE IF NOT EXISTS "$HOMEDIR"_shop;"
Q4="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'#'localhost' IDENTIFIED BY '$MYSQLPASS';"
Q5="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'#'anotherip' IDENTIFIED BY '$MYSQLPASS';"
# Need to grant permissions from another server as well
Q6="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}${Q4}${Q5}${Q6}"
echo $SQL
echo " "
ssh -p 8899 root#$REMOTEIP "mysql -u root -p "$SQL""
I then run:
/root/testing/migratesite.sh
And get:
bash: DROP: command not found
bash: CREATE: command not found
bash: GRANT: command not found
bash: GRANT: command not found
bash: FLUSH: command not found
What am I missing?
You are missing quotes and a proper mysql client command line:
ssh -p 8899 root#$REMOTEIP "mysql -u root -p -e \"$SQL\""
You need to escape the quotes around the $SQL variable so they get passed to the remote shell, else they get interpreted by the local shell (that's why you get DROP: command not found, the semi colon is interpreted by the shell.) Also, to have the mysql client to execute a command you have to pass the -e command line option.
Did you try this:
ssh -p 8899 root#$REMOTEIP "echo \"$SQL\" | mysql -u root --password=$SQL_PASS"