终止空闲的 mysql 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4284194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:48:58  来源:igfitidea点击:

Terminating idle mysql connections

mysqltimeoutkillwaitpython-idle

提问by shantanuo

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

我看到很多连接都打开并长时间保持空闲状态,比如 5 分钟。

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

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

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

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

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

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

# default 28800 seconds

interactive_timeout=60
wait_timeout=60

回答by Konerak

Manual cleanup:

手动清理:

You can KILL the processid.

你可以杀死进程ID。

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;

But:

但:

  • 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.
  • php 应用程序可能会报告错误(或网络服务器,检查错误日志)
  • 不要修复没有损坏的东西- 如果你不缺少连接,就让它们保持原样。

Automatic cleaner service ;)

自动清洁服务;)

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

或者你通过设定一个较短的超时配置MySQL服务器wait_timeoutinteractive_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 with:

设置:

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

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

(也在你的配置文件中设置,当你的服务器重新启动时)

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...

但是你是在治疗症状而不是根本原因——为什么这些联系是开放的?如果 PHP 脚本完成,它们不应该关闭吗?确保您的网络服务器没有使用连接池...

回答by guykaplan

I don't see any problem, unless you are not managing them using a connection pool.

我没有看到任何问题,除非您没有使用连接池管理它们。

If you use connection pool, these connections are re-used instead of initiating new connections. so basically, leaving open connections and re-use them it is less problematic than re-creating them each time.

如果使用连接池,这些连接将被重用,而不是启动新连接。所以基本上,留下打开的连接并重新使用它们比每次重新创建它们的问题要少。