1

Summarize the problem

My PDO (MySQL) connection is disconnected during execution of the code. The connection is created successfully, but around 0.05% of the time the Error log reports a disconnection.

Provide details and any research

Created wrapper functions and debug statements. https://www.php.net/manual/en/pdo.construct.php https://www.php.net/manual/en/pdo.prepare.php

When appropriate, describe what you’ve tried

I have tried to backtrace the code in the logs with debug_backtrace.

I would like to know what might cause a PDO MySQL connection to be disconnected?

4
  • 1
    Is there anything in MySQL logs? What is the exact error code / status message you get and from which function it comes from? Sep 20, 2022 at 7:04
  • Unfortunately the app uses a wrapper function around PDO, and there is no error code, just the variable that stores the $pdo connection becomes null. The classes that have this issue seem to store the pdo connection on its own property instead of using the singleton directly. I am modifying that to check if it solves the issue.
    – ipegasus
    Sep 20, 2022 at 13:46
  • 1
    MySQL logging does not care about the code that uses its API. So, you should check its logs anyway. However, one possible culprit here is that a copy of single PDO instance is used somewhere in the code, and that object's status is changed. The change does not propagate to the main PDO instance, but the actual connection state to MySQL is changed. Therefore, when the main PDO instance is used, there is corrupted state which could lead to disconnection. This is only an educated guess. Sep 20, 2022 at 19:03
  • Thanks, I will update the issue once this modification is applied on production. The plan is exactly that, not using a copy of the connection, but instead a singleton directly from the main PDO class.
    – ipegasus
    Sep 20, 2022 at 22:57

1 Answer 1

0

From MySQL Command Prompt, use SHOW GLOBAL STATUS LIKE 'aborted%' to monitor aborted_connections count. If count goes up about the same time, in your my.cnf [mysqld] section, change or add a line for connect_timeout=30 to be patient with the process of connecting.

Stop/start the instance when convenient for this change to be effective.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .