workerman
workerman copied to clipboard
MySQL server has gone away when in daemon mode?
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.
I got it. move the database connect stuff into the onMessage now too...
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.
Just create a timer that's sequentially pinging the database.
Guess that's possible with workerman timers.
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.
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;
}