swoole-src icon indicating copy to clipboard operation
swoole-src copied to clipboard

MySQL server has gone away due to Connection timed out - prepare result

Open DevilSerj opened this issue 1 year ago • 3 comments

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.
go(function () {
        $config = Config::get('database.connections.mysql');
        $db = new MySQL();
        $server = array(
            'host' => $config['host'],
            'port' => $config['port'],
            'user' => $config['username'],
            'password' => $config['password'],
            'database' => $config['database'],
        );

        echo "connect\n";
        $ret1 = $db->connect($server);
        var_dump($ret1);

        echo "prepare [1]\n";
        $stmt1 = $db->prepare('SELECT * FROM users WHERE id=?', 5);
        var_dump($stmt1);
        if ($stmt1 == false)
        {
            var_dump($db->errno, $db->error);
        }
    });
  1. What did you expect to see? A positive result prepare

  2. What did you see instead? connect bool(true) prepare [1] bool(false) int(2006) string(77) "SQLSTATE[HY000] [2006] MySQL server has gone away due to Connection timed out"

  3. What version of Swoole are you using (show your php --ri swoole)? PHP | 7.4.33 | Swoole | 4.8.13 |

  4. What is your machine environment used (show your uname -a & php -v & gcc -v) ? Linux

DevilSerj avatar Sep 26 '24 20:09 DevilSerj

Code Try to reconnect to the database.

https://github.com/swoole/swoole-src/issues/4131#issuecomment-815353916

more info : https://github.com/jingjingxyk/swoole-cli/issues/125

jingjingxyk avatar Sep 28 '24 00:09 jingjingxyk

Code Try to reconnect to the database.

#4131 (comment)

more info : jingjingxyk/swoole-cli#125

Of course I understand this and I have tried many different options and reconnect as well. But it doesn't work for me, the connection is bigger, I easily use the query method and it works. I have a task that creates coroutines and I need to have a separate connection within the coroutine and I was thinking of doing it using Swoole/Coroutine/Mysql as I expected it to work better. But it doesn't work for me and I used a normal pdo and just create a meal through it.

Maybe I should update swoole and php but now I can't do it yet.

DevilSerj avatar Sep 29 '24 22:09 DevilSerj

Are you using connection pool outside of that example you gave above? Maybe you've hit your server limit and that's why you can't create additional connections?

Draghmar avatar Oct 17 '24 11:10 Draghmar

Check database configuration, 2006 indicates inability to connect to MySQL server

matyhtf avatar Nov 07 '25 03:11 matyhtf