4

i'm using php 5.4.4 and it seems that persistent connections with mysqli don't work. In particular, let's look at this script:

<?php

$links = array();

for ($i = 0; $i < 15; $i++) {
    $links[] =  mysqli_connect('p:192.168.1.40', 'USER', 'PWD', 'DB', 3306);
}

sleep(15);

Then, when the script is running, open another shell and:

netstat -an | grep 192.168.1.40:3306
tcp        0      0 192.168.1.6:52441       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52454       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52445       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52443       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52446       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52449       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52452       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52442       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52450       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52448       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52440       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52447       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52444       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52451       192.168.1.40:3306       ESTABLISHED
tcp        0      0 192.168.1.6:52453       192.168.1.40:3306       ESTABLISHED

I think that this an error: PHP should open only one persistent connection instead of 15 distinct connections.

Is it true?

THANKS ALL.

3
  • Why do you want to use a persistent connection? The chances are you're causing more harm than good
    – Martin
    Commented Jul 25, 2012 at 20:23
  • Thanks Martin, but i need to use it!
    – MaCi
    Commented Jul 25, 2012 at 20:44
  • Using $link = mysqli_init(); $links[] = mysqli_real_connect($link, 'p:192.168.1.6', 'USER', 'PWD', 'DB', 3306); seems to work
    – MaCi
    Commented Jul 25, 2012 at 21:03

2 Answers 2

4

You are looking for the benefit in the wrong place; Using persistent connections does not help a single php script execution like in your test - it helps over a series of requests.

When you ask mysqli to make a persistent connection, it looks in a pool of established connections that match your connection details (username/password/etc) and that aren't being used. Your loop asks to use fifteen connections, you will get fifteen connections. Each connection is dedicated to your thread.

When your thread closes, you should have fifteen connections listed in the netstat, like you do.

If you run the script again, you should see no change in the listed connections (ie no new connections were made). That is where the advantage comes in - on later script executions you save the overhead of establishing the connection.

a better test would be to time the execution of creating a hundred persistent connections on the first request, and then re-run the same request which should go faster because it doesn't need to make a hundred new connections.

0

This makes sense if the server API you are running PHP through creates multiple parent processes to run PHP.

For example, if you were using FastCGI and configured it to have 15 processes, and say 50 children in each process, you could have up to 15 persistent connections at any given time (1 per process) and each of the 50 children of each respective process would use the 1 persistent link from their parent process.

In Apache, I believe the StartServers directive would be relevant, and then for each child server process running, you have ThreadsPerChild threads running that would each use the persistent link from their respective parents.

My guess is that if you are using Apache, you have 15 child processes running and each of those have their own threads. That is why you are seeing 15 persistent connections.

Hope that helps.

5
  • Thanks drew010, but what you're saying is different from what PHP.net says (php.net/manual/en/features.persistent-connections.php). Then, using mysql instead of mysqli, it works.
    – MaCi
    Commented Jul 26, 2012 at 5:58
  • It says that here: They cause the child process to simply connect only once for its entire lifespan, instead of every time it processes a page that requires connecting to the SQL server. This means that for every child that opened a persistent connection will have its own open persistent connection to the server. For example, if you had 20 different child processes that ran a script that made a persistent connection to your SQL server, you'd have 20 different connections to the SQL server, one from each child.
    – drew010
    Commented Jul 26, 2012 at 6:42
  • But it also says that: This means that when the same client makes a second request to the server, it may be served by a different child process than the first time. When opening a persistent connection, every following page requesting SQL services can reuse the same established connection to the SQL server.
    – MaCi
    Commented Jul 26, 2012 at 9:02
  • Maybe the next appropriate question is, what SAPI is your server using and how is that SAPI configured as far as process control? Run netstat -anp|grep 3306 so you can see the PID that each of those connections belongs to and then you can determine what each of those parent processes are and if they are separate server processes running for handling requests.
    – drew010
    Commented Jul 26, 2012 at 17:10
  • It's perfectly true what you said about apache and how db connections are managed within its children, but i'm using CLI not HTTP. Mysql works fine with persistent connections using the first script (1 connection only) and mysqli not (15 distinct connections). I try to update php and now seems that mysqli works, probably it was a bug of my php 5.4 installed version. Thank you!
    – MaCi
    Commented Jul 30, 2012 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.