In-depth discussion: PHP uses a database permanent connection to operate MySQL with non-_php tutorials

Source: Internet
Author: User
PHP programmers should know that connecting to a MySQL database can use the Mysql_pconnect (permalink) function, the use of a database permanent connection can improve efficiency, but the actual application of persistent database connection will often lead to some problems, The usual performance is the intermittent inability to connect to a database on a site with a large amount of traffic, and a similar "Too many connections in ... "Error message, restarting the server is normal, but not a moment later, the same failure occurred. The cause of these problems, I am afraid that not everyone can be clear, although there are some information in the PHP document, but the explanation is not easy to understand, here I have the cheek to try to do a simple discussion, the views are not all correct, welcome feedback.

First look at the definition of a persistent database connection:
A persistent database connection is a connection that is not closed when the script finishes running. When a request for a permanent connection is received. PHP will check to see if there is already an identical persistent connection (previously open). If present, the connection will be used directly and if it does not exist, a new connection is established. The so-called "Same" connection refers to a connection that uses the same user name and password to the same host.

PHP uses the permanent connection mode to operate MySQL is a prerequisite: PHP must be installed as a multi-threaded or multi-process Web server plug-in or module. The most common form is to use PHP as a module for a multi-process Apache server. For a multi-process server, the typical feature is that there is a parent process and a set of child processes running in harmony, where the actual generation of the Web page is a child process. Whenever a client makes a request to the parent process, the request is passed to a child process that is not yet occupied by another client request. This means that when the same client requests a second time to the server, it is likely to be handled by a different child process. After a persistent connection is turned on, the subsequent pages of all the different sub-processes requesting the SQL service are able to reuse the established SQL Server connection. It allows each child process to do only one connection operation in its lifetime, rather than making a connection request to the SQL server each time a page is processed. Each child process establishes its own, separate, permanent connection to the server. PHP itself does not have the concept of database connection pool, but Apache has the concept of process pool, an Apache child process will be put back to the process pool, which makes the mysql_pconnect open with the MySQL connection resource can not be released, Instead, it is attached to the corresponding Apache child process and saved to the process pool. It can then be reused on the next connection request. Everything seems to be normal, but when the Apache concurrency is large, if you use Mysql_pconnect, the MySQL connection occupied by the previous Apache child process is not close, and the MySQL reaches the maximum number of connections quickly. Causes the subsequent request to be unresponsive.

Some of the above text is excerpt from the PHP document, it may seem that some genteel is not good understanding, then I will use plain English to give an example to illustrate the problem:

Assuming that the Apache configuration has a maximum number of connections of 1000,mysql configured with a maximum of 100 connections, when the Apache server receives 200 concurrent accesses, 100 of them involve database access, and the remaining 100 do not involve database access. Because there is no available database connection at this time, so this involves the database access of the 100 concurrency will simultaneously generate 100 database permanent connection, reached the maximum number of database connections, when these operations are not finished, any other connections can no longer get the database connection, when these operations are finished, The corresponding connection will be put into the process pool, the Apache process pool has 200 free child processes, of which 100 are with the database connection, because Apache will randomly pick up the idle child process for the access request, So you get the sub-process is likely to not contain the database connection of the 100 of the one, and the database connection has reached the maximum, you can not successfully establish a new database connection, alas, you will have to constantly refresh the page, at which time lucky, happened to be assigned to a child process with database connection, To browse the page normally. If the site is a large amount of traffic, there can be a lot of concurrency at any time, so the browser may not stop to find that the database is unable to connect the phenomenon.

Perhaps you would say, we put the maximum number of Apache and MySQL connection to the same size is not okay? Yes, a reasonable adjustment to this maximum number of connections will avoid this problem, but Apache and MySQL load capacity is different, if according to Apache load capacity to set, for MySQL, the maximum number of connections, will produce a large number of MySQL database permanent connection, for example, like the peace era to feed a millions of of the army, its costs outweigh the cost; If you follow the load capacity of MySQL set, for Apache, the maximum number of connections is small, a little kill chicken sledgehammer feeling, The maximum efficiency of Apache cannot be played.

So according to the PHP manual, only suitable for the use of persistent database connection on a site with little concurrent access, but for a site with little concurrent access, the efficiency of using a database permanent connection does not seem to make much sense, from this point of view, I think the database permanent connection in PHP is basically a chicken role, if you must use the concept of database connection pool, you can try Sqlrelay or Apache itself to provide mod_dbd, perhaps there will be surprises.

http://www.bkjia.com/PHPjc/327329.html www.bkjia.com true http://www.bkjia.com/PHPjc/327329.html techarticle PHP programmers should know that connecting to MySQL database can use Mysql_pconnect (permanent connection) function, the use of database permanent connection can improve efficiency, but the actual application of the database permanent ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.