How to view MySQL links and kill abnormal links

How to view MySQL links and kill abnormal links

Preface:

During database operation and maintenance, we often pay attention to the connection status of the database, such as how many links there are in total, how many active links there are, whether there are any links that take too long to execute, etc. Various database anomalies can also be indirectly reflected through the link status. Especially when the database is deadlocked or severely stuck, we should first check whether there are abnormal links in the database and kill these abnormal links. This article will mainly introduce how to view database links and how to kill abnormal links.

1. View database link

The most commonly used statement to view database links is show processlist, which can view the status of threads existing in the database. Ordinary users can only view links initiated by the current user, while users with PROCESS global permissions can view links of all users.

The Info field in the show processlist result only displays the first 100 characters of each statement. If you need to display more information, you can use show full processlist. Similarly, you can view the database connection status information by viewing the information_schema.processlist table.

# Ordinary users can only see the links initiated by the current usermysql> select user();
+--------------------+
| user() |
+--------------------+
| testuser@localhost |
+--------------------+
1 row in set (0.00 sec)

mysql> show grants;
+----------------------------------------------------------------------+
| Grants for testuser@% |
+----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'testuser'@'%' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `testdb`.* TO 'testuser'@'%' |
+----------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show processlist;
+--------+----------+-----------+--------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+----------+-----------+--------+---------+------+----------+------------------+
| 769386 | testuser | localhost | NULL | Sleep | 201 | | NULL |
| 769390 | testuser | localhost | testdb | Query | 0 | starting | show processlist |
+--------+----------+-----------+--------+---------+------+----------+------------------+
2 rows in set (0.00 sec)

mysql> select * from information_schema.processlist;
+--------+----------+-----------+--------+---------+------+-----------+----------------------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+--------+----------+-----------+--------+---------+------+-----------+----------------------------------------------+
| 769386 | testuser | localhost | NULL | Sleep | 210 | | NULL |
| 769390 | testuser | localhost | testdb | Query | 0 | executing | select * from information_schema.processlist |
+--------+----------+-----------+--------+---------+------+-----------+----------------------------------------------+
2 rows in set (0.00 sec)

# After granting PROCESS permission, you can see the links of all usersmysql> grant process on *.* to 'testuser'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants;
+----------------------------------------------------------------------+
| Grants for testuser@% |
+----------------------------------------------------------------------+
| GRANT PROCESS ON *.* TO 'testuser'@'%' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `testdb`.* TO 'testuser'@'%' |
+----------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show processlist;
+--------+----------+--------------------+--------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+----------+--------------------+--------+---------+------+----------+------------------+
| 769347 | root | localhost | testdb | Sleep | 53 | | NULL |
| 769357 | root | 192.168.85.0:61709 | NULL | Sleep | 521 ​​| | NULL |
| 769386 | testuser | localhost | NULL | Sleep | 406 | | NULL |
| 769473 | testuser | localhost | testdb | Query | 0 | starting | show processlist |
+--------+----------+--------------------+--------+---------+------+----------+------------------+
4 rows in set (0.00 sec)

Through the results of show processlist, we can clearly understand the detailed information of each thread link. The meaning of the specific fields is relatively easy to understand. The following is a detailed explanation of what each field represents:

  • Id: It is the unique identifier of this link. You can kill this link by using the kill command plus this Id value.
  • User: refers to the user name that initiated this link.
  • Host: records the IP and port number of the client that sends the request, so you can locate which process of the client sends the request.
  • db: On which database the command is currently executed. If no database is specified, this value is NULL.
  • Command: refers to the command that the thread link is executing at the moment.
  • Time: Indicates the time that the thread link is in the current state.
  • State: The state of the thread, corresponding to Command.
  • Info: records the specific statements executed by the thread.

When there are too many database links, filtering useful information becomes a hassle, for example, we only want to check the link of a certain user or a certain status. At this time, using show processlist will find some information we don't need. At this time, using information_schema.processlist to filter will become much easier. The following shows several common filtering requirements:

# View only the link information of a certain ID select * from information_schema.processlist where id = 705207;

# Filter out the links of a certain user select * from information_schema.processlist where user = 'testuser';

# Filter out all non-idle links select * from information_schema.processlist where command != 'Sleep';

# Filter out links with idle time of more than 600 seconds select * from information_schema.processlist where command = 'Sleep' and time > 600;

# Filter out links in a certain state select * from information_schema.processlist where state = 'Sending data';

# Filter the links of a certain client IP select * from information_schema.processlist where host like '192.168.85.0%';

2. Kill the database link

If a database link is abnormal, we can kill the link through the kill statement. The standard syntax of kill is: KILL [CONNECTION | QUERY] processlist_id;

KILL allows optional CONNECTION or QUERY modifiers:

  • KILL CONNECTION is the same as KILL without any modifiers, it terminates the process's associated links.
  • KILL QUERY terminates the statement currently executing on a link, but leaves the link itself intact.

The ability to kill links depends on the SUPER privilege:

  • If you do not have the SUPER privilege, you can only kill the links initiated by the current user.
  • A user with the SUPER privilege can kill all links.

When you encounter an emergency and need to kill links in batches, you can get the kill statement by splicing SQL and then execute it. This will be much more convenient. Here are some SQL statements that may be used to kill links:

# Kill the links that are idle for more than 600 seconds and concatenate them to get the kill statement select concat('KILL ',id,';') from information_schema.`processlist`
 where command = 'Sleep' and time > 600;

# Kill the links in a certain state and concatenate them to get the kill statement select concat('KILL ',id,';') from information_schema.`processlist`
 where state = 'Sending data';

select concat('KILL ',id,';') from information_schema.`processlist`
 where state = 'Waiting for table metadata lock';

# Kill a link initiated by a user and concatenate the kill statement select concat('KILL ',id,';') from information_schema.`processlist`
  user = 'testuser';

Here is a reminder that the kill statement must be used with caution! Especially when this link executes an update statement or a table structure change statement, killing the link may require a relatively long rollback operation.

Summarize:

This article explains how to view and kill database links. If you suspect that there is a problem with the database in the future, you can check the database link status as soon as possible.

The above is the details of how to view MySQL links and kill abnormal links. For more information about viewing MySQL links and killing abnormal links, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • The reason why MySQL manually registers the binlog file and causes master-slave abnormalities
  • MySQL database connection exception summary (worth collecting)
  • How to fix abnormal startup of mysql5.7.21
  • Sharing of experience on repairing MySQL innodb exceptions
  • MySQL definition and exception handling details
  • Some basic exception handling tutorials in MySQL stored procedures
  • Analyzing a MySQL abnormal query case
  • A Brief Analysis of MySQL Exception Handling
  • Analyze several common solutions to MySQL exceptions

<<:  Implementation of element shuttle frame performance optimization

>>:  The corresponding attributes and usage of XHTML tags in CSS

Recommend

CSS to achieve chat bubble effect

1. Rendering JD Effect Simulation Effect 2. Princ...

MySQL table return causes index invalidation case explanation

Introduction When the MySQL InnoDB engine queries...

CSS and HTML and front-end technology layer diagram

Front-end technology layer (The picture is a bit e...

Solution to elementui's el-popover style modification not taking effect

When using element-ui, there is a commonly used c...

React implements dynamic pop-up window component

When we write some UI components, if we don't...

Linux general java program startup script code example

Although the frequency of starting the shell is v...

mysql8.0.23 msi installation super detailed tutorial

1. Download and install MySql Download MySql data...

Introduction to Spark and comparison with Hadoop

Table of contents 1. Spark vs. Hadoop 1.1 Disadva...

Implementation script for scheduled database backup in Linux

Table of contents Scenario: The server database n...

Detailed explanation of the six common constraint types in MySQL

Table of contents Preface 1.notnull 2. unique 3. ...

Summarize how to optimize Nginx performance under high concurrency

Table of contents Features Advantages Installatio...

JS achieves five-star praise case

This article shares the specific code of JS to ac...