终止空闲的mysql连接 [英] Terminating idle mysql connections

查看:574
本文介绍了终止空闲的mysql连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到许多连接处于打开状态,并且保持了很长一段时间(例如5分钟)的空闲状态.

I see a lot of connections are open and remain idle for a long time, say 5 minutes.

有什么解决方案可以在不重新启动mysql服务的情况下从服务器终止/关闭它吗?

Is there any solution to terminate / close it from server without restarting the mysql service?

我正在维护旧版PHP系统,无法关闭为执行查询而建立的连接.

I am maintaining a legacy PHP system and can not close the connections those are established to execute the query.

我应该将my.cnf文件中的超时值减少为默认的8小时吗?

Should I reduce the timeout values in my.cnf file those defaults to 8 hours?

# default 28800 seconds

interactive_timeout=60
wait_timeout=60

推荐答案

手动清理:

您可以杀死进程ID.

Manual cleanup:

You can KILL the processid.

mysql> show full processlist;
+---------+------------+-------------------+------+---------+-------+-------+-----------------------+
| Id      | User       | Host              | db   | Command | Time  | State | Info                  |
+---------+------------+-------------------+------+---------+-------+-------+-----------------------+
| 1193777 | TestUser12 | 192.168.1.11:3775 | www  | Sleep   | 25946 |       | NULL                  |
+---------+------------+-------------------+------+---------+-------+-------+-----------------------+

mysql> kill 1193777;

但是:

  • php应用程序可能会报告 错误(或网络服务器,请检查 错误日志)
  • 不修复未损坏的内容-如果您的连接不短,只需 离开他们.
  • the php application might report errors (or the webserver, check the error logs)
  • don't fix what is not broken - if you're not short on connections, just leave them be.

或者您通过在上设置较短的超时来配置mysql服务器wait_timeout interactive_timeout

Or you configure your mysql-server by setting a shorter timeout on wait_timeout and interactive_timeout

mysql> show variables like "%timeout%";
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| connect_timeout          | 5     |
| delayed_insert_timeout   | 300   |
| innodb_lock_wait_timeout | 50    |
| interactive_timeout      | 28800 |
| net_read_timeout         | 30    |
| net_write_timeout        | 60    |
| slave_net_timeout        | 3600  |
| table_lock_wait_timeout  | 50    |
| wait_timeout             | 28800 |
+--------------------------+-------+
9 rows in set (0.00 sec)

设置为:

set global wait_timeout=3;
set global interactive_timeout=3;

(并且还在服务器重新启动时在您的配置文件中进行了设置)

(and also set in your configuration file, for when your server restarts)

但是,您正在处理症状而不是根本原因-为什么打开连接?如果PHP脚本完成,它们是否应该关闭?确保您的网络服务器未使用连接池...

But you're treating the symptoms instead of the underlying cause - why are the connections open? If the PHP script finished, shouldn't they close? Make sure your webserver is not using connection pooling...

这篇关于终止空闲的mysql连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆