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.
Related
If I run a SQL query in MySQL workbench and the connections time out after 30 seconds because it is taking a long time. Does my Query continue executing on the MySQL server even though I am disconnected?
For example, if I am doing an update and the update loops over a billion records. Does the MySQL server disconnect me first then it finishes the query after? Or does it disconnect me and terminate the query?
It does. As Mustafa mentioned, you can see the query still running if you look at "Administration tab" --> Management --> Client Connections.
Also good to remember that you can change the 30sec cap to longer, shorter or none.
Yes, MySQL Workbench can disconnect and the query keeps running. This has been reported as a bug, but it's in the "Verified" state, which means it is not fixed: https://bugs.mysql.com/bug.php?id=78809
See also this related SO thread: MySQL Query running even after losing connection
If you have a long-running query that needs to do a bulk update, you may need to change the MySQL Session timeout options in the MySQL Workbench preferences. Alternatively, don't use MySQL Workbench for long-running jobs, use the mysql command line tool.
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.
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.
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;"
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