3

I've noticed that if I execute a long running mysql query with php using mysql_query() (I know I'm not supposed to use that) and then the php process gets killed then the query continues to run on the mysql server. This is not a persistent connection. The connection is made with:

$db = mysql_connect($host, $login, $pass, false);
$sql = 'SELECT COUNT(*) FROM `huge_table`';
$result = mysql_query($sql, $db);

For example, let's say I have a 1 billion row table and a php process does this for some reason:

SELECT COUNT(*) FROM `huge_table`

And then it times out (say because I'm running php-fpm with request_terminate_timeout=5), so it kills the process after 5 seconds to make sure it doesn't hog things.

Eventhough the process is killed, the query still runs on mysql even far after wait_timeout.

Is there anyway to make sure that if the php process exits for whatever reason it also kills any running queries that it made?

I'm using tokudb 5.5.38-tokudb-7.1.7-e which is mysql 5.5.38

4

3 回答 3

3

crickeys,当 PHP 脚本开始执行并到达执行 MySQL 查询的部分时,该查询将移交给 MySQL。查询的控制权不再掌握在 PHP 手中......此时 PHP 只是等待 MySQL 的响应,然后它可以继续。杀死 PHP 脚本不会影响 MySQL 查询,因为查询是 MySQL 的事情。

换句话说,PHP 来到门前,敲门,交出货物,等待您回复,这样他就可以上路了。射击他不会影响门后发生的事情。

你可以运行这样的东西来检索运行时间最长的进程并杀死它们:

<?php
    $con=mysqli_connect("example.com","peter","pass","my_db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SHOW FULL PROCESSLIST");

while($row = mysqli_fetch_array($result)) {
if ($row["Time"] > $max_excution_time ) {
    $sql="KILL ".$row["Id"];
    mysql_query($sql);
    }   

}

mysqli_close($con); ?>
于 2014-07-23T05:18:47.070 回答
0

您可以使用KILL.

  • KILL CONNECTION与没有修饰符相同KILL:它终止与给定 thread_id 关联的连接。
  • KILL QUERY终止连接当前正在执行的语句,但保持连接本身不变。

你应该KILL QUERY在关机事件中,然后做一个mysqli_close().


您可能会从这个关于超时的问题中获得一些有价值的信息:客户端超时,而 MySQL 查询仍在运行?

于 2014-07-23T04:31:08.913 回答
0

那么你可以使用析构函数来调用

mysql_close();功能。我希望我明白你的问题...

于 2014-07-23T04:09:40.540 回答