Home  >  Q&A  >  body text

mysql优化 - 一个关于 mysql_connect ,mysql_pconnect 连接时间的问题?

1、mysql_connect 连接后,多长时间会自动释放 ?
2、mysql_pconnect呢,多长时间会自动释放 ?
3、如果使用pdo,是不是和上面情况一样的?
4、配置参数wait_timeout 和连接时间是什么关系?

迷茫迷茫2743 days ago720

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 13:16:39

    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.

    reply
    0
  • Cancelreply