search

Home  >  Q&A  >  body text

php - Create a mysql connection in onWorkerStart in Swoole, how to keep the long connection continuous?

$db   = new swoole_mysql;
$serv = new swoole_http_server("127.0.0.1", 9501);

$serv->on('WorkerStart', function(swoole_server $server) {
    global $db;
    $db->connect($db->server, function($db, $r) {
        if ($r === false) {
            var_dump($db->connect_errno, $db->connect_error);
            die;
        }
    });
});
$serv->on('Request', function($request, $response) {
        global $db;
        $db->query($sql, function(swoole_mysql $db, $r) {
        ........
        });
}

After a while, the mysql connection will be closed. How can I never close it?

滿天的星座滿天的星座2806 days ago708

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-05-19 10:10:00

    Disconnection and reconnection or heartbeat mechanism will work

    reply
    0
  • Cancelreply