Resolve the differences between mysql_connect and mysql_pconncet in php

  • 2020-06-03 05:55:46
  • OfStack

Talk about the difference between mysql_connect and mysql_pconnect. The two functions are similar in usage. Some websites say you should use pconnect. There are also those who regard pconnect as a terrible beast, who resolutely refuse to use pconnect, and those who have ambiguous attitudes. So what does this thing look like?

A permanent link does not mean that the server opens a connection and everyone shares the link. Permanent connection 1 is one connection per client, 200 connections for 200 visitors. The real mysql_pconnect() itself doesn't do much, the only thing it does is not actively drop close's connection to mysql after the php run is over.

In php pconnect cgi way runtime and connect is almost no difference, because cgi is per 1 php access up 1 process, after the visit process ends, the resources are all released. When run php apache module way, because there are using apache process pool, 1 httpd process after the pool will be back on the process, it also makes connections with the mysql pconnect open resources were released, And there under a connection request can be reused. This makes the apache concurrent traffic is not big, by using pconnect php saved repeatedly connected db time, makes the access speed. This should be better understood. But in apache concurrent trafficked, if use pconnect, due to previous 1 mysql connection without close occupied some httpd process, may be because mysql has reached the dalian then, 1 after making some requests are not being met. Forever if mysql maximum number of connections is set to 500, and the maximum number of simultaneous access apache set to 2000, assuming all access will require access db, and operating time is not long, the current 500 requests httpd all have no, at the end of the after httd process is unable to connect to mysql (as has been achieved mysql maximum number of connections). Only the current 500 httpd process ends or be reuse can connect mysql are obtained.

When db is complex and time-consuming, many connection timeouts will occur because httpd will process many concurrent fork processes, and the first generated httpd process will not release the db connection, making the later generated httpd process unable to connect to db because it does not reuse the mysql connection of other httpd processes. When concurrent traffic is not high, using pconnect can simply improve access speed, but after the increase in concurrent volume, whether to use pconnect again depends on the choice of programmers.

As far as I am concerned, php does not really use the connection pool for the connection of mysql, and pconnect just borrows the process pool of apache to use, so pconnect cannot improve the efficiency of accessing db very well when concurrent visits are large.

In a practical application, mysql_pconnect would be faster to refresh and request a new page each time, while mysql_connect would require a new request each time. You can see the difference when the database connection is slow. pconnect works when your database connection is slow, the DB operation is not too complicated, and your program is confident enough not to create deadlocks, or if you have control of the server and any two of the above four conditions are met, you can use pconnect.

pconnect does not need to be turned off in the script. You can set lifetime in mysql, or write shell to scan regularly and kill to drop dormant long connections. To make good use of pconnect, it is not just php scripting but also database and server Settings.

Related articles: