fetch mysql result in batch shell - mysql

i'm trying to create a batch script to do some actions in my mysql database and some other actions with linux command, I'm trying to fetch result from my query to mysql in my script to put the result in an associative array, I've found just how to do it in simple array for one column, here is my query:
result=`mysql -h $DATABASE_HOST --user=$DATABASE_USER --password=$DATABASE_PASSWORD --skip-column-names -s -e "select id,type from $DATABASE_NAME.Media where status=0 "
I'm not an expert in batch script, sorry if it is a noob question !

result=$(mysql ...)
declare -A ary
while read id type; do
ary[$id]=$type
done <<< "$result"

Related

Why is this bash variable blank when taking output from mysql?

I am trying to take the output from a MySQL query in bash and use it in a bash variable, but it keeps coming up blank when used in the script, but works perfectly from the terminal. What's wrong here?
I've tried changing the way the statement is written and changing the name of the variable just in case it was somehow reserved. I've also done a significant amount of searching but it turns out if you but 'bash', 'blank', and 'variable' in the search it usually comes up with some version of how to test for blank variables which I already know how to do.
tempo=$(mysql -u "$dbuser" -p"$dbpass" -D "$database" -t -s -r -N -B -e "select user from example where user='$temp' > 0;")
printf "the output should be: $tempo" # This is a test statement
The end result should be that the $tempo variable should either contain a user name from the database or be blank if there isn't one.
I think there is some error with your sql statement at user = '$temp' > 0.
But to get the result from MySql you have to redirect the standard error (stderr) to the standard output (stdout), you should use 2>&1.
Most probably you will run into MySql error but try running this on terminal.
tempo=$((mysql -u "$dbuser" -p"$dbpass" -D "$database" -t -s -r -N -B -e "select user from example where user='$temp' > 0;") 2>&1)
The solution was to echo the result of the sql query like this:
tempo=$(echo $(mysql -u "$dbuser" -p"$dbpass" -D "$database" -s -N -B -e "select user from example where user='$username' > 0;"))
Now I'm left with logic issues but I think I can handle that.

Oneliner pipe to PostgreSQL from MySQL with bash

So I am piping quite a lot of data using bash everyday between 3 servers:
Server A is mysql (connection over SSH)
Server B is just a centos server where I run the
bash script.
Server C is postgresql 9.6.
All was good until one table got one row with a double quote in the middle of a varchar. This is breaking my pipe at the insertion level (on pg side).
Indeed, when getting the data this way from Mysql, it is not quoted. So, I believe in the end it's because of the basic behaviour of COPY and its QUOTE parameter.
Here is the bash code:
ssh -o ConnectTimeout=5 -i "$SSH_KEY" "$SSH_USER"#"$SSH_IP" 'mysql -h "$MYHOST" -u "$USER"-p"$PWD" prod -e "SELECT * FROM tableA "' | \
psql -h "$DWH_IP" "$PG_DB" -c "COPY tableA FROM stdin WITH CSV HEADER DELIMITER E'\t' NULL AS 'NULL';"
I tried playing with the COPY parameter QUOTE but unsuccessfully.
Should I put some sed in the middle of the pipeline?
I also tried double quoting when getting the data out of mysql but could not find the relevant parameter when mysql is used in a pipe like this.
I'd lik to keep things in one pipe (no MYSQL->CSV then CSV->PG please).#
Thanks!
Here's working sample of importing csv to postgres:
t=# create table so10 (i int,t text);
CREATE TABLE
t=# \q
postgres#vao-VirtualBox:~$ echo "1,Bro" | psql -d t -c "copy so10 from stdin with csv"
COPY 1
postgres#vao-VirtualBox:~$ psql t -c "select * from so10"
i | t
---+-----
1 | Bro
(1 row)
You can open ssh tunnel to mysql and run mysql -h "$MYHOST" -u "$USER"-p"$PWD" prod -e "SELECT * FROM tableA "' locally (instead of echo in my example)

Passing a MySQL "SELECT" query to a shell variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
Lets say I have a query first that inserts certain values in a table and the next query is to display the maximum value of one of the columns and then store that in a variable. I then need to display that variable such that it shows the max value.
For example:
sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -e "INSERT INTO service_status_batch VALUES ();"
batch_id= sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -e "SELECT MAX(id) as maxid FROM service_status_batch;"
echo "Value of the id is:" $batch_id
This echo command should then show the value of the variable. However it ends up showing me the value of the select query in the form of a table and not the value of the variable.
Is there a particular way to assign the query value to a variable in shell script?
I have attached the select query value that it shows.
Use -s and -N options with mysql command like this.
sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -e "INSERT INTO service_status_batch VALUES ();"
batch_id=`sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -s -N -e "SELECT MAX(id) as maxid FROM service_status_batch;"`
echo "Value of the id is:" $batch_id
Refer the details for -s and -N :
--silent, -s
Silent mode. Produce less output. This option can be given multiple
times to produce less and less output.
This option results in nontabular output format and escaping of
special characters. Escaping may be disabled by using raw mode; see
the description for the --raw option.
--skip-column-names, -N
Do not write column names in results.
EDIT3: Bad explanation - I was trying to show how to get the value considering it could be used as necessary:
sudo echo $(echo "SELECT MAX(id) as maxid FROM service_status_batch" | mysql dbnamehere -uUser -pPassword)
EDIT1: variable version obviously
EDIT2: corrected variable assignment by using shellcheck.net as suggested. thanks.
EDIT3: one last edit to add sudo right before mysql command as it won't work without it for users other than root.
batch_id=$(echo "SELECT MAX(id) as maxid FROM service_status_batch" | sudo mysql dbnamehere -uUser -pPassword)

HOWTO read mysql SELECT into bash variables, then use those variables for INSERT INTO a different table

i am fairly new to this so please be patient, my understanding of bash however is that i can run
mysql --host=hostname --user=username --password=password -e "SELECT * FROM database.table;"
but i have less than no idea from other manuals how to get those into actual bash variables someone mentioned using
read a b c
do while
echo "..${a}..${b}..${c}.."
but i fail to see how that will read them into the variables?
also on reading the varibles back in i will be doing something like
#>WGET $a
then login to mysql again and doing something like
LOAD DATA INFILE data.csv INTO thattable ON DUPLICATE UPDATE
i want to also so something like
INSERT INTO thattable WHERE (i just loaded the info) date = today
but because there will be multiple dates how do i do this, and yes this all needs to be bash-able php is too slow and C i want to avoid unless it's the only way,
thanks i know this is a lot!
-AW
Option 1
Use "select ... \G", store result in a tmp file and grep for the columns.
By example:
mytmp=$(mktemp /tmp/mytemp.XXXXXX)
mysql --host=hostname --user=username --password=password -e "SELECT * FROM database.table \G;" > $mytmp
column_foo=$( fgrep COLUMN_FOO $mytmp | cut -d ':' -f2-)
column_bar=$( fgrep COLUMN_BAR $mytmp | cut -d ':' -f2-)
echo $column_foo
echo $column_bar
Option 2
If the amount of columns is high store all them in a hash array:
mytmp=$(mktemp /tmp/mytemp.XXXXXX)
mysql --host=hostname --user=username --password=password -e "SELECT * FROM database.table \G;" | xargs -I{} echo {} > $mytmp
declare -A a
while IFS=':' read k s; do a[$k]=$s; done < $mytmp
echo ${a[COLUMN_FOO]}
echo ${a[COLUMN_BAR]}

Reading result of create database in mysql from Bash shell script

I am creating a database in a shell script using
mysql -u$username -p$password -e " create database testdb"
I want to store the result of this statement to make decisions in shell script based on its success or failure.
Even during insert statements i do not get any return value
how can is store the result after executing create database / insert statements in shell script?
use the following command substitution syntax:
var=$(command-name-here arg1 arg2...)
So for your command you can do something like this,
output=$(mysql -u$username -p$password -e " create database testdb")
echo "$output"