MySQL query is not executed, MySQL server has gone away

Problem: I was trying to make a mysql query after a function, that was taking a lot of time to load, and my query was not being executed. In addition I was receiving the error: “MySQL server has gone away”. This was caused because the mysql server was timed out and closed the connection.

The solution in this case was to ping the server and check if there is any active connection otherwise try to reconnect.

if (!$mysqli->ping())
     $db = new mysqli($host, $username, $password, $dbname, (int)$port, $socket);

The default behavior for ping() is to try and reconnect automatically, but in my case it didn’t work out.

References:
http://dev.mysql.com/doc/refman/5.0/en/gone-away.html
http://dev.mysql.com/doc/refman/5.0/en/mysql-ping.html
http://www.php.net/manual/en/mysqli.connect.php
http://www.php.net/manual/en/mysqli.ping.php

This entry was posted in Solutions and tagged , , , . Bookmark the permalink.

4 Responses to MySQL query is not executed, MySQL server has gone away

  1. Dominic says:

    You share interesting things here. I think that your website can go viral easily, but you must give
    it initial boost and i know how to do it, just type in google for – wcnu traffic increase

  2. Skip says:

    Forums … Forums.. Fomt7s!!Iu&#821r;s not easy to manage and moderate and edit a forum, this is true, I like the idea of separating forums and other stuff from your blog brand! thanks for the informative post!

  3. Stan says:

    Wouldn’t it be $mysqli = on the second line…(since you check !$mysqli->ping())?

  4. Stan says:

    Modified mine to

    if (!mysqli_ping($mysqli))
    $mysqli = new mysqli($DBServer, $DBUser, $DBPassword, $DBName, (int)$port, $socket);

    Seems to work so far…also set PHP variable

    ini_set(‘mysqli.connect_timeout’, 300);
    ini_set(‘mysqli.reconnect’, 1);

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>