search

Home  >  Q&A  >  body text

Does MySQL disconnect after the php script is executed?

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. . .

PHP中文网PHP中文网2782 days ago893

reply all(5)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:08:27

    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

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 13:08:27

    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.

     长连接就可以避免每次请求都创建连接的开销,节省了时间和IO消耗。
     
     mysql发现一个链接长时间没有执行查询请求,就会自动断掉这个连接

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:08:27

    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. .

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:08:27

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:08:27

    If you use original php, you need to close it manually. The framework is usually packaged and can be used directly

    reply
    0
  • Cancelreply