Mysql_connect () and Mysql_pconnect ()

Source: Internet
Author: User
Tags mysql connect pconnect

Mysql_connect () and Mysql_pconnect (), the former function is to establish a new connection each connection, the latter is a continuous connection.

Mysql_connect () creates one or more new connections each time the current PHP page is invoked, and then closes the connections after the request is completed. This approach is more suitable for use in less onerous pages, do not need to adjust, directly in the internal use.
Mysql_pconnect () also creates a new connection when the page is invoked, but does not close the connection at the end of the request, but instead keeps the connection in the connection pool so that a concurrent request can continue to use the connection. This connection is provided to those pages that use very frequently, otherwise the resource will be consumed by frequent opening and closing, which will have a serious effect on performance.
The P in Mysql_pconnect () is the first letter of the word persistent (permanent).

Mysql_pconnect () requires adjustments to the server, limit the number of connections, configure timeouts, and when to process idle processes. He can help you improve your efficiency in the right environment.
Pconnect will often lead to MySQL connection failure, prompted too many connections, because after Pconnect, Apache will not automatically shut down the MySQL connection.
Let's look at how Apache works.
Under Windows, Apache uses a main process, plus a secondary process, and then a secondary process to derive n threads to provide services, the number of threads can be configured in the httpd.conf: Threadsperchild 500, if specified as 500 threads, Apache starts with 500 threads, but uses up to 500 threads at a time, and if there are more than 500 simultaneous connections (500 connections may be available for 300 users), the method for determining the current connection can be used Netstat-na|grep 80|grep est| Wc-l or using the Apache status module), the extra connection will fail to wait or connect. (So, the main configuration parameters of the Apache in Windows should be Threadsperchild, based on the current number of connections, and then see if there is a need to increase some, general PC server set to 1000 is relatively large.)
Nix, Apache uses the process to run, the same principle, need to adjust the number of process parameters are several, such as Serverlimit.

Let's see how Apache+php+mysql_pconnect works.
Whenever a client sends a connection request (including pictures, html,php, etc.) to the server, Apache will use a thread to accept the request, if the request is a PHP file, and the PHP file uses Pconnect, The current thread will determine that the current line Chengyu has not been opened Pconnect, if it has been opened, then use the original MySQL connect, if not opened, then a new connect, and, after the disconnect, the thread is still running, and keep MySQL Connect. After running in this way for some time, it is entirely possible that all Apache threads have open pconnect PHP pages, so if Apache threadsperchild=500, then 500 threads are looking for MySQL connections, And does not shut down, it requires that the number of MySQL connections must be greater than or equal to 500, if less than this value, will cause the PHP page prompts the database connection failed.

So, come to the conclusion that when using Pconnect under Apache+php+mysql, the MySQL max_connect must be greater than or equal to the maximum number of threads (processes) in Apache. In a very large site, using pconnect may not be realistic, The best approach is to make the database content as static as possible, without the need for each page to connect to the database, and to use mysql_connect (even if most of the pages are made into static files but still have mysql_pconnect, the same requirements for MySQL's max_ Connect is greater than the number of Apache threads, so using pconnect is extremely inaccessible in this case.

A persistent database connection is a connection that is not closed when your script finishes running. When a request for a permanent connection is received. PHP will check to see if there is already an identical permanent connection (previously open). If it exists, the connection is used directly, or a new connection is established if it does not exist. A connection called "Same" means a connection with the same username and password to the same host.
Readers who do not fully understand the work and distribution of the WEB server may incorrectly understand the effect of a permanent connection. In particular, a permanent connection does not provide you with the ability to establish a "user session" on the same connection, nor does it provide the ability to effectively establish a transaction. In fact, in the strictest sense, a permanent connection does not give you any special functionality that cannot be provided by a non-persistent connection.
Why?
This is related to how the WEB server works. Your Web server can use PHP to generate Web pages in three different ways.
The first approach is to use PHP as a "shell." Running this way, PHP generates and ends a PHP interpreter thread for each PHP page request to your WEB server. Because the thread ends with the end of each request, any resource that is exploited in this thread, such as a connection to the SQL database server, is closed with the end of the thread. In this case, you will not get any change using the permanent connection

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.