简体   繁体   English

PHP 套接字服务器一夜之间失去与 MySQL 的连接

[英]PHP Socket Server Loses Connection to MySQL Overnight

I've got a PHP socket server that sits around waiting for connections, then talks to a database to resolve them.我有一个 PHP 套接字服务器,它等待连接,然后与数据库对话以解决它们。 It works fine when I'm testing, but then when I leave it sitting around, the next morning it no longer talks to the database.当我测试时它工作正常,但是当我让它坐在那里时,第二天早上它不再与数据库对话。

When I look at my logs, I see this:当我查看我的日志时,我看到:

200327 11:54:37    24 Connect   dbuser@localhost as anonymous on dbname
       24 Quit  

Where I'm expecting to see something more like this:我期待看到更像这样的东西:

200327 11:54:20    23 Connect   dbuser@localhost as anonymous on dbname
           23 Query SELECT * FROM table1 WHERE num=4
           23 Query SELECT * FROM table2 WHERE num='4' AND info='deleted'
           23 Query SELECT * FROM table3 WHERE num='4'
           23 Quit  

But for some reason, after the server has been running for a while, the queries never go through after that initial connection.但出于某种原因,在服务器运行一段时间后,在初始连接后查询永远不会通过。

The only thing I can think of is that maybe my PDO object is somehow timing out, as I create it once when I fire up the server.我唯一能想到的是我的 PDO 对象可能以某种方式超时,因为我在启动服务器时创建了一次。

$dbh = new PDO($dbName,$dbUser,$dbPass);

Any thoughts on what might be going on, and if creating the PDO object at the start of the process isn't correct, how to better manage that resource?关于可能发生的事情的任何想法,如果在流程开始时创建 PDO 对象不正确,如何更好地管理该资源?

PHP is PHP 7.0.33, MySQL is 10.1.44-MariaDB-0+deb9u1 Debian 9.11. PHP 是 PHP 7.0.33,MySQL 是 10.1.44-MariaDB-0+deb9u1 Debian 9.11。

Mysql server will close inactive connection after "wait_timout" seconds. Mysql 服务器将在“wait_timout”秒后关闭非活动连接。 The variable is described here: https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_wait_timeout变量在这里描述: https : //dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_wait_timeout

I would suggest to open new connection on client side(php) every time request comes to socket and close after job is done because increasing wait_timeout could lead to too many hanging connections(unless php server never goes down and reuses same db connection)我建议每次请求进入套接字并在工作完成后关闭时在客户端(php)打开新连接,因为增加 wait_timeout 可能导致挂起连接太多(除非 php 服务器永远不会关闭并重用相同的数据库连接)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM