Let's talk about the difference between mysql_connect and mysql_pconnect. These two functions are similar in usage. pconnect should be used on the Internet. pconnect is a good thing, and pconnect is also regarded as a flood of animals, we are determined not to use pconnect, but we are also confused. What is this?
The permanent link does not mean that the server opens a connection and all people share the link. A permanent connection means that each client opens a connection, and 200 connections can be accessed by 200 people. Its actual mysql_pconnect () does not handle much. The only thing it does is that it does not actively close the MySQL connection after the PHP operation ends.
There is basically no difference between pconnect and connect when PHP runs in CGI Mode, because each PHP accesses a process and the process ends after the access is complete, all resources are released. when PHP runs as an Apache module, because Apache uses a process pool, an httpd process will be put back into the process pool after it ends, this will prevent the MySQL connection resource opened with pconnect from being released, so it can be reused when there is a connection request. this makes it possible for PHP to save time for repeatedly connecting to the database due to the use of pconnect when the concurrency traffic of Apache is low, which speeds up access. this should be easy to understand. however, if you use pconnect when the number of concurrent accesses to Apache is large, it may be because the MySQL connection occupied by some httpd processes has not been closed, in this way, subsequent requests will never be satisfied. if the maximum number of connections in MySQL is set to 500, and the maximum number of concurrent connections in Apache is set to 2000, it is assumed that all accesses require access to the database, and the operation time will be relatively long, when the httpd of the current 500 requests are not completed, the subsequent HTTD processes cannot connect to MySQL (because the maximum number of connections in MySQL has been reached ). mySQL can be connected only when the current 500 httpd processes are terminated or reused.
When dB operations are complex and take a long time, because httpd will fork many concurrent processes for processing, the first HTTPd process will not release the DB connection, making the subsequent HTTPd process unable to connect to the DB. as a result, the MySQL connection of other httpd processes is not reused. therefore, many connection times out. When the concurrent access volume is not high, pconnect can be used to increase the access speed. However, when the concurrent access volume increases, whether to use pconnect again depends onProgramEmployee's choice.
I personally think that the connection pool is not actually used by PHP for MySQL connections, and pconnect is only used by the Apache process pool, therefore, pconnect does not greatly improve the efficiency of accessing the database when the concurrency traffic is high.
In actual applications, if mysql_pconnect is used, each refresh and request for a new page are relatively fast. If mysql_connect is used, each refresh request must be made again. When the database connection is slow, we can see the difference. When your database connection is slow, DB operations are not very complex, and your program is confident enough to avoid deadlocks, or you have control over the server, if any two of the above four conditions are met, you can use pconnect.
Pconnect does not need to be closed in the script. You can set the life time in MySQL, Or you can write a shell to scan regularly to kill connections with too long sleep.In a word, pconnect should be used well, not only for PHP scripts, but also for database and server settings.