swoole / swoole-src

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

Error while sending QUERY packet #1406

Closed zhang988925 closed 6 years ago

zhang988925 commented 6 years ago

Please answer these questions before submitting your issue. Thanks!

  1. What did you do? If possible, provide a recipe for reproducing the error.

Yii2 console 投放任务,连接数据库(模型)处理数据

  1. What did you expect to see?

我找遍了百度和谷歌上的各种问答 都无法解决问题

  1. What did you see instead?

  2. What version of Swoole are you using (php --ri swoole)? 1.9.21

  3. What is your machine environment used (including version of kernel & php & gcc) ? CentOS Linux release 7.4.1708 (Core)

PHP Fatal error:  Uncaught yii\base\ErrorException: Error while sending QUERY packet. PID=1049 in /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Transaction.php:128
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'Error while sen...', '/data/wwwroot/t...', 128, Array)
#1 /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Transaction.php(128): PDO->beginTransaction()
#2 /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Connection.php(721): yii\db\Transaction->begin(NULL)
#3 /data/wwwroot/tanhui/public_html/console/components/MatchHandle.php(37): yii\db\Connection->beginTransaction()
#4 /data/wwwroot/tanhui/public_html/console/controllers/SwooleController.php(83): console\components\MatchHandle->run()
#5 [internal function]: console\controllers\SwooleController->onTask(Object(swoole_server), 200, 0, Array)
#6 /data/wwwroot/tanhui/public_html/console/controllers/SwooleController.php(38): swoole_server->start()
#7 [internal function]: console\controllers\SwooleController->actionIndex( in /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Transaction.php on line 128
Error: Uncaught yii\base\ErrorException: Error while sending QUERY packet. PID=1049 in /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Transaction.php:128
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'Error while sen...', '/data/wwwroot/t...', 128, Array)
#1 /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Transaction.php(128): PDO->beginTransaction()
#2 /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Connection.php(721): yii\db\Transaction->begin(NULL)
#3 /data/wwwroot/tanhui/public_html/console/components/MatchHandle.php(37): yii\db\Connection->beginTransaction()
#4 /data/wwwroot/tanhui/public_html/console/controllers/SwooleController.php(83): console\components\MatchHandle->run()
#5 [internal function]: console\controllers\SwooleController->onTask(Object(swoole_server), 200, 0, Array)
#6 /data/wwwroot/tanhui/public_html/console/controllers/SwooleController.php(38): swoole_server->start()
#7 [internal function]: console\controllers\SwooleController->actionIndex(
[2017-12-04 15:46:54 ^1049.55]  ERROR   zm_deactivate_swoole (ERROR 503): Fatal error: Uncaught yii\base\ErrorException: Error while sending QUERY packet. PID=1049 in /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Transaction.php:128
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'Error while sen...', '/data/wwwroot/t...', 128, Array)
#1 /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Transaction.php(128): PDO->beginTransaction()
#2 /data/wwwroot/tanhui/public_html/vendor/yiisoft/yii2/db/Connection.ph
zhang988925 commented 6 years ago

我们有3个类型的应用 全部被此问题困扰无解 每次都需要重复服务,还请各位给个正确的方法或者告知深层原因

loyating commented 6 years ago

MySQL根据配置文件会限制Server接受的数据包大小。有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入或者更新失败 改一下 max_allowed_packet 配置

matyhtf commented 6 years ago

https://wiki.swoole.com/wiki/page/325.html 所以在多个进程之间,一定不能共用连接

HuangStomach commented 6 years ago

@zhang988925 不知道你解决没有? 我这边遇到了类似的问题并解决了 虽然我没有用过Yii不知道你是否可以参考:

在我们的框架中以PDO为例 由于swoole是守护进程 实际上每个请求都用的同一个sql连接 传统的fpm模式会在fpm进程结束后会自己进行销毁,PDO据说会在析构的自己去释放链接 但是守护进程不会有这一步 就会导致php这边连接以为存在 但是MYSQL自己的链接超时进行了销毁 再有请求进入的时候,php误认为链接存在仍然去进行操作 就会报错 不知道对你有没有参考价值……

zhang988925 commented 6 years ago

我找了问题的根源,是因为mysql长连接,一直发送数据,时间长了,数据量就会增加,触发mysql单个连接最大发送数据,修正方案,长连接一定时间关闭一次,或者定时重启swoole进程