Close mysql connection through shell script - mysql

I am running shell scripts which have multiple mysql -u <> -e "select * from tbl" statements. Do i need to explicitly close mysql connection through my shell script? If yes, can you guide me on how to do that?
I am seeing many sleep connections in mysql. Even if i kill them, they are created again in short period an i am suspecting that shell script execution is causing this.
Thanks

If you didn't solve the root cause of the sleep queries it will be restarted and will consume the resources after each time you killed the connection, so you have to check your code and modify it to close the connection after executing the query.
Any way, you can kill any connection by it's ID which can be determined by:
mysql -e 'show processlist;'
The ID will be in the first column and then you can kill them by the following:
mysql -e 'kill $ID'
And to kill them all in one time:
for i in $(mysql -e "show processlist" | awk '/Sleep/ {print $1}'); do mysql -e "KILL $i;"; done

Related

mysql cli client, keep session between multiple commands

I'm trying to do something like this:
$ mysql -e "FLUSH TABLES WITH READ LOCK"
$ ./do-something.sh
$ mysql -e "UNLOCK TABLES"
so-something.sh script should be unable to write to database.
Is that possible using bash, or I need python or something like that? Problem is that lock is removed when session is lost, can I run 2 commands with same session?
You can execute the shell script from the MySQL client using the system command. So you can write something like this:
$ mysql -e "FLUSH TABLES WITH READ LOCK; system ./do-something.sh; UNLOCK TABLES;"

How to set timeout on MySql query?

I have a shell script how execute some query on a big loop, but sometimes the script doesn't respond.
I want to add a timeout to Mysql query to prevent this bug.
$sql=`mysql --protocol=tcp --user=**** --password=\'****\' --host="vl*****1" --database="C***K" -e "UPDATE C****k SET current_status =\'$status\',last_state_change=\'$lastchange\' WHERE MC_internal_name=\'$InternalName\';"
Tkanks !
Use option:
--connect-timeout=#
Replace dash with number of seconds before connection timeout.

MySQL query doesn't run within cronjob

My problem is that this:
mysql -u root -pmypassword -D cache -e "DELETE FROM cache WHERE job IN('SET', 'UO', 'AV');"
MySQL query doesn't run with the cronjob. I am sure that the cronjob starts itself because rest of the code (mysqldump) is running.
#!/usr/local/bin/bash
mysql -u mysql-u1494 -pmy-password -D cache -e "DELETE FROM cache WHERE job IN('SET', 'UO', 'AV');"
/usr/local/bin/mysqldump --skip-lock-tables --ignore-table=log.log --user="mysql-u1494" --password="my-password" names > dump.sql
When I run this command manually inside of the terminal it works fine. So the question is, why doesn't this query run?
Probable causes are
because you didn't provide a full path for mysql
your password contains characters that need to be escaped
Try to change
mysql -u mysql-u1494 -pmy-password -D cache -e "DELETE FROM cache WHERE job IN('SET', 'UO', 'AV');"
to
/usr/local/bin/mysql -u mysql-u1494 -p"my-password" -D cache -e "DELETE FROM cache WHERE job IN('SET', 'UO', 'AV');"
^^^^^^^^^^^^^^ ^ ^

MySQL kill sleep connections

My DB seems to have almost 100 "sleep" connections and I believe it would be better to get rid of them. I have found the following script which helps me analyze all tables in a DB from the command line. Does anybody know of a similar one liner which can kill all "sleep" connections?
mysql -p -D<database> -B -e "SHOW TABLES" \
| awk '{print "CHECK TABLE "$1";"}' \
| mysql -p -D<database>
You can set the wait_timeout variable to a shorter time than the default and it should kill off the connections once they exceed this time value.
From what I've read there is some bugginess and connections will still randomly hang around, especially under load, but the wait_timeout variable would be the place to start.
Some thoughts:
You may use SHOW PROCESSLIST instead of SHOW TABLES. I'm not much familiar with awk linux utility but I think you can do all sorts of pattern matching there in.
Alternatively, you can write a script to query MySQL for "SHOW PROCESSLIST", parse the resultset for sleeping connections and accordingly use KILL <process id>
for i in `mysql -e "show processlist" | awk '/Sleep/ {print $1}'` ; do mysql -e "KILL $i;"; done
https://www.digitalocean.com/community/questions/how-can-i-kill-all-mysql-sleeping-queries

Batch script to issue commands to mySQL db?

I am trying to create a batch script that would connect to a mySQL database and issue a delete command:
#echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback
delete from competency_question_answer;
I will run this script providing the password as a command-line argument, but all this script does is, connects to the database, and the mysql> prompt will be shown. After I exit from mysql, the rest of the batch commands get to execute (and fail, no surprise).
How can I pass the SQL commands from the batch script to the mysql console? Is this even possible?
You need to use command line tools. I don't know if there exists any for MySQL but for SQL there is SQLCMD and for Oracle there is OSQL.
What you can also do is something like this.
mysql -uuser -ppass < foo.sql
Where foo.sql is the commands you want to execute.
You may need to connect multiple times:
#echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback -e delete from competency_question_answer;
Alternatively, you should be able to put all your commands in a separate file such as input.sql and use:
mysql -hlocalhost -urdfdev -p%1 rdf_feedback <input.sql
echo "delete from competency_question_answer;" | mysql -hlocalhost -ur... etc.
Putting multiple sets of commands into .sql batch files works best, and you can execute multiples of these in the .bat file.