mysql_pconnect is used to establish a continuous connection between php and mysql. The general execution mode of php is to initialize all resources when the script starts executing, and release all resources after the script is finished. This is not the case with mysql_pconnect. Mysql_connect re-establishes a relationship with the sql server through tcp or unix domian socket every time. Each handshake consumes a lot of server resources.
When using pconnect, when there is a request to connect to mysql, php will check whether the same connection (connected to the same mysql server with the same username and password) has been established before. If so, use this connection directly. , It is worth noting that the concept of the same connection is for processes. Different processes calling mysql_pconnect will establish multiple connections.
Connect and pconnect will not bring about functional differences, only performance differences.
Generally, PHP has two operating modes, one is to run as cgi, and the other is to run as an apache module. When used as cgi, connect is no different from pconnect, because the resources will be destroyed and cleaned up every time cgi is run.
When PHP is run as an apache module, continuous connection to the database can be used, but there may be potential problems.
Assume that the mysql server is configured to support a maximum of 10 concurrencies. And apache is configured to use 100 child processes.
Apache uses a parent process to coordinate the distribution of received http requests to which idle child processes are processed. In this way, 10 http requests are quickly processed. Assuming that 10 are all assigned to different child processes, then 10 A persistent connection with mysql is established, and the capabilities of mysql have reached its limit. At this time, another http request comes, and Apache distributes it to any other process that is not among these 10 sub-processes. Then this process cannot establish a connection to mysql because the pit is full.
There are other problems with using persistent connections.
If you use a persistent connection in your script and perform a table lock operation, it will not be unlocked until the end of the script. Then the next time you run this script, it will wait endlessly for the past to unlock the table in order to obtain the lock table. The past it can no longer come back, and this becomes an infinite loop. Unless restarting the web or mysql server. Another thing that can cause locking is transactions.
To avoid this, you can use register_shutdown_function to register a callback function, where you can release the table lock or roll back the transaction.