When connecting to the database with a persistent connection (to avoid the connection time overhead), PHP warns that the "server has gone away". This is the code I use:
$db = new mysqli("p:10.0.0.1", "user", "pass");
I looked at the following questions:
- PDO: MySQL server has gone away
- Solving "MySQL server has gone away" errors
- PHP mysqli reconnect problem
- What would cause PHP to ignore mysqli.reconnect setting?
- Error on connecting to MySQL database using PHP scripts
- mysqli::__construct(): MySQL server has gone away
- PHP mySQLi:: MySQL server has gone away
- PHP cant connect to MySql (MySqli)
- other search results from https://stackoverflow.com/search?q=mysql+server+has+gone+away+php+connect+is%3Aq
And a bunch of others. None of them are exactly the same as my issue: they either have long-running scripts or queries, or they ask how to automatically reconnect upon this error. In my case, it's not a long running script (it's a connection pool), and it's not an error: the scripts seem to work fine, it just outputs this ugly warning. PHP's connection pool just contains connections that have since timed out.
On my previous server, I very rarely saw this warning. In fact, there was an issue with hanging pages (now resolved) which would often show this warning or error (I don't remember which) after a few minutes. I never figured out what the issue was, but it seems related.
I want to avoid having to change every script, as there are hundreds of small applications running on the server (each is standalone and has its own database credentials). Adding an @
in front of each connection has the side effect that it suppresses all errors, not just this particular one, and feels like a workaround instead of solving the underlying issue.
Is this just a bug in PHP, or is there a configuration setting that resolves it? I saw someone recommending a longer timeout on MariaDB's side, but that just lessens the frequency with which we'll see the warnings instead of resolving the issue fundamentally. Can PHP check its pool from time to time, or just discard old connections from the pool?
I also checked the documentation pages (1, 2), but the docs nor the comments mention this issue.