PDO connection time in API, php-fpm restart resolves

I keep getting the following error from my code:

Failed to connect to the database: PDO :: __ construct (): send error of 12 bytes with errno = 110 Connection timeout

This error is constantly repeated on api. If this api continues to receive many calls throughout the day, this does not happen. Only when api is not used for some time.

I can solve this problem by rebooting / reloading php-fpm, but this should not be the solution.

Does anyone know how to solve this?

- EDIT -

This is the code to connect to the database:

public function connectDatabase($allow_persistent = true) { $this->db = null; $this->readINI(); $pdo_attr = [ PDO::ATTR_PERSISTENT => $allow_persistent, PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8;", PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true, ]; $this->db = new PDO("mysql:host=" . $this->db_data_dbhost . ";dbname=" . $this->db_data_dbname . ";charset=utf8", $this->db_data_username, $this->db_data_password, $pdo_attr); $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } 

To repeat a few more, this is used in two APIs at the moment.

  • One of them is constantly used by customers and never encounters this problem.
  • The second is not used by many clients, but still sees that it is still under development, but with only a few clients for testing. We are trying to push the application in the Appstore, but it gets confused with this problem. This API will eventually encounter an error, as shown above, and will not recover from this state without rebooting / reloading the php-fpm service.
+5
source share
1 answer

Using persistent connections is not so good ( see why ), but still, to solve this problem, you can increase the latency of MySQL connections. To do this, see the wait_timeout parameter for my.ini.

ps In addition, you can catch this error and just connect to the database.

+2
source

Source: https://habr.com/ru/post/1269258/


All Articles