Suppose you use 5 PHP-FPM worker processes to run PHP web services.
mysql_connect is a short connection, that is, after the PHP-FPM worker process completes the current request, it will automatically release the database connection to MySQL.
mysql_pconnect is a persistent connection, that is, after each PHP-FPM establishes a connection with MySQL, the database connection will not be released after the current request is processed. The next request can reuse this database connection, thereby avoiding repeated establishment of different requests. Overhead caused by database connection.
With 5 PHP-FPM working processes, you can maintain 5 persistent connections to MySQL, forming a one-to-one "database connection pool", but be careful that the number of PHP-FPM processes pm.max_children should not be more than The maximum number of MySQL connections max_connections (default 151).
show global variables like '%timeout%'; It can be seen that the default values of MySQL's wait_timeout and interactive_timeout are both 28800 seconds (8 hours). PHP-FPM has a persistent connection with MySQL. After the idle time exceeds wait_timeout, the page will execute mysql_pconnect again. Return the Warning information of "MySQL server has gone away". At this time, the persistent connection will be re-established and will not affect the normal operation of the program. At this time, you can use @ to suppress the output of the Warning information. But the writing runs in When using the PHP CLI program under the command line, it is recommended not to use persistent connections. It is recommended to re-open and close the database connection every time it is used to avoid the problem of CLI program failure when the database connection is lost.
Mention that the mysql_ series of functions are no longer supported in PHP7. It is recommended to use mysqli and pdo_mysql to operate the MySQL database. Mysqli and pdo_mysql also support the establishment of persistent connections.
Suppose you use 5 PHP-FPM worker processes to run PHP web services.
mysql_connect is a short connection, that is, after the PHP-FPM worker process completes the current request, it will automatically release the database connection to MySQL.
mysql_pconnect is a persistent connection, that is, after each PHP-FPM establishes a connection with MySQL, the database connection will not be released after the current request is processed. The next request can reuse this database connection, thereby avoiding repeated establishment of different requests. Overhead caused by database connection.
With 5 PHP-FPM working processes, you can maintain 5 persistent connections to MySQL, forming a one-to-one "database connection pool", but be careful that the number of PHP-FPM processes
pm.max_children
should not be more than The maximum number of MySQL connectionsmax_connections
(default 151).show global variables like '%timeout%';
It can be seen that the default values of MySQL'swait_timeout
andinteractive_timeout
are both 28800 seconds (8 hours). PHP-FPM has a persistent connection with MySQL. After the idle time exceedswait_timeout
, the page will execute mysql_pconnect again. Return the Warning information of "MySQL server has gone away". At this time, the persistent connection will be re-established and will not affect the normal operation of the program. At this time, you can use @ to suppress the output of the Warning information. But the writing runs in When using the PHP CLI program under the command line, it is recommended not to use persistent connections. It is recommended to re-open and close the database connection every time it is used to avoid the problem of CLI program failure when the database connection is lost.Mention that the mysql_ series of functions are no longer supported in PHP7. It is recommended to use mysqli and pdo_mysql to operate the MySQL database. Mysqli and pdo_mysql also support the establishment of persistent connections.