Setting PDO connection timeout time less than 1 second - php

I'm using PHP and a PDO object to connect to mysql. I have 3 DB servers that my php code can connect to. If I try to connect to DB server #1 and the connection fails I would like to immediately try to connect to DB server #2. The lowest I can set the connection timeout time is 1 second with the code below.
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $username, $password,array(PDO::ATTR_TIMEOUT => "1",PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
Ideally I'd like to set the timeout time to <50 milliseconds. Or 0ms if possible. Is there any way to do this?

This is not possible because the underlying MySQL driver won't allow it:
Request #60716: Ability to set PDO connection timeout in milliseconds

I think it is impossible to set a timeout using milliseconds. Refer to the php doucmnetation of mysql.connect_timeout:
mysql.connect_timeout:
Connect timeout in seconds. On Linux this timeout is also used for waiting for the first answer from the server.
Btw, I think what you are about to do sounds a little bit hacky to me. If you have professional requirements I would use a load balancer. You can follow this tutorial

Related

PHP Socket Server Loses Connection to MySQL Overnight

I've got a PHP socket server that sits around waiting for connections, then talks to a database to resolve them. It works fine when I'm testing, but then when I leave it sitting around, the next morning it no longer talks to the database.
When I look at my logs, I see this:
200327 11:54:37 24 Connect dbuser#localhost as anonymous on dbname
24 Quit
Where I'm expecting to see something more like this:
200327 11:54:20 23 Connect dbuser#localhost as anonymous on dbname
23 Query SELECT * FROM table1 WHERE num=4
23 Query SELECT * FROM table2 WHERE num='4' AND info='deleted'
23 Query SELECT * FROM table3 WHERE num='4'
23 Quit
But for some reason, after the server has been running for a while, the queries never go through after that initial connection.
The only thing I can think of is that maybe my PDO object is somehow timing out, as I create it once when I fire up the server.
$dbh = new PDO($dbName,$dbUser,$dbPass);
Any thoughts on what might be going on, and if creating the PDO object at the start of the process isn't correct, how to better manage that resource?
PHP is PHP 7.0.33, MySQL is 10.1.44-MariaDB-0+deb9u1 Debian 9.11.
Mysql server will close inactive connection after "wait_timout" seconds. The variable is described here:
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_wait_timeout
I would suggest to open new connection on client side(php) every time request comes to socket and close after job is done because increasing wait_timeout could lead to too many hanging connections(unless php server never goes down and reuses same db connection)

PHP mysql persistent connection not reused ( opens more than one connection per fpm-worker )

I'm facing a really weird behaviour while testing persistent connections from php to mysql. I have a small script that looks like this:
<?php
$db = new mysqli('p:db-host','user','pass','schema');
$res = $db->query('select * from users limit 1');
print_r($res->fetch_assoc());
My setup is :
OS: CentOS 7.3
PHP/7.1.18
php-fpm
nginx/1.10.2
MySQL-5.6.30
I tried to do some requests with ab:
$ ab -c 100 -n 500 http://mysite/my_test_script.php
PHP-FPM was configured to have 150 workers ready, and i saw what i was expecting, 150 established connections to mysql, which stayed open after the ab finished. I launched ab once again, and the behaviour was still the same, 150 connections, no new connections where opened. All fine. Then i created a script which did the the same exact requests, same IP, same HTTP headers, but used curl to make the request, and BOOM i had 300 connections on mysql instead of 150. I launched the script again, i got still 300 connections. Subsequent runs of the same script didn't increase the number of connections. Did anyone ever faced anything like this? Does anyone know what could make php open more connections than needed? Am I missing something obvious?
If it's not clear what i'm asking, please comment below and i will try to better my explain problem.
P.S. I tried this with PDO too, same behaviour.
EDIT: My tests where not accurate
After further testing i noticed that my first tests where not accurate. I was in a multi-tenant environment and different connections ( different schema ) where initialized when i launched ab. In my case the php documentation was a bit missleading, it says:
PHP checks if there's already an identical persistent connection (that remained open from earlier) - and if it exists, it uses it. If it does not exist, it creates the link. An 'identical' connection is a connection that was opened to the same host, with the same username and the same password (where applicable).
http://php.net/manual/en/features.persistent-connections.php
Maybe its i obvious to everyone, I don't know, it was not for me. Passing the 4th parameter to mysqli made php consider connections not identical. Once i changed my code to something like this:
<?php
$db = new mysqli('p:db-host','user','pass');
$db->select_db('schema');
$res = $db->query('select * from users limit 1');
print_r($res->fetch_assoc());
The application started to behave as i expected, one connection per worker.

postgresql pdo very slow connect

We are facing performance issue with our web server. We are using an apache server (2.4.4) with php 5.4.14 (it's a uniserver package) and a postgresql database 9.2. It’s on a Windows system (can be XP, 7 or server…).
Problem is that requests answers from the web server are too slow; we have made some profiling and found that database connection is around 20 ms (millisecond).
We are using PDO like this:
$this->mConnexion = new \PDO(“postgres: host=127.0.0.1;dbname=”, $pUsername,$pPassword, array(\PDO::ATTR_PERSISTENT => false));
We have made some time profiling like this:
echo "Connecting to db <br>";$time_start = microtime();
$this->mConnexion = new \PDO(…
$time_end = microtime();$time = $time_end - $time_start;
echo "Connecting to db done in $time sec<br>";
We have made a test with ATTR_PERSISTENT to true and we came up with a connection time much faster. Code reports connection time = 2. E-5 second (whereas it’s 0.020 s with persistent to false).
Is 20 ms a normal value (and we have to move to persistent connection) ?
we have also made a test with mysql, connection time for non persistent connection is around 2 ms.
We have these options set in postgresql configuration file :
listen_addresses = '*'
port = 5432
max_connections = 100
SSL = off
shared_buffers = 32MB
EDIT
We do not use permanent (yet) because there are some drawbacks, if the script fail connection will be in a bad state (so we will have to manage these cases, and it’s what we will have to do…). I would like to have more points of view concerning this database connection time before directly switching to persistent connection.
To answer Daniel Vérité question, SSL is off (I already checked this option from my previous search about the subject).
#Daniel : i have tested on a intel core 2 Extreme CPU X9100 # 3.06Ghz 4Gb RAM
Try using unix domain socket by leaving host empty. It's a little bit faster.

Why does making a connection to MySQL take about 2 seconds in PHP?

My code is:
$start = time();
$sqli = new mysqli("localhost", "root", "mySelectedPass", "mydb");
echo time() - $start;
Result: 2.
What's the problem?
I'm using XAMPP 1.8.1 and Windows 8.
Ans: Use 127.0.0.1 instead of localhost :)
You're testing the time it takes to connect to the server and run the query, whereas in your SQL admin tool you're only testing the query execution time.
You might have something misconfigured that makes connecting to your database server really slow. Most applications should use a connection pool to avoid having to reconnect to the database on every single request.

How long does a PHP MySQL database connection remain active?

I have a long running PHP script. I am making the database connection at the very beginning of the script and does some database operation at the start up.
After that, the script perform 4 hours of PHP operation without pinging to MySQL with that connection even a single time.
At the end of these long running PHP operations, when I try to execute mysql_query it gives me the following error: MySQL Server has gone Away
Is there any possibility of increasing the connection timeout to be 4 hours? I am using PHP ADODB to connect with MySQL from my PHP application.
Please suggest what to do?
MySQL has a different timeout than PHP. You could increase it in php.ini on the line mysql.connect_timeout = 14400. Also increase the default_socket_timeout = 14400
Note that if your PHP setting allow you to do an ini_set, you can also do as follows:
ini_set('mysql.connect_timeout', 14400);
ini_set('default_socket_timeout', 14400);

Categories