Use variable in mysql CL - mysql

Is it possible to use shell variable in a mysql query via command line?
What I want to do is :
$ var='test'
$ mysql -e 'INSERT INTO table (text) VALUE ($var);' database
It didn't seems to work, even when I use ${var}
Any idea of how I can do something like that?
I have to stay in the shell, as I am running this through a jenkins 'execute shell'

So, with the help of the comment, I finally manage to do it.
The link explanations of quotes in mysql explain everything.
The solution :
$ var='test'
$ mysql -e "INSERT INTO table (text) VALUE ('$var');" database

Related

MySQL Bash script with variables

How can I execute multiple SQL queries in the bash script?
I read these two posts from previous years:
A better way to execute multiple MySQL commands using shell script
How to execute a MySQL command from a shell script?
They brought some clarification, but there is still something I do not understand.
I have multiple queries for deleting information about subject with defined subject_id.
Unfortunately I need to run all of them since the table is not in the "cascade" mode.
Is there a way, to create a bash script in which I can use the "user given" variable (by that I mean for example [ read -p 'Subject ID' SUBJECT_ID ]) that will be used inside as the subject_id in each of the queries?
Do I still have to adjust everything to this:
mysql -h "server-name" -u root "password" "database-name" < "filename.sql"
or is there a way to just run this script with connection to db from .cnf file inside it?
There are two questions above. One is how to get a bash variable into your SQL script. I would do this:
read -p 'Subject ID' SUBJECT_ID
mysql -e "SET #subject = '${SUBJECT_ID}'; source filename.sql;"
Bash will expand ${SUBJECT_ID} into the string before it uses it as an argument to the mysql -e command. So the MySQL variable is assigned the string value of SUBJECT_ID.
This will be tricky if SUBJECT_ID may contain literal single-quote characters! So I suggest using Bash syntax for string replacement to make each single-quote in that into two single-quotes:
mysql -e "SET #subject = '${SUBJECT_ID//'/''}'; source filename.sql;"
Note you must put a semicolon at the end after the filename.
The second question is about specifying the host, user, and password. I would recommend putting these into an options file:
[client]
host=server-name
user=root
password=xyzzy
Then when you invoke the mysql client:
mysql --defaults-extra-file myoptions.cnf -e '...'
This is a good idea to avoid putting your plaintext password on the command-line.
Read https://dev.mysql.com/doc/refman/8.0/en/option-files.html for more details on option files.

Escaping special characters in mysql password while executing mysql query on remote host

I'm trying to run a mysql query on a remote host using a bash script.
${MYSQL} -u ${USER} -p${PASS} -P${PORT} -h ${HOST} -e "select * from information_schema;"
My PASS looks something like "dfsf#DFD". It conatins '#' character.
For some reasons the PASS is not retrieving correctly in the script. Its getting chopped off after '#'.
And for some reason including source /etc/environment in the script, seems to fix the problem.
I want to understand why is this happening. How to make it work without the source statement.
As far as I know - the safest way to specify remote connection details for MySQL is using configuration file: https://dev.mysql.com/doc/refman/8.0/en/option-files.html
So briefly you just put something like this in ~/.my.cnf file:
[client]
host='...'
port='...'
user='...'
password='...'
And then you simply run
mysql -e "select * from information_schema;"
Not sure it suits your situation, but it should solve your issue, if you quote the values.
Quoting at command line is also a solution, but your credentials will be visible to everyone able to see your processes at the system where you run that.

How can i pipe the ouput variables of CUT command to another command

I want to do something like this:
cat files_list | cut -d' ' -f1,3 | mysql -uroot -pxxxx -e "insert into table(var1, var2) values($f1,$f2)"
How can i achieve this thing?
You can use the following bash script:
while read -a record ; do
mysql -uroot -pxxxx -e "insert into table(var1, var2) values(${record[0]}, ${record[2]})"
done < files_list
However, this is simple but performs not very well.
If the task is performance critical, I would build just a single mysql query, which inserts all rows at once, or even better: use LOAD_DATA_INFILE
Also note, if the task is security critical, meaning the input data comes from an untrusted source, I wouldn't use the command line mysql client at all. Using a programming language which supports prepared statements for mysql - like PHP - would be the way to go.
Using a programming language would had another important advantage - you wouldn't need to pass the password via commandline which is insecure.

BASH script get's row data from mySQL and uses that to launch PHP scrips

I've been trying for a few hours now to store row data into a BASH var then use that to launch some PHP scripts.
So far, I've only been able to echo out the whole result set. I have since broken that script, so I can't even paste it here. This is what I have, that's not working at all.
#! bin/bash
query=`echo "SELECT id FROM searches WHERE running=1 AND id_user=2" | mysql -u root`
I've never used BASH before, so I'm completely lost.
What I'm asking for is some resources that can show me how I can connect to mysql, then loop through a result set using the data from that row.
Thanks.
You can read mysql --silent output into Bash vars with while read:
>sql="SELECT id FROM searches WHERE running=1 AND id_user=2"
>mysql --silent -u ctrahey test -e "$sql" |while read myid; do
php -f my_processor.php $myid;
done
>
Notes:
sql="... sets a Bash variable of your sql (not actually necessary, just aides readability)
mysql -e the -e option allows you to pass in a query, so you don't need STDIN
mysql --silent --silent suppresses the extra formatting mysql usually does. in my testing, this was actually also unnecessary (the pipe chars in the output did not mess up Bash)
php -f .. this executes a php file, passing the current id as an arg.
Bash is not a good language for interacting with mysql, it only works for very very simple cases. Use php instead (since you mentioned php), it has an api for interacting with mysql databases sanely. And yes, you can run a php script as you would a bash script.
If you want to run a single command with MySQL, use mysql -e or mysql --execute. They are the same command, but the second version is more memorable. If you add this flag to the above, the rows will be stored in the variable.
However, as mentioned by geirha, BASH may not be the best language to get row data. But if you choose to parse it wish BASH, mysql -e is the right command to execute queries.

How to get output of shell in MySQL?

In linux I can use ! to start a shell command:
mysql >\! ping localhost
Is there a way to get the output of shell in MySQL?
While in the mysql prompt, no. You can however pipe the results of a command into mysql with something similar to:
mysql -u user -ppassword < `ping localhost`
The only issue with this is that whatever your shell command returns needs to be the sum of the commands/data you want to issue to mysql. This is most commonly used to execute a script or something similar.