0

I have migrated my PHP application to a serverless model where the cloud provider limits the amount of active connections between a container instance and the MySQL server. Sometimes during the week, this limit of 100 active connections to MySQL per container instance is exceeded . The SQL client library used in PHP is Medoo, an abstraction of PDO, we use PHP 7.3 and Slim Framework 3.

When checking the SHOW PROCESSLIST; from the MySQL server, it is possible to see several connections that are already old and with sleep status, why weren't they closed? According to the PDO documentation, PHP closes MySQL connections when finishing the script, are there any exceptions in this "end script" condition? When does the script ends? Is there some Apache or PHP config that may be avoiding this and causing the problem?

After contextualizing you, the reader, to the problem. My question is: What could be causing this? Are there any config in PHP or Apache that might be related to the issue? What should I pay attention to in the code? What would you try?

And no, we have not set persistent connections.

  • it depends on your architecture i guess you could just explicitly close the connection at the end of your script. If you have multiple connection objects everywhere then that's a hassle, but if you're passing round a single (or small number of managed) objects it shouldn't be too hard - take a look at this answer https://stackoverflow.com/questions/18277233/pdo-closing-connection – imposterSyndrome Aug 13 '21 at 18:26

1 Answers1

0

Unfortunately PDO does not have any functionality to configure connection limits.

In order to handle the db connections, GCP provide in it's official documentation some good practices to manage it.

In summary I suggest to try the following things:

  • Implement a connection pool so your connections are reuse.
  • Make sure to properly open and close the connections.
  • Handle properly broken or unsuccessful connections implementing timeouts.
drauedo
  • 641
  • 4
  • 17