swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.25k stars 3.16k forks source link

blocking on get data with pdo from db #5273

Closed parsibox closed 1 month ago

parsibox commented 2 months ago

hi i have 10 Coroutine and each of them have separate conenction to mysql with pdo in each Coroutine i have a select to mysql and fetch data

$processList = $class_db_new->query( $my_query);
        $processList->execute();
        $processList = $processList->fetchAll();
        foreach ($processList as $mysql_r_l) {

}

when my query have more than 30000 result mysql say PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

my time_wait and other config on mysql is set but we get error on this step i think when we use fetchAll or foreach or some other function that take long time whole of our Coroutine blocked for that process is it true? if it is true how can we fix this problem?

NathanFreeman commented 2 months ago

Do you instantiate a new database connection for each request or do you retrieve it from a database connection pool? If you are retrieving the database connection from a connection pool, you may need to attempt a reconnection when disconnected.

parsibox commented 2 months ago

Do you instantiate a new database connection for each request or do you retrieve it from a database connection pool? If you are retrieving the database connection from a connection pool, you may need to attempt a reconnection when disconnected.

i use separate pdo connection in each Coroutine i think some function like ksort , sort or fetch big data from mysql blocked whole my file

moroswitie commented 2 months ago

when my query have more than 30000 result mysql say PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

my time_wait and other config on mysql is set but we get error on this step i think when we use fetchAll or foreach or some other function that take long time whole of our Coroutine blocked for that process is it true? if it is true how can we fix this problem?

It sounds like you need to increase "max_allowed_packet" in your mysql config my.ini

jingjingxyk commented 2 months ago

Try to reconnect to the database. reference

  1. https://github.com/swoole/swoole-src/issues/4131#issuecomment-815353916
  2. https://github.com/swoole/swoole-src/issues/2041
  3. https://github.com/swoole/swoole-wiki/blob/master/doc/15.11%20-%20MySQL%E7%9A%84%E8%BF%9E%E6%8E%A5%E6%B1%A0%E3%80%81%E5%BC%82%E6%AD%A5%E3%80%81%E6%96%AD%E7%BA%BF%E9%87%8D%E8%BF%9E.md
  4. https://github.com/swoole/swoole-src/issues/4141
  5. https://github.com/swoole/swoole-src/issues/5143

最佳的方案是进行断线重连

matyhtf commented 1 month ago

You should catch this exception and reconnect to the MySQL server after disconnection.