There is a SQL statement that connects to mysql in a script. When executed, the mysql server opens a new thread (or process?) to process the connection. After the script ends, does it disconnect from PHP (or apache?) . What are those mysql long connections? Suddenly I felt like I didn’t understand anything. . .
If you want to manually close the framework, write the closing code in the destructor of the class. It is usually encapsulated. Don’t worry about it. It is usually connected to mysql in singleton mode
1.mysql starts the process to handle the corresponding transaction
2. Disconnect from the apache httpd process. PHP runs in apache mod mode. Apache receives the request and forwards the request to mod. The mod calls PHP
sapi execution. The whole process PHP is executed in the apache module mode. In the httpd process Medium
3. Disadvantages of short links: Create a connection, and after the program is executed, the link to mysql will be automatically broken. So how many times PHP is executed, there will be many such creation and release processes. Frequently creating and releasing connections consumes CPU resources.
It is not possible to maintain a long connection from PHP alone, but there are ways to achieve it.
If you run PHP in Apache+php_module mode, you can establish a permanent link through mysql_pconnect, but this link is maintained by Apache (mysql_pconnect cannot maintain long connections under nginx+fpm, as the official documentation explains)
nginx+fpm fpm is generally set to static. Through PDO extension, a long connection can be set when connecting to the database, and each fpm maintains a permanent link. However, it is still necessary to evaluate the number of FPM processes and the maximum number of database connections based on the system. If there are too few PHP requests, a large number of idle connections will waste resources (mysql wait_time needs to be configured appropriately). If there are too many PHP requests, too many FPM processes will exceed the database limit. The maximum number of connections will cause too many connections. .
Generally, php will create a short link every time it executes sql. When the execution is completed, php will disconnect the link (perhaps after timeout, MySQL will disconnect the link)
Nowadays, a common way to reduce this IO overhead is to establish a database connection pool, maintain a specified number of connections, and directly obtain relevant resources when using it.
If you use original php, you need to close it manually. The framework is usually packaged and can be used directly