How do I routinely kill MySQL queries that have been alive for "too long"? - mysql

How do I routinely kill MySQL queries that have been alive for "too long"?
Is there a system information table of sorts that shows all current queries, and their age?
Edit: updated question from "killing connections" to "killing queries"

Install the RubyGem mysql_manager (sudo gem install mysql_manager) and then run a command like this:
mysql-manager --kill --kill:user api --kill:max-query-time 30 --log:level DEBUG
For more options, run mysql-manager --help.
You might need to specify an alternative --db:dsn, --db:username, or --db:password.
Read more about it here: https://github.com/osterman/mysql_manager

You can execute...
SHOW FULL PROCESSLIST;
...to show you the currently executing queries.
However, you shouldn't just kill the connections, as this may cause data integrity issues. Instead, you should use the processlist output as a means of highlighting where potential problems may lie prior to correcting the issues at source. (It's sort of a (very) poor man's MySQL Enterprise Monitor in that sense.)

MySQL 5.0.x only supports the “SHOW FULL PROCESSLIST” command. There’s no ability to query and filter the process list as thought it were a SQL table, e.g. SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST. This ability was added in MySQL 5.1+
MySQL has a KILL command that can kill either a query or the entire connection.
http://dev.mysql.com/doc/refman/5.0/en/kill.html
Still, you’d need a Ruby or Perl script that runs “SHOW FULL PROCESSLIST”, identifies which queries are running “too long”, then issues the appropriate KILL commands.
You can also do this from the command line, e.g.
mysqladmin processlist
mysqladmin kill

The command "mysqladmin processlist" will show the current connection, and a Time column which indicates the time since last activity.
You can do the same with the SQL command "SHOW FULL PROCESSLIST;"

Related

mySQL loses connection on one table, doesn't on any others

I have a MySQL database with multiple tables. When I run a simple SELECT command on one of the tables, it gives the "lost connection" error, but when I run the same command on any of the other tables, it works just fine. Does anyone know what could be the cause of this issue? I'm running the commands from the MySQL Workbench.
You must set the 'Interactive_timeout' and 'wait_timeout' properties in the mysql configuration file to the values ​​you need.
Quite likely there is some sort of a transaction running. Whether you started the transaction via a sql command (i.e. START TRANSACTION) or whether some other query (possibly from a GUI) started a transaction (Like an ALTER TABLE command when adding a column) and the transaction never finished.
Run the command
SHOW FULL PROCESSLIST;
to find the IDs of all processes that are running for your user
You can then kill the processes by running the command
KILL <PROCESS ID> (i.e. KILL 40196;)
When you find and stop the process that contained the transaction that never finished you will likely be able to query the table again. Your table will be queryable again, and you can look further into solving the problem of why the transaction was started but never finished.

What is the purpose of the `KILL QUERY` command?

I am beginner to MySQL and SQL and I am learning these technologies from MySQL and MyISAM websites.
I am learning the KILL command now and have learned how to use KILL CONNECTION. But when I come to learn KILL QUERY from MySQL and MyISAM websites where they simply provided information about KILL QUERY that they terminate query that the specified connection id is executing.
Other user (not me) executes INSERT and DELETE queries but when i have used KILL QUERY by getting the connection id of other user via SHOW PROCESSLIST command, it says
Query OK, 0 rows affected (0.01 sec)
But nothing happen to other user.
I just want to know that what is the exact use of this command. What is the purpose of this KILL QUERY command. Can anyone tell me?
Some useful pointers:
How can I stop a running MySQL query?
MySQL Kill query
How to kill all processes for a Specific user
MySQL Manaul - Kill
You need to have some clarity here KILL will kill the connection, but KILL QUERY will only kill that query and will leave that connection intact.
Really, if you are using KILL QUERY on another users command, this can only really be noticable if their query is running for longer than your Kill Query command, so if it's a slow query, because [most] queries will execute and close within a fraction of a second, so by the time you've KILLed the other users query, their query may well have completed and closed anyway.
I suspect this is what you are seeing, that you're trying to kill a query that closes and is finished before the kill command can execute.
Killing queries seems (from literature) only useful for slow and long running queries, and why this is so should be obvious from the above.
Edit:
(I had this ready to post on my original answer and then took it out as being irrelevant, but seems to be more relevant now due to comments!)
From the MySQL Manual:
Note
You cannot use KILL with the Embedded MySQL Server library because the embedded server merely runs inside the threads of the host application. It does not create any connection threads of its own.
So to answer your question in the comment, I think that you may be running MySQL embedded so your kill calls will not function.

Cancel a mysql query

Is it possible to cancel a mysql query?
I have the problem that I submit a query which is very time-consuming because of a mistake. And now I can't make a new query because the server is working and working...
Or is there a way to stop all queries in a database or in a table.
For your information I am using phpMyAdmin (MySql).
In phpMyAdmin, go to home page > Status; you'll see a list of your MySQL processes and you have a Kill link for each of them.
Probably your phpMyAdmin will be stuck showing the "Searching" message. What you can do is to open another tab / open a MySQL session with the console and do the following:
Look for all the processes running:
SHOW PROCESSLIST;
And then kill that specific thread with the KILL command:
KILL <thread_id>;
Resources:
SHOW PROCESSLIST page in MySQL
KILL page in MySQL
You could use the KILL statement : http://dev.mysql.com/doc/refman/5.0/en/kill.html
Sometimes, you can't even get to phpmyadmin even on a new tab.
If you have access to the environment via command line,
1) log into mysql
mysql -u yourusername -p
2) SHOW PROCESSLIST;
3) KILL <thread_id>;
This might help you:
Post in Mysql Forum
This will teach you to test your queries before just throwing them into a page. EXPLAIN PLAN is your friend.
Sometimes if the database server is not responding because of a too long query and:
the phpMyAdmin is stuck showing the "Loading..." message...
you can't log into MySQL server with appropriate permissions or if so but theSHOW PROCESSLIST command in CLI is not very useful because of for example too big amount of started processes...
then, the fastest option might be to restart the server, which stops all queries in a database:
service mysql restart
or
sudo /etc/init.d/mysql restart
This is not the safest option, because in some cases the interrupted queries may cause inconsistency of data in the database..., but sometimes it may be the best solution.

mysql - is there a tool that can show a live log of all the sql commands the mysql engine receives?

using mysql - is there a tool that can show a live log of all the sql commands the mysql engine receives?
a. It would be helpfull to see explain plan for the DML commands.
b. It would be helpfull if it could be turned on and off on a mysql server with no need to restart the service.
c. open source/free like the mysql itself would be nice.
Thanks.
You first need to activate the general query log. There must be some tools around to help you parse this log, my favourite being :
tail -f /path/to/mysql.log
;)
Unfortunately, this may not be disabled dynamically, you would need to restart the server in order to activate/disable the log.
Careful, though, activating this log has a severe performance impact. Do not use it in production.

How do I Interrupt a long query in the mysql command line tool without quitting mysql?

While debugging SQL statements, if I accidentally execute a query in using the mysql command line that outputs at lot of results (even if the query itself executes in reasonable time), the only way I know of to stop the endless stream of output is CTRL-C.
Unfortunately this puts me back in the shell, forcing me to login and select the database again.
To avoid this I've started running mysql with the --sigint-ignore option so that CTRL-C is ignored.
Now I'd like a way to interrupt the output of those long queries.
Is there a keyboard shortcut that will do this?
Not a keyboard shortcut.
The only choice is to open another session, use SHOW PROCESSLIST and then KILL QUERY the one you want to terminate.
You can also use the mysqladmin command-line tool to issue these commands.
Either way, it requires you to login. So it's not much of an advantage over just hitting Ctrl-C.
You can use --pager to have your output passed to a pager such as less which will give you control over the output. Not just killing it, but also paging, searching and even storing the output better than your terminal window gives you.
There's also the --safe-updates or -U switch aka --i-am-a-dummy which protects you from clauseless updates and deletes and also auto limits selects to 1000 (modifyable with select_limit).
All of this can be set by default in ~/.my.cnf.
[mysql]
pager
safe-updates
From the current mysql docs:
As of MySQL 5.1.10, typing Control-C
causes mysql to attempt to kill the
current statement. If this cannot be
done, or Control-C is typed again
before the statement is killed, mysql
exits. Previously, Control-C caused
mysql to exit in all cases.
Since I was using version 5.0.67 seems that updating mysql would be the best fix. However I have accepted Schwern's answer because it was quick to implement and works like a dream.
Bit late, but maybe my answer will help someone.
A way to kill a specific mysql query through the command line would be:
Run mysqladmin processlistand find the process id of the query by the command
Run mysqladmin kill <query_id>.
Assuming a query ID 123456789 you'd just need to run: mysqladmin kill 123456789
Use mysql --sigint-ignore
and to clear a line, use control+U
As of MySQL 5.7 (released in 2013), ^C cancels a query in progress and returns you to the mysql shell. ^C currently only exits if there is no query running.
Previously, Control+C in mysql interrupted the current statement if there was one, or exited mysql if not. Now Control+C interrupts the current statement if there was one, or cancels any partial input line otherwise, but does not exit.
MySQL 5.7 release notes