Question

So I have this problem when I run a script with PHP PDO that I'm not able to resolve. My script is indexing informations from different databases on different server (mostly Select queries).

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)

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

  • Modifiying the max_connections to allow more in case I forgot to kill some already existing connections.

  • Checking for a problem on the network

  • Many other little things such a connection from the host to the remote sql server.

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;
}
Était-ce utile?

La solution 2

I found that the reason for this error was because there was a tcp connection timeout. 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. Of course, I know that the real solution will be to read each of the queries, and try to optimize those :)

Autres conseils

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?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top