doucigua0278 2018-05-02 23:10
浏览 114

mysql进程保持活力 - 为什么?

I have a problem of mysql processes staying alive.

SHOW PROCESSLIST; returns a list of processes. There are processes existing for hours - according to process ID, host (port).

It seems like the process performs a query (SELECT) than sleeps, performs the query again - and it happens all over again. The "time" shown in PROCESSLIST keeps resetting after each sleep.

How to make Mysql automatically kill such processes? I suppose that such a process stays alive after a PHP script timeout (it also performs a CURL task).

[EDIT - to answer comments]

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

columns: updated (DATETIME), status (INT) and active (BOOL) are INDEXED.

There is ~120K records in the table "keywords".

My PHP script is connecting to MySQL via PDO.

I have also set the following variables:

interactive_timeout = 180 wait_timeout = 180

  • 写回答

1条回答 默认 最新

  • dpzjl68484 2018-05-03 02:16
    关注

    The behavior you have described is possible only with persistent connections. Persistent connections allow PHP to re-use a previous database connection when the credentials match. While there are downsides, persistent connections are not necessarily a bad thing and it is not necessary to manually kill them off.

    If you want to disable persistent connections, then you must find the place in your code where the database connection is initialized. It should look something like this:

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

    Once you've located it, either remove the PDO::ATTR_PERSISTENT option or set it to false:

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

    After you've disabled persistent connections, it will be impossible for the database session to persist after the exit of the PHP script. As stated by the 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计一个单管共射放大电路
  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架