Issues with mysql command in bash script [duplicate] - mysql

This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
Closed 3 years ago.
I'm really struggling with a bash command, trying to launch a mysql query :
output=$(mysql --user=root --password="$password" -s --execute 'grant all privileges on zabbix.* to zabbix#localhost identified by "$zabbix_pwd";')
The statement above does not work, because $zabbix_pwd is not take as a variable, as it should...
Any idea on how to rewrite this statement would be very helpful.
Cheers,
Raj.

Your escaping is wrong. Please try this :
output=$(mysql --user=root --password="$password" -s --execute "grant all privileges on zabbix.* to zabbix#localhost identified by \"$zabbix_pwd\";")
Hope it helps.

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!

Bash mysql not being executed [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 1 year ago.
I'm constructing a Bash script to execute a MySQL query. The Bash scrip is very simple, but the query is not being executed correctly. MySQL responds like a do a mysql usage (help of commands). What am I doing wrong?
The bash file is:
COMANDO='mysql -h 148.72.64.68 -p******** -u root db_vias_ue -e "select count(*) from clientes"'
$COMANDO
Bash quoting rules are seriously weird. In what you wrote, the expression within double quotes actually gets separated into multiple arguments.
Try this:
COMANDO='mysql -h 148.72.64.68 -p******** -u root db_vias_ue -e "select count(*) from clientes"'
bash -c "$COMANDO"

Read user input and send it to MySQL [duplicate]

This question already has answers here:
How to feed mysql queries from bash
(7 answers)
Closed 5 years ago.
So I am attempting to make my first Linux script and its just to setup a MySQL user and I'm trying to do that all with a script, but I can not seem to find how I can send the MySQL command from the script. This is what I have right now.
#!/bin/bash
read -p "Please enter your desired MySQL username: " USER
echo $USER
read -p "Please enter your desired MySQL password: " PASS
echo $PASS
mysql CREATE USER $USER#'localhost' IDENTIFIED BY $PASS;
any help would be appreciated
Thanks
-Jamie
You cannot mixup mysql queries in bash script.So, you have to save the mysql queries to a mysql file i.e .sql file and then run the mysql command
To append the create user line to a .sql file use
echo 'mysql CREATE USER $USER#'localhost' IDENTIFIED BY $PASS' >> filename.sql
Now the .sql file will contain the sql queries and you can run
$ mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql"

MySQL prompts for the password when using heredoc [duplicate]

This question already has answers here:
How to provide password to a command that prompts for one in bash?
(8 answers)
Closed 8 years ago.
I am trying to execute a shell script to login to mysql as root and execute some commands, and in order to avoid putting the root's password in the command line, I am using heredoc format as shown below.
However, mysql prompts me for the password despite the fact that I am giving it the right password.
Exactly the same syntax works perfect on some other hosts but not here.
Why?
mysql#myhost:MySQL> mysql -uroot -p -s -P3306 -e 'SELECT NOW();' <<EOF
> MyPassword
> EOF
Enter password: <---- why does it require manual password entry in here?
2014-12-23 14:57:25
Assuming you use a BASH or KSH command line interface:
read -s pwd;mysql -uroot --password=${pwd} -s -P3306 -e 'SELECT NOW();'
For the read command, you should use the -s option because, as it happens with using heredoc, this solution would also display the password as it is being typed, if you wouldn't use said option.
This solution works because the mysql client obfuscates the password so it does not show in the output from process-listing commands, like ps. Run this:
read -s pwd;mysql -uroot --password=${pwd} -s -P3306
Then, if you open an additional command line interface and execute ps, you will see that it does not show the password, but and obfuscated version of it, in the process list.
This was a famous bug of the mysql client, back in the day, but was fixed in 2002.
Using read -s to put the password in a variable has the added benefit of not showing the password in the command-line interface's history.

BASH script not reading input mysql password [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to execute Mysql command from a shell script?
I'm trying to execute a .sql file using a bash script. But I am having a problem connecting to MySQL. Here's what I have so far:
#! /bin/sh
PWD="thepassword"
mysql -p -u theuser < Randomsqlfile.sql
echo $PWD
When the password is prompted, nothing fills out.
Make this:
mysql -utheuser -pthepassword <Randomsqlfile.sql