workerman icon indicating copy to clipboard operation
workerman copied to clipboard

MySQL server has gone away when in daemon mode?

Open kenw232 opened this issue 2 years ago • 5 comments

When I run my server normally with "php80 server/server.php start" it works fine. I have a database call in my onMessage() function:

$wsWorker->onMessage = function ($connection, $data)
    {
        global $pdoConnection;
        $sqlCallCheckResult = $pdoConnection -> prepare("select...

This is fine and work sell. As soon as I run it in daemon mode with "php80 server/server.php -d start" it server repeatedly fails with: "MySQL server has gone away in...". See attached full error. Does anyone know why? I do nothing different at all except add -d for daemon mode.

error.txt

kenw232 avatar Dec 23 '21 15:12 kenw232

I got it. move the database connect stuff into the onMessage now too...

kenw232 avatar Dec 23 '21 18:12 kenw232

It is necessary to close the mysql connection and renew it x times. In fact, by launching a daemon or debug, the connection with the database will remain open and mysql will close it automatically after a certain time. So, to solve this problem, you will have to renew the connection with mysql regularly.

ShockedPlot7560 avatar Dec 24 '21 09:12 ShockedPlot7560

Just create a timer that's sequentially pinging the database.

Guess that's possible with workerman timers.

cayolblake avatar Mar 03 '22 22:03 cayolblake

Depending on the version, this will not work every time. Mysql8 closes connections after x amount of time, even if there were requests so it will not work if you just send a ping.

ShockedPlot7560 avatar Mar 03 '22 22:03 ShockedPlot7560

It is possible to check the connection and re-init it before you do an actual request. Use wrapper like this:

function getPdoConnection() {
    global $config;
    global $pdoConnection;
    
    try {
        $pdoConnection->query("SELECT 1");
    } catch (\PDOException $e) {
        $pdoConnection = new \PDO($config->dsn, $config->user, $config->password, $config->options);
    }
    
    return $pdoConnection;
}

Qclanton avatar Aug 23 '22 17:08 Qclanton