2

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:

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.

Luc
  • 5,339
  • 2
  • 48
  • 48
  • Which flavor of "persistent connection" are you using? – Rick James May 10 '19 at 21:09
  • @RickJames I am not sure what you mean. The code that makes it persistent is literally just that oneliner I mentioned near the top of the question (specifically, the `p:` flag in a mysqli connection). Are there flavors to choose from? Where would I find which one I am using? – Luc May 10 '19 at 22:00
  • @Luc do you already have a solution for this? – Dude Aug 07 '20 at 09:08
  • @Dude I don't have a solution unfortunately. Now that you mention it, I haven't seen the error anymore. Not sure why. If you would like me to check any config values in my setup to see what the difference might be, let me know! – Luc Aug 07 '20 at 13:27
  • @Luc I saw the error in the logs once, I'll check if they are in the logs again on monday, then I'll let you know :) – Dude Aug 08 '20 at 16:34
  • @Luc I'm letting go of the persistent connections, as I was getting lot's of "too many connections in use" errors yesterday... If I understand it correctly, you don't have to close the connections and it will reuse them, right? Well that got me those too many connections in use errors :-( – Dude Aug 11 '20 at 11:43
  • 1
    @Dude Can you just set the limit higher? Those idle connections don't cost anything on the database side, so 100 concurrent connections (which would be huge and require a lot of processing power if they were all parallel running queries) should have no impact if it's just from the connection pool. Or perhaps you can tweak how many connections are in php's pool? It has been a while since I worked with this, and I haven't had this issue so I don't know which dial to tweak. – Luc Aug 11 '20 at 13:00
  • @Luc Thx for the tip, I'll check it out later :-) – Dude Aug 12 '20 at 13:25
  • As far as I can tell, this is just a warning, and the only way to change this would be to raise an Issue against PHP to change the behaviour. You can however change the PHP INI setting "display_errors" to "Off" in your php.ini file to avoid this message corrupting your HTTP responses on your production site. – nucc1 Jun 16 '22 at 10:16

0 Answers0