终止空闲的 mysql 连接

发布于 2024-10-04 03:50:01 字数 261 浏览 0 评论 0 原文

我看到很多连接处于打开状态并且长时间处于空闲状态,例如 5 分钟。

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

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

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

# default 28800 seconds

interactive_timeout=60
wait_timeout=60

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

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

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

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

# default 28800 seconds

interactive_timeout=60
wait_timeout=60

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十六岁半 2024-10-11 03:50:01

手动清理:

您可以杀死 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 应用程序可能会报告
    错误(或网络服务器,检查
    错误日志)
  • 不要修复未损坏的 - 如果您的连接不短,只需
    让他们去吧。

自动清理服务;)

或者您可以通过在 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 global wait_timeout=3;
set global interactive_timeout=3;

也可以在配置文件中设置,以便服务器重新启动时使用)

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

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;

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.

Automatic cleaner service ;)

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

美人骨 2024-10-11 03:50:01

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文