Stop MySQL query with keyboard shortcut [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is there a way that I can stop/kill an running SQL query with some keyboard shortcut?
I'm using MariaDB.

Here is a sample for [ctrl]-C
sample [ctrl]-C
MariaDB [yourschema]> select sleep(999);
^CCtrl-C -- query killed. Continuing normally.
+------------+
| sleep(999) |
+------------+
+------------+
1 row in set (2.12 sec)
MariaDB [yourschema]>
Sample KILL
window 1
MariaDB [yourschema]> select sleep(999);
window 2
MariaDB [(none)]> show processlist;
+--------+-----------------+-----------------+------------+---------+---------+-----------------------------+-------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+--------+-----------------+-----------------+------------+---------+---------+-----------------------------+-------------------+----------+
| 34 | event_scheduler | localhost | NULL | Daemon | 1340348 | Waiting for next activation | NULL | 0.000 |
| 43607 | root | localhost:65119 | yourSchema | Sleep | 20 | | NULL | 0.000 |
| 43609 | root | localhost:65120 | NULL | Sleep | 58 | | NULL | 0.000 |
| 80450 | root | localhost:51083 | yourSchema | Sleep | 68 | | NULL | 0.000 |
| 80452 | root | localhost:51084 | NULL | Sleep | 45 | | NULL | 0.000 |
| 80545 | root | localhost:51184 | yourSchema | Sleep | 3 | | NULL | 0.000 |
| 80546 | root | localhost:51185 | NULL | Sleep | 3 | | NULL | 0.000 |
| 129783 | root | localhost | yourschema | Query | 9 | User sleep | select sleep(999) | 0.000 |
| 129809 | root | localhost | NULL | Query | 0 | init | show processlist | 0.000 |
+--------+-----------------+-----------------+------------+---------+---------+-----------------------------+-------------------+----------+
9 rows in set (0.00 sec)
MariaDB [(none)]> kill 129783;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
window 1
ERROR 2013 (HY000): Lost connection to MySQL server during query
MariaDB [yourschema]>

Related

Not able restore mysql database

When i try to delete mysql database, before restoring, getting an error,
ERROR 1146 (42S02): Table 'mysql.proc' doesn't exist
But the mysql.proc exists in the database before dropping database.
Any idea why MySql is behaving unexpectedly here?
NOTE: This is not upgraded database, this is newly installed MariaDb-10.0.26 database.
Database changed
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| column_stats |
| columns_priv |
| db |
| event |
| func |
| general_log |
| gtid_slave_pos |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| index_stats |
| innodb_index_stats |
| innodb_table_stats |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| roles_mapping |
| servers |
| slow_log |
| table_stats |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
30 rows in set (0.00 sec)
MariaDB [mysql]> drop database mysql;
ERROR 1146 (42S02): Table 'mysql.proc' doesn't exist

How does MySQL know when connection is "over"?

I have just closed a MySQL shell by clicking x on my command prompt and I am now curious what happens to database connections when you close the window.
I know that the MySQL server runs on port 3306. If I understand correctly, the server waits for connection requests and passes them to a new thread/available thread for managing the connection.
How does the server know when the connection has been terminated, so that the thread can be released or put back into a pool of available threads?
Scenarios such as:
Blue Screen during a connection with a MySQL server on a remote connection.
Program terminated before it could call a disconnect request.
From the mysql client:
show processlist;
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
| 2 | root | localhost:6509 | so_gibberish2 | Sleep | 105 | | NULL |
| 3 | root | localhost:6510 | so_gibberish2 | Sleep | 105 | | NULL |
| 820 | root | localhost:40286 | stackoverflow | Sleep | 13457 | | NULL |
| 821 | root | localhost:40287 | stackoverflow | Sleep | 966 | | NULL |
| 827 | root | localhost:42254 | so_gibberish2 | Query | 0 | init | show processlist |
| 831 | root | localhost:44036 | stackoverflow | Sleep | 230 | | NULL |
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 827 |
+-----------------+
Now hit [x] as you say to client the mysql client that ran the above.
Below is from Mysql Workbench:
show processlist;
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
| 2 | root | localhost:6509 | so_gibberish2 | Sleep | 105 | | NULL |
| 3 | root | localhost:6510 | so_gibberish2 | Sleep | 105 | | NULL |
| 820 | root | localhost:40286 | stackoverflow | Sleep | 13457 | | NULL |
| 821 | root | localhost:40287 | stackoverflow | Sleep | 966 | | NULL |
+-----+------+-----------------+---------------+---------+-------+-------+------------------+
select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 3 |
+-----------------+
So the closing of a program naturally closes any open sockets it has.
Honestly, the latter output looks more like:
But I always use the mysql client for output formatting for tables which would throw off the results. So there you have it as an image.

MySQL MTS - Waiting for an event from Coordinator

I'm trying to setup mysql mts replication on 5.7.
I'm replicating one database using LOGICAL_CLOCK:
mysql> show variables like 'slave_parallel%';
+------------------------+---------------+
| Variable_name | Value |
+------------------------+---------------+
| slave_parallel_type | LOGICAL_CLOCK |
| slave_parallel_workers | 4 |
+------------------------+---------------+
2 rows in set (0.00 sec)
But only one thread is active, others just wait:
mysql> show processlist;
+--------+-------------+-----------------------+--------------------+---------+--------+---------------------------------------------+----------------------------------------------------------------------------+-----------+---------------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined |
+--------+-------------+-----------------------+--------------------+---------+--------+---------------------------------------------+----------------------------------------------------------------------------+-----------+---------------+
| 8103 | system user | | NULL | Connect | 72189 | Waiting for master to send event | NULL | 0 | 0 |
| 8104 | system user | | NULL | Connect | 0 | Waiting for dependent transaction to commit | NULL | 0 | 0 |
| 8105 | system user | | db_repl | Connect | 344764 | System lock | DELETE FROM docs WHERE id = 21099810 | 0 | 0 |
| 8106 | system user | | NULL | Connect | 344765 | Waiting for an event from Coordinator | NULL | 0 | 0 |
| 8107 | system user | | NULL | Connect | 344771 | Waiting for an event from Coordinator | NULL | 0 | 0 |
| 8108 | system user | | NULL | Connect | 344813 | Waiting for an event from Coordinator | NULL | 0 | 0 |
| 600637 | sphinx | localhost | mysql | Query | 0 | starting | show processlist | 0 | 0 |
+--------+-------------+-----------------------+--------------------+---------+--------+---------------------------------------------+----------------------------------------------------------------------------+-----------+---------------+
7 rows in set (0.00 sec)
How I can debug my configuration to see why threads are not active?
P.S. I have high loaded master and have big slave delay because slave has only one thread for replication, so I need to increase slave threads to decrease "Seconds_behind_master".

How can I clean up sleeping mysql processes?

I have an issue with a rails 4 application that leaves idle mysql connections. While we look at the code to determine the cause, I at least want to be able to automatically eliminate the offending processes. The mysql documentation says setting wait_timeout and interactive_timeout will close inactive connections so I've set them both to 300. However, as can be seen below, there are various processes that exceed the 300 limit. I've confirmed that the settings are indeed 300. Why isn't mysql eliminating these processes?
mysql> show processlist;
+-----+--------------+-----------+--------------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+--------------+-----------+--------------+---------+------+-------+------------------+
| 143 | application | localhost | application | Sleep | 1500 | | NULL |
| 144 | application | localhost | application | Sleep | 1499 | | NULL |
| 145 | application | localhost | application | Sleep | 0 | | NULL |
| 146 | application | localhost | application | Sleep | 780 | | NULL |
| 147 | application | localhost | application | Sleep | 783 | | NULL |
| 148 | application | localhost | application | Sleep | 0 | | NULL |
| 149 | application | localhost | application | Sleep | 7 | | NULL |
| 150 | application | localhost | application | Sleep | 3 | | NULL |
| 151 | application | localhost | application | Sleep | 8 | | NULL |
| 152 | application | localhost | application | Sleep | 3 | | NULL |
| 155 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+-----+--------------+-----------+--------------+---------+------+-------+------------------+
11 rows in set (0.00 sec)
try run mysql query kill 143;
this works for me

high cpu usage mysql

I've started to see very high cpu usage on mysql on my CentOS server.
It used to run at between 20%-30% but now will spike into the 400% at times.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9169 mysql 20 0 2232m 66m 6816 S 140.6 0.9 17:37.03 mysqld
when i do a show processlist i dont see much running
mysql> show processlist;
+-------+------+-----------+----------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-------+------+-----------+----------+---------+------+-------+------------------+
| 53 | sqluser | localhost | mydns | Sleep | 0 | | NULL |
| 449 | sqluser | localhost | DB1 | Sleep | 0 | | NULL |
| 16151 | sqluser | localhost | DB2 | Sleep | 105 | | NULL |
| 16473 | sqluser | localhost | DB2 | Sleep | 61 | | NULL |
| 16641 | sqluser | localhost | DB2 | Sleep | 39 | | NULL |
| 16729 | sqluser | localhost | DB2 | Sleep | 29 | | NULL |
| 16733 | sqluser | localhost | DB2 | Sleep | 29 | | NULL |
| 16815 | sqluser | localhost | DB2 | Sleep | 15 | | NULL |
| 16867 | sqluser | localhost | DB2 | Sleep | 9 | | NULL |
| 16886 | sqluser | localhost | NULL | Query | 0 | NULL | show processlist |
| 16910 | sqluser | localhost | DB2 | Sleep | 5 | | NULL |
| 16919 | sqluser | localhost | DB1 | Sleep | 4 | | NULL |
| 16935 | sqluser | localhost | DB1 | Sleep | 2 | | NULL |
| 16937 | sqluser | localhost | DB1 | Sleep | 2 | | NULL |
| 16939 | sqluser | localhost | DB1 | Sleep | 1 | | NULL |
| 16941 | sqluser | localhost | DB1 | Sleep | 1 | | NULL |
| 16951 | sqluser | localhost | DB1 | Sleep | 1 | | NULL |
| 16952 | sqluser | localhost | DB2 | Sleep | 1 | | NULL |
| 16955 | sqluser | localhost | DB1 | Sleep | 1 | | NULL |
| 16960 | sqluser | localhost | DB1 | Sleep | 0 | | NULL |
| 16965 | sqluser | localhost | DB1 | Sleep | 0 | | NULL |
+-------+------+-----------+----------+---------+------+-------+------------------+
21 rows in set (0.00 sec)
Is there anything else i can do to see what is causing such high usage?
First of all, you need to get some more data, in order to do that I usually use some tools like:
Mytop
Innotop
Mytop has some great ways of viewing active connections and queries.
Innotop has great ways of seeing what is using your table spaces, buffer pools, etc.
You don't have to use these tools to gather the data. You could use the Percona Toolkit tools, they have some great ones for really drilling down into the knitty gritty of MySQL usage.
Don't forget that there are logs to check. Specifically you should check on the slow log. To find if and where that is being stored, you can run following:
First make sure it's turned on:
MySQL> SHOW VARIABLES LIKE 'slow_query_log';
Then check where it's located on your file system:
MySQL> SHOW VARIABLES LIKE 'slow_query_log_file';
Using the Percona tool pt-query-digest on the slow log has proved to be helpful for me.
It gives a cohesive list of what queries are using the most time (which usually means resource intensive queries). You can view the usage of the tool here: PT-Query-Digest
You may find some really slow queries, if so I would evaluate how they are being used and see if adding an index, or optimizing them is needed.
Once you gather these data pieces you should have a good picture of what's happening to your MySQL instance. Of course there's many more tools and techniques, but I find these to be sufficient to form a good baseline of performance.