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

MySQL process control IF(), IFNULL(), NULLIF(), ISNULL() functions

In MySQL, you can use IF(), IFNULL(), NULLIF(), a...

Let's take a look at some powerful operators in JavaScript

Table of contents Preface 1. Null coalescing oper...

Docker Detailed Illustrations

1. Introduction to Docker 1.1 Virtualization 1.1....

How does Vue3's dynamic components work?

Table of contents 1. Component Registration 1.1 G...

Html/Css (the first must-read guide for beginners)

1. Understanding the meaning of web standards-Why...

Solution to the ineffective margin of div nested in HTML

Here's a solution to the problem where margin...

Understanding the MySQL query optimization process

Table of contents Parsers and preprocessors Query...

MySQL 8.0.13 installation and configuration tutorial under CentOS7.3

1. Basic Environment 1. Operating system: CentOS ...

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

How to upload projects to Code Cloud in Linux system

Create a new project test1 on Code Cloud Enter th...