gpt4 book ai didi

php - mysql 进程保持事件状态 - 为什么?

转载 作者:行者123 更新时间:2023-11-30 21:44:17 26 4
gpt4 key购买 nike

我遇到了 mysql 进程保持事件状态的问题。

显示进程列表;返回进程列表。存在几个小时的进程 - 根据进程 ID、主机(端口)。

似乎进程执行查询 (SELECT) 而不是休眠,再次执行查询 - 并且它再次发生。 PROCESSLIST 中显示的“时间”在每次 sleep 后不断重置。

如何让Mysql自动杀掉这样的进程?我想这样的进程在 PHP 脚本超时后仍然存在(它还执行 CURL 任务)。

[编辑 - 回答评论]

查询: SELECT COUNT(*) FROM keywords WHERE updated < '2018-05-03 00:00:00' AND status = '0' AND active = '1;

列:更新 (DATETIME)、状态 (INT) 和事件 (BOOL) 是索引的。

“关键字”表中有约 120K 条记录。

我的 PHP 脚本通过 PDO 连接到 MySQL。

我还设置了以下变量:

interactive_timeout = 180
wait_timeout = 180

最佳答案

您所描述的行为仅适用于 persistent connections .持久连接允许 PHP 在凭证匹配时重新使用以前的数据库连接。虽然有 downsides ,持久连接不一定是坏事,也没有必要手动杀死它们。

如果你想禁用持久连接,那么你必须在你的代码中找到初始化数据库连接的地方。它应该看起来像这样:

<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::ATTR_PERSISTENT => true
));
?>

找到它后,删除 PDO::ATTR_PERSISTENT 选项或将其设置为 false:

<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::ATTR_PERSISTENT => false
));
?>

禁用持久连接后,数据库 session 将无法在 PHP 脚本退出后继续存在。正如 docs 所述:

The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.

关于php - mysql 进程保持事件状态 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com