MySQL query inside ssh, inside bash, with arguments - mysql

I'm writing a small bash script to get a random sample of a database table, connecting through ssh.
ssh $SERVER 'mysql -e "SELECT * FROM ${TABLE} WHERE RAND() < ${PROBABILITY} LIMIT ${LIMIT}" -uroot -p ${DATABASE} > temp_dump_file.sql'
I can't get the arguments to be interpreted. The error is:
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 'WHERE RAND() < LIMIT' at line 1
Thanks in advance :)

As noted by #l0b0, the reason it's failing is because you're using single quotes, so ${TABLE} etc won't be expanded properly. Try swapping your use of quotes:
ssh $SERVER "mysql -e 'SELECT * FROM ${TABLE} WHERE RAND() < ${PROBABILITY} LIMIT ${LIMIT}' -uroot -p ${DATABASE} > temp_dump_file.sql"

Bash won't expand variables inside single quotes.

Related

Running mysql commands inside bash script

I am trying to execute a mysql command inside a bash script but every time I try to execute it fails for some reason. I have tried several ways and none seemed to work(e.g: <<QUERY...;QUERY)
My select is the following but I get an error:
#!/bin/bash
mysql -utesting -pMypass -hlocalhost -D test DB -e "
SELECT value FROM h6_options
where module=cloud
AND `option`=prefix;"
I get the following error.
ERROR 1064 (42000) at line 3: 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 '='prefix'' at line 5
Any help is appreciated.
You are using a multi-line SQL statement. this means you have two options:
you can push everything into one single line.
try reformatting your script to use EOF tags instead.
any option I didn't think of that the smart people here can consider...
here's an example:
mysql -u USER -pPASSWORD <<EOF
SQL_QUERY 1
SQL_QUERY 2
SQL_QUERY N
EOF
so for you, I would do this:
mysql -utesting -pMypass -hlocalhost -D test DB <<EOF
SELECT value FROM h6_options
where module=cloud
AND `option`='prefix';
EOF
Note: I don't like using the word EOF....for me, I like the word SQL or QUERY. because EOF means end of file. The code is identical, so you can use the word of choice:
mysql -utesting -pMypass -hlocalhost -D test DB <<QUERY
SELECT `value` FROM `h6_options`
where `module`='cloud'
AND `option`='prefix';
QUERY
Source: https://www.shellhacks.com/mysql-run-query-bash-script-linux-command-line/
Turns out the issue was the backticks. I had to escape them in order to not evaluate the line.
<<QUERY
SELECT value FROM h6_options
WHERE \`option\`="prefix"
AND module="cloud"
QUERY

Need to connect database mysql from shell script (linux centos7)

#!/bin/bash
date +'%F %T'
echo "Show Database"
mysql -u [user] -p[password] -e 'SHOW DATABASES,USE eventime,SELECT * FROM dt;'
output :
2016-09-22 16:01:33
Show Database
Warning: Using a password on the command line interface can be insecure.
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 'USE eventime,SELECT * FROM dt' at line 1
I don't know why not select data
need to connect and select data
and delete data eveytime 90 day ago
thx for help.
Try semicolon instead of comma in command -
mysql -u [user] -p[password] -e 'SHOW DATABASES;USE eventime;SELECT * FROM dt;'

Update MYSQL table via BASH script give syntax error

I'm trying to run an update query on a db server via bash command.
I have to update an IP field (which is sorted as a string) but i'm getting a syntax error...
ssh admin#192.168.3.240 "/usr/local/mysql/bin/mysql -D SMARTPARK -u parkuser -ppass -e 'update client SET online=0 where client_ip='192.168.42.11''"
I'm getting as error
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 '.42.11' at line 1
which is the error?
Try to escape " character with \" to make sure you don't escape from the string you're sending to your DB.
In other words, try to put the following in the bash file you're executing:
ssh admin#192.168.3.240 "/usr/local/mysql/bin/mysql -D SMARTPARK -u parkuser -ppass -e \"update client SET online=0 where client_ip='192.168.42.11'\""

How to execute a "INSERT INTO" query to a remote database with linux command line?

I'm tryng to create a code in raspbian system to update a value in my database mySQL stored into my hosting server using a bash ".sh" scrypt.
this line won't to work but I don't know why...
mysql -h eliuslab.com -u user -pPassWord -D mydatabase -e "INSERT INTO 'home-IP' ('IP') VALUES ('192.168.1.2')"
And the output:
'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 ''home-IP' ('IP') VALUES ('192.168.1.2')' at line 1 '
this query works well in a GUI mySQL client, but what I want to do is to launch this action with my ".sh" bash scrypt.
Thank you so much :)
Okay I got the solution. I ran your script in my mysql database and got the same error. It's not the fault of script but it's a parsing error.
The correct command is
mysql -h eliuslab.com -u user -pPassWord -D mydatabase -e "INSERT INTO tablename (column) VALUES ('BLABLASTRING')"
Notice there is no need of using quotation marks around the table name and the column name. The quotation marks treat them as string constant rather than a container.

Passing variable in MYSQL select statement

I have a trouble with my bash script here, the mysql select to be more specific. It runs fine without a variable, but when I am declaring variable the script fails, please advise:
#!/bin/bash
table="user_vpn_account"
1="2" # when I introduce this variable script fails , if I will use value "2" directly script works
pwd="password"
D="database"
mysql -uroot -p$pwd -D$D -s -N -e "SELECT port FROM user_vpn_account where user_vpn_id=$1;"
/usr/bin/mysql -uroot -p$pwd -D$D<< eof
eof
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
Thanks in advance.
You can put $1 parameter into a variable, like this:
variable=$1
and then use it like $variable.
Bash Guide - Variables