Run mysql query inside bash script - mysql

I am trying to run MySQL query through bash script. But, when I run SELECT * FROM EXAMPLE_DB; inside bash scripting, it is translated to SELECT files1 files2 files3 where I run the script.
Example :
read -d '' SQL_QUERY << EOF
SET #var_name = NOW() - INTERVAL 30 DAY;
CREATE TABLE tassta.temp_vontista_messages AS SELECT * FROM tassta.vontista_messages WHERE date(sent_date) >= date(#var_name);
EOF
echo ${SQL_QUERY} | mysql
What I want to run the mysql query as it is. What happened now that this is translated to
read -d '' SQL_QUERY << EOF
SET #var_name = NOW() - INTERVAL 30 DAY;
CREATE TABLE tassta.temp_vontista_messages AS SELECT file1 file2 file3 [files from where I run the script.] FROM tassta.vontista_messages WHERE date(sent_date) >= date(#var_name);
EOF
echo ${SQL_QUERY} | mysql

SQL_QUERY="SET #var_name = NOW() - INTERVAL 30 DAY;
CREATE TABLE tassta.temp_vontista_messages AS SELECT * FROM tassta.vontista_messages WHERE date(sent_date) >= date(#var_name);"
mysql -Be "$SQL_QUERY"
or:
echo "$SQL_QUERY" | mysql
NOTE: Do not put spaces before, or after the =.
see: How do I escape the wildcard/asterisk character in bash?

Related

Stock query result in variable and send email

I'm trying to monitor a specific port of a switch. I have to send email if my query returns a number >= 2.
I have tested my query and it works in Mysql CLI :
SELECT COUNT(message)
FROM devices, eventlog WHERE sysName = 'sysName'
AND message = 'ifOperStatus: up -> down'
AND datetime >= now() - INTERVAL 15 MINUTE
This Query returns a correct value. So, I have decided to create a script that executes the query and get infos :
#!/usr/bin/bash
mysql -u DB_USER -p PASSWORD DB_NAME <<MY_QUERY
SELECT COUNT(message)
FROM devices, eventlog WHERE sysName = 'swysName'
AND message = 'ifOperStatus: up -> down'
AND datetime >= now() - INTERVAL 15 MINUTE
MY_QUERY
When I execute this script in terminal, I got this:
librenms#librenms:/opt/librenms/scripts$ bash -x test1.sh
+ mysql -u DB_USER '-p PASSWORD' DB_NAME
COUNT(message)
0
Now I have to get query's value in a variable and test variable with an "if" in the script as :
....
if ($nb >= 2)
Then, if condition is true, send mail to alert admin.
I don't know how to get value in a variable and send by mail in bash script. If someone can help me...
Thanks.
You should get results of mysql query first:
RES = $(mysql -u DB_USER -p PASSWORD DB_NAME <<$MY_QUERY)
and then send it using:
echo $RES | mail -s "Information" email#email.com

Mysql variable in bash script

I'd like to create a script that check a variable on my mysql db, let me explain better.
I have this query:
SELECT * FROM my.backup
WHERE start_time > DATE_ADD(NOW(), INTERVAL -3 DAY) AND host=20;
I'd like to modify the value host like this
host='$1
but I have a little trouble, below there is a simple script:
test="$(sshpass -p ,password> ssh -T <my_host>#$1 <<'ENDSSH'
mysql --defaults-extra-file=~/my.conf mybman
SELECT * FROM my.backup WHERE start_time > DATE_ADD(NOW(), INTERVAL -1 DAY) AND host=20;
ENDSSH
)"
echo $test
result=$(echo $test | cut -d " " -f 8)
EXITCODE=$result
echo $EXITCODE
if [ $EXITCODE == "SUCCESS" ] ; then
echo "OK - to connect ($1)"
exit 0
elif [ -z $test ] ; then
echo "Critical to connect ($1)"
exit 2
else
exit $EXITCODE
fi
exit
Any ideas how can we create this kind of variable?
Thanks in advance!

Store multiple values from sql select in bash

I am trying to store each value from the following sql select statement and store them in separate variables using bash.
#!/bin/bash
mysqlhost="thehost"
mysqldb="thedb"
mysqlun="theusername"
mysqlpw="thepassword"
mysqlconnection="--disable-column-names --host=$mysqlhost --user $mysqlun --password=$mysqlpw --database=$mysqldb"
declare -a pinIDs=$(mysql $mysqlconnection -e "SELECT pinID FROM somewhere WHERE something = something";)
I get the following result when I use code
echo $pinIDs
8 11 23 26
I need to store each of those values into their own variable.
Add brackets to put output in array pinIDs. Replace
declare -a pinIDs=$(mysql $mysqlconnection -e "SELECT pinID FROM somewhere WHERE something = something";)
by
declare -a pinIDs=( $(mysql $mysqlconnection -e "SELECT pinID FROM somewhere WHERE something = something";) )
Then see output of: declare -p pinIDs
For others that are using this to learn. What I did after fixing the brackets in order to put the result into an array such as (8 11 23 26) instead of 8 11 23 26, was this:
cnt=${#pinIDs[#]}
for (( i=0 ; i<cnt ; i++ ))
do
echo "pinId: ""${pinIDs[$i]}"
done

In ksh , pass parameters to mysql select query

I have a linux script that gets a variable and I store it to var JOB_EXEC_ID
I am trying to pass the value of this to a MySQL query
Here is MySQL query set-up
print "JOB EXEC ID value for DataMeer Job ${LOADJOB} is : ${JobExecId} " |
tee -a ${LOGDIR}/${LOGFILE}
#Log on to MySQL to get the DataId
#Remove first the output file that would house the dataid
rm -f ${SCRDIR}/list_dataid.csv
mysql -u root -pmonday1 ${DAPDBNAME} < ${SCRDIR}/dataid_query.nosql
SQLRTN=$?
if [[ ${SQLRTN} != 0 ]]
then
print "Return code from sqlcall - DAP : ${SQLRTN}" |
tee -a ${LOGDIR}/${LOGFILE}
print "Exiting $Script - 55 " |
tee -a ${LOGDIR}/${LOGFILE}
exit 55
fi
The file dataid_query.nosql looks like this:
set #job_exec_id=10151
select d.id DataId
from data d inner join dap_job_configuration djc on d.dap_job_configuration__id = djc.id
left outer join dap_job_execution dje on djc.id = dje.dap_job_configuration__id and dje.created_data__id = d.id
where dje.id=#job_exec_id
into OUTFILE "/home/app1ebb/cs/list_dataid.csv"
I want to pass the value of JOB_EXEC_ID to the set command that is currently hardcoded right now with a value of 10151
in place of
mysql -u root -pmonday1 ${DAPDBNAME} < ${SCRDIR}/dataid_query.nosql
SQLRTN=$?
this lines
sed "1 s/[0-9]*$/${JOB_EXEC_ID}/" > /tmp/dataid_query.nosql
mysql -u root -pmonday1 ${DAPDBNAME} < /tmp/dataid_query.nosql
SQLRTN=$?
rm /tmp/dataid_query.nosql

MySQL Check Auto Increment Script - without 'SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES'

I've been working on this script for a couple of days now (i'm recently started writing in bash)
The script checks when a column is near to reach its auto_incement limit. I could of just get the auto_increment value from information schema. But, because we have hundreds of tables and about 4TB of data that would of taken awhile. So I decided to get the auto_increment value from 'SHOW CREATE TABLE' which hopefully will be faster(still have to run this in staging). I'm sure there are multiple ways to solve this problem and like I said I'm not an bash script expert but I would like your opinion and If there is anything I could be doing better. Hopefully this also helps others.
Thank you! -Gio
#!/bin/bash
#
# This is bash script checks when auto_increment column is reaching its limit
# To run Script $ ./auto_increment_check.sh [username] [password] [host]
MYSQL_USER="$1"
MYSQL_PASSWD="$2"
MYSQL_HOST="$3"
MYSQL=$(which mysql)
if [ $? != 0 ]
then
echo -e "\nMYSQL CLIENT NOT PRESENT!\n"
exit 1
fi
MYSQLCONNECT="$MYSQL -u$MYSQL_USER -p$MYSQL_PASSWD -h $MYSQL_HOST"
QUERY="
SELECT table_schema,
table_name,
data_type,
( CASE data_type
WHEN 'tinyint' THEN 255
WHEN 'smallint' THEN 65535
WHEN 'mediumint' THEN 16777215
WHEN 'int' THEN 4294967295
WHEN 'bigint' THEN 18446744073709551615
end >> IF(Locate('unsigned', column_type) > 0, 0, 1) ) AS MAX_VALUE
FROM information_schema.columns
INNER JOIN information_schema.tables USING (table_schema, table_name)
WHERE table_schema NOT IN ( 'MYSQL', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA'
)
AND extra = 'auto_increment'"
$MYSQLCONNECT --batch -N -e "$QUERY" | while read DATABASE TABLE DATA_TYPE MAX_VALUE;
do
NEXT_AUTO_INCREMENT=`mysql -uroot -pgio --batch -N -e "SHOW CREATE TABLE $DATABASE.$TABLE" | awk -F'AUTO_INCREMENT=' 'NF==1{print "0";next}{sub(/ .*/,"",$2);print $2}'`
AUTO_INCREMENT_RATIO=$(awk 'BEGIN {printf "%2.2f\n", '$NEXT_AUTO_INCREMENT' / '$MAX_VALUE'}')
[[ $(awk 'BEGIN{print ('$AUTO_INCREMENT_RATIO'>=0.9)}') -eq 1 ]] && echo "Auto Increment limit almost reached! $DATABASE.$TABLE - NEXT_AUTO_INCREMENT= $NEXT_AUTO_INCREMENT, MAX= $MAX_VALUE, RATIO= $AUTO_INCREMENT_RATIO"
done
If information_schema is slow for you, I recommend setting innodb_stats_on_metadata=0.
As for monitoring auto increment capacity, you should do this with common_schema.
Once common_schema is installed you can run a query like this:
select *
from common_schema.auto_increment_columns
order by auto_increment_ratio desc
limit 10;
For further information please read the blog post I wrote on this subject last year.