MySQL 查看链接及杀掉异常链接的方法
前言:
在数据库运维过程中,我们时常会关注数据库的链接情况,比如总共有多少链接、有多少活跃链接、有没有执行时间过长的链接等。数据库的各种异常也能通过链接情况间接反应出来,特别是数据库出现死锁或严重卡顿的时候,我们首先应该查看数据库是否有异常链接,并杀掉这些异常链接。本篇文章将主要介绍如何查看数据库链接及如何杀掉异常链接的方法。
1.查看数据库链接
查看数据库链接最常用的语句就是 show processlist 了,这条语句可以查看数据库中存在的线程状态。普通用户只可以查看当前用户发起的链接,具有 PROCESS 全局权限的用户则可以查看所有用户的链接。
show processlist 结果中的 Info 字段仅显示每个语句的前 100 个字符,如果需要显示更多信息,可以使用 show full processlist 。同样的,查看 information_schema.processlist 表也可以看到数据库链接状态信息。
# 普通用户只能看到当前用户发起的链接 mysql> 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) # 授予了PROCESS权限后,可以看到所有用户的链接 mysql> 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)
您可能感兴趣的文章
- 05-31MySQL中的 inner join 和 left join的区别解析(小结果集驱动大结果集)
- 05-31MySQL索引失效十种场景与优化方案
- 05-31MYSQL 高级文本查询之regexp_like和REGEXP详解
- 05-31MySQL获取binlog的开始时间和结束时间(最新方法)
- 05-31MySQL索引查询的具体使用
- 05-31基于MySQL和Redis扣减库存的实践
- 05-31关于MySQL的存储过程与存储函数
- 05-31MySQL实战文章(非常全的基础入门类教程)
- 05-31MySQL Flink Watermark实现事件时间处理的关键技术
- 05-31MySQL Flink实时流处理的核心技术之窗口机制
阅读排行
推荐教程
- 05-30Navicat for MySQL 11注册码激活码汇总
- 05-27Mysql误删数据快速恢复
- 05-31VS2022连接数据库MySQL并进行基本的表的操作指南
- 05-30解决seata不能使用mysql8版本的问题方法
- 05-30MYSQL字符集设置的方法详解(终端的字符集)
- 05-30解决MySQL启动报错:ERROR 2003 (HY000): Can't con
- 05-30关于Mysql-connector-java驱动版本问题总结
- 11-22mac下安装mysql忘记密码的修改方法
- 05-30MySQL中的隐藏列的具体查看
- 11-22mysql exists与not exists实例详解