PHP Socket Server Loses Connection to MySQL Overnight

Shannon A. :

I've got a PHP socket server that sits around waiting for connections, then talks to a database to resolve them. 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.

$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?

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

Markownikow :

Mysql server will close inactive connection after "wait_timout" seconds. The variable is described here: 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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=372175&siteId=1