简体   繁体   English

MySQL:服务器已消失

[英]MySQL : Server has gone away

So I have this problem when I run a script with PHP PDO that I'm not able to resolve. 因此,当我使用无法解决的PHP PDO运行脚本时,就会遇到这个问题。 My script is indexing informations from different databases on different server (mostly Select queries). 我的脚本正在索引来自不同服务器上不同数据库的信息(主要是Select查询)。

When I was developing the script, all the databases were on the same server and everything was running fine. 当我开发脚本时,所有数据库都在同一台服务器上,并且一切运行正常。 Now that I move the databases on three different server, I get this dreaded error. 现在,我在三个不同的服务器上移动了数据库,我得到了这个可怕的错误。

So this is what happens everytime I run my script : 所以这是每次我运行脚本时发生的情况:

The script runs fine, until a point (and it crashes there every time) where it runs this simple query : 该脚本运行良好,直到运行此简单查询的一个点(每次都崩溃):

(Note, the script is in PHP and I'm using PHP PDO to access my DB and work with it) (请注意,该脚本在PHP并且我正在使用PHP PDO访问我的数据库并使用它)

SELECT *
FROM a_table
WHERE a_column = a_value.

Then it crashes and I get this error : 然后它崩溃了,我得到这个错误:

[a_date] - trunk - FATAL - Unmanaged Exception of type 'App_Exception'
File: /.../.../.../a_php_file.php
Line: a_line
Message: Error executing query.
Info:
Array
(
    [0] => HY000
    [1] => 2006
    [2] => MySQL server has gone away
)

Trace:
 #0 /.../.../.../a_php_file.php(124): App_something::a_metho('a_variable...')

I've searched for a long time and still haven't found a solution. 我已经搜索了很长时间,但仍然没有找到解决方案。

I tried: 我试过了:

  • Modifiying the max_allowed_packed in /etc/my.cnf (on the sql on which occurs the crash) to set it from 16M to 64M (even though the results the query return is really not that big). 修改/etc/my.cnf中的max_allowed_pa​​cked(在发生崩溃的SQL上)以将其从16M设置为64M(即使查询返回的结果确实不是那么大)。

  • Modifiying the max_connections to allow more in case I forgot to kill some already existing connections. 修改max_connections以允许更多操作,以防万一我忘记杀死一些已经存在的连接。

  • Checking for a problem on the network 检查网络上的问题

  • Many other little things such a connection from the host to the remote sql server. 其他许多小事情,例如从主机到远程sql服务器的连接。

What is really weird is that it runs fine if I run the query alone and it also works fine if all the databases are on the same server. 真正奇怪的是,如果我单独运行查询,它运行正常,如果所有数据库都在同一服务器上,它也可以正常运行。

I'm really lost at this point and have no idea of where the problem could come from and that's why I'm coming to ask for help or just a hint. 在这一点上,我真的迷失了方向,不知道问题可能来自何处,这就是为什么我要寻求帮助或只是一个提示。

This is the method that crashes at the exception: 这是在异常情况下崩溃的方法:

public static function my_method($my_mysql_WHERE_variable)
{
    //======================================
    // cached?
    //======================================
    if(isset(self::$_table1[$my_mysql_WHERE_variable])) {
      return self::$_table1[$my_mysql_WHERE_variable];
    }

    $pdo = my_App::getConnection();

    $query = "SELECT *
              FROM
                a_table
              WHERE
                a_column = :my_mysql_WHERE_variable";
    $stmt = $pdo->prepare($query);

    if($stmt === false) { 
        **---------------IT CRASHES HERE-----------**
        throw new my_App_Exception("Error executing query.\nInfo:\n" . print_r($pdo->errorInfo(), true)); 
    }
    .
    .
    .
    return $a_value;
}

You are probably having a case where you a having a DB connection problem. 您可能遇到数据库连接问题的情况。 Are you accidentally closing the DB connection to this DB somewhere else? 您是否在其他地方意外关闭了与此数据库的数据库连接? Did you not reference the correct connection for the query you are trying to run? 您没有为要运行的查询引用正确的连接吗?

I found that the reason for this error was because there was a tcp connection timeout. 我发现此错误的原因是因为tcp连接超时。 I still can't really explain myself why it timeouts even if the query are actually pretty short, but I managed to fix my problem by modifiying /etc/sysctl.conf and setting higher values for the keepalives. 即使查询实际上很短,我仍然无法真正解释自己为什么会超时,但是我设法通过修改/etc/sysctl.conf并为keepalive设置更高的值来解决问题。 Of course, I know that the real solution will be to read each of the queries, and try to optimize those :) 当然,我知道真正的解决方案是读取每个查询,并尝试优化那些:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM