This article is about introducing the specific steps of persistent connection between PHP and MySQL database. Friends in need can learn about it. I hope it will be helpful to you.
Persistent database connection:
1. A persistent database connection refers to a connection that is not closed when the script ends running. When a persistent connection request is received. PHP will check whether there is already an identical persistent connection (that was opened previously). If it exists, the connection will be used directly; if it does not exist, a new connection will be established. The so-called "same" connection refers to a connection to the same host using the same user name and password.
Related tutorials: mysql video tutorial
2. The first method is to use PHP as a separately running language interpreter (CGI Wapper). In this case, there is no difference between using a persistent connection and a non-persistent connection - because the execution of the PHP script itself is not persistent.
3. Use PHP as a module of a multi-process web server. This method is currently only applicable to Apache. When the same client makes a request to the server for the second time, it may be used by a different one. subprocess to handle. After opening a persistent connection, all subsequent pages that request the SQL service can reuse the established SQL Server connection.
4. When using a data table lock in a persistent connection, if the script cannot release the data table lock for any reason, subsequent scripts using the same connection will be permanently blocked, requiring the httpd service to be restarted. Or database service.
5. When using transaction processing, if the script ends before the transaction blocking occurs, the blocking will also affect the next script using the same connection
6. The program uses persistent connections (PDO ::ATTR_PERSISTENT) to access the database, then a PHP-FPM worker process corresponds to a long connection to MySQL.
After the request is completed, PHP will not release the connection to MySQL for reuse next time. This process is harmful to the program. It is transparent.
This can be regarded as the "database connection pool" maintained by PHP-FPM.
7. Instead of saving MySQL resources, it will increase the load on the database.
PDO persistent connection:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array( PDO::ATTR_PERSISTENT => true ));
Proof:
## Related tutorials:The above is the detailed content of How to make a persistent connection between PHP and MySQL database. For more information, please follow other related articles on the PHP Chinese website!