Cancel a mysql query - mysql

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.

Related

Q: MYSQL Data tables suddenly missing

Earlier today [11-09-2021] one of our databases from our production environment suddenly dropped it's table for reasons we don't know. This happened around after 4am, since we still had a snapshot of our drive for that time, which is weird as no one was using or accessing the server at the time. Can someone tell if this normally happens?
This for sure its not normal behavior, you should check MySQL logs to see what was happening at that time.
In MySQL we need to see often 3 logs which are mostly important:
The Error Log. It contains information about errors that occur while the server is running (also server start and stop)
The General Query Log. This is a general record of what mysqld is doing (connect, disconnect, queries)
The Slow Query Log. Ιt consists of "slow" SQL statements (as indicated by its name).
The one that will be your starting point is The General Query Log.
By default no log files are enabled in MYSQL. All errors will be shown in the syslog (/var/log/syslog).
To Enable them just follow below steps:
1. Go to mysql conf file (/etc/mysql/my.cnf) and add following lines:
Enable general query log add following
general_log_file = /var/log/mysql/mysql.log
general_log = 1
2. Save the file and restart mysql using following commands
service mysql restart
To read content of the error log file in real time, run:
sudo tail -f $(mysql -Nse "SELECT CONCAT(##datadir, ##general_log_file)")
Hope this will help you to find out what actually happened on your database server.

Safely Cancel a MySQL import

I'm trying to upload a large database:
$mysql -u USERNAME -p MY_DATABASE < MY_DATABASE.sql
But the process is going extremely slowly, I would like to cancel and try again with different settings. How can I cancel this upload 'safely'? Or what is the best way to clean up my database after forced termination of the import?
You can kill MySQL process involved in your MY_DATABASE.sql
This show you running processes:
show processlist;
Then you should kill the process selecting it by the correct process_id from the user and time shown from previous command to understand which one is the correct one, if others processes are running:
kill process_id
I think that cleaning the DB is the hardest work, because it depends on the MY_DATABASE.sql content: if it populates a table, you can simply truncate it, if it create a DB and upload tables, views, stored procedures and other elements, you should drop the DB.
If you take a look at the MY_DATABASE.sql content and at the server content after killing the process (db created? tables or other elements created?) you will understand what to do.
If, as I can imagine, the script creates entirely a DB, you should only have to drop the DB created from the previous interrupted upload, if any, and restart the upload.
Rather stopping MySQL import with killing the process, I prefer use terminate key which is ctrl+c and service mysqld restart or service mysql restart.
This just ends the import process and it gets sometimes to end. Once it's done you can go with your process. If you end the process with killing it may cause some unexpected errors like a mysql server crash.

How to detect what is running on MySQL every hour?

Munin showing huge spike on MySQL queries every hour but I am unable to detect what is causing this. I am running version 5.6.30.
Tried to enable slow running queries but can't find it there.
Also logged all queries and tried to see what is running on that particular time. I cannot find it.
Checked cronjobs but there wasn't anything related
Disabled almost everything on LFD & CSF
The event scheduler status is set to OFF
Is there any other way to find what is running every hour?
Munin graph showing sql queries:
You can use a shell script and put the instruccion "processlist" and send the output to a log file.
Put the delay in seconds that you want for run again the instucction.
while [true];
mysql -h localhost -u root -ppasswd < process
delay xtime
done
And the file processlist you put the instrucction "show full processlist"
I hope this help you
Regards
Some crawlers were mining data from my website. I wasn't able to detect because requests sent from a million different IPs.
Added captcha to website as human control and spikes gone.

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

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;"

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