Passing variable in MYSQL select statement - mysql

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

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

How to pass user defined variables to mysql command

How do I pass variables that I would have created in linux shell to the mysql command.
For example:
# carname=tesla-new
# echo $carname
I want to use the $car-name variable when passing arguments to MySQL commands.
This variable will come in as input from loop while iterating through a list of values.
mysql -u wp-supportuser -h xx.xxx.xxx.xxx -p<mypassword>! -e 'DROP DATABASE ${carname};'
I need the hyphon(-) in the variable's value, else my application will accept. We get the below error when the above command is run:
mysql: > [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 '-wp' at line 1
How will I be able to achieve this?
It is a MySQL error, because MySQL does not support the usage of hyphen by itself. Instead, you need to enclose it into
`
characters. You can use it if you wrap your command inside apostrophes, like:
mysql -u wp-supportuser -h xx.xxx.xxx.xxx -p<mypassword> -e 'drop database `$carname`;

Syntax error in mysql statement used in shell script

I am currently working on a shell script.
I want to modify the contains of the column "balance" in "Student" table as "balance=balance-$deduct" in shell script.
I am executing following line in shell
mysql -h "$host" -u "$user" --password="$pass" --database="$db" -e "UPDATE student SET balance=balance-$deduct WHERE pid=$pid";
Here i specified the host, username and password properly and i have one numeric value stored in $deduct
but when i execute the script i am getting following error:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE pid=132054' at line 1
where I am wrong here?
thanks
You don't need --database=
mysql -h "$host" --user="$user" --password="$pass" "$db" -e "UPDATE student SET balance=balance-$deduct WHERE pid=$pid";
You can also do Use $db in you query
Just remove --database=
It should work.

MySQL Set Password Via Command-Line Variable

I need a one-liner to provision users in my databases. I'm running MySQL 5.6. Here is what I'm setting the password as a variable so that I can pass it dynamically (obviously it won't always be 'password').
mysql_password="password"
mysql -u ted -e "SET PASSWORD FOR 'ted'#'localhost' = PASSWORD($mysql_password);"
I'm getting the following error when I run this:
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 'password)' at line 1
What is wrong with the statement? Can it be corrected or is another solution available so I accomplish this via Bash?
You need to surround the variable by single quotes:
mysql -u root -e "SET PASSWORD FOR 'ted'#'localhost' = PASSWORD('$mysql_password');"

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'\""