search

Home  >  Q&A  >  body text

mysql pconnect长连接是这样理解吗?

MySQL pconnect长久连接,是这样理解吗?
有三个用户A、B、C正在访问网站。如果开启了pconnect长连接的,那么:

第一种理解

A用户请求/index.php,/index.php连接mysql,返回到客户端浏览器。
B用户再去请求/index.php,那么是不是就直接用A用户连接到数据库的那个连接了
同理C用户也是这样用的同一个连接

第二种理解

A用户请求/index.php,暂停10分钟未有第二次请求
10分钟之后,再次请求/index.php,那么php使用数据库连接还是原先的那个。
且不管A用户之后请求该网站的任何其他PHP,PHP连接数据库都是用的一开始那个连接?
那时间是永久的?可以支撑多久?

同一时刻,B用户去请求/index.php,他重新开一个长久连接,与A用户开启的那个是不一样的
黄舟黄舟2812 days ago858

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 11:33:47

    Look at what the parasitic state of PHP is like on the web server. If it is the nginx+phpcgi method, then the long connection is maintained by each cgi program itself, and has nothing to do with the address of the user requesting the page, as long as it is the IP and port of the database If the user name remains unchanged, the connection will not be disconnected. Even if the user requests a page and then closes the page connection, the connection will remain for a certain period of time. It is a bit like phpcgi itself maintains a connection pool, which can be viewed with show processlist in mysql. If it is the apache method, pconnect and connect may be the same. Because of the apache+module method, the PHP parser must be re-initialized every time.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:33:47

    PHP is not very clear. Python I have done experiments on Mysql database operations.

    • The first option is 单例连接. web When the service is initialized, such as starting Flask or tornado, it connects to the database Mysql. This connection has a timeout setting. Once it times out, it will automatically reconnect. All requests share this connection for database query operations. Simply put, this connection is a singleton. The database driver used is packaged as torndb
    • The second option is to use 数据库连接池. When the service is initialized, several connections are started and thrown into the database connection pool. Then the connection is retrieved from the connection pool for query. After the query is completed, the connection is released and returned to the connection. Pool. Reference code

    The concurrent request efficiency was tested. The first solution has a stronger request processing capability and Mysql takes up less resources.

    reply
    0
  • Cancelreply