Mysql_connect () and mysql_pconnect ()

Source: Internet
Author: User
Tags mysql connect pconnect

Mysql_connect () and mysql_pconnect (). The former is used to establish a new connection each time, while the latter is a continuous connection.

Mysql_connect () establishes one or more new connections each time the current php page is called, and closes these connections after the request ends. This method is applicable to pages that are not too heavy and can be used internally without any adjustments.
Mysql_pconnect () will also create a new connection when the page is called, but the connection will not be closed after the request ends. Instead, it will save the connection in the connection pool, such a concurrent request can continue to use this connection. This connection mode is provided to pages that are frequently used. Otherwise, the resources will be frequently turned on and off, which seriously affects the performance. P in mysql_pconnect () is the first letter of the word persistent (permanent.

Mysql_pconnect () requires server adjustment, limit the number of connections, configure timeout, and when to process idle processes. It allows you to improve efficiency in the right environment.
Pconnect often causes Mysql connection failure and prompts too many connections because Apache does not automatically close mysql connections after pconnect.
Let's take a look at the working mode of APACHE.
In Windows, Apache uses a main process, adds a secondary process, and then derives N threads from the secondary process to provide services. The number of threads can be found in httpd. configuration in conf: ThreadsPerChild 500. If it is set to 500 threads, apache will start 500 threads as soon as it is started, but only 500 threads can be used at most, if the number of connections exceeds 500 at the same time (300 connections may be accessed by 500 users, and the current connection method is determined, you can use netstat-na | grep 80 | grep EST | wc-l or the apache status module). Then, redundant connections will be waiting or connection failure. (In Windows, the main configuration parameter of Apache should be ThreadsPerChild. First, based on the current number of connections, check whether it is necessary to increase the value. Generally, setting the value of 1000 on a PC server is relatively large .)
In Nix, Apache uses the process method to run. The principle is the same. There are several parameters to adjust the number of processes, such as ServerLimit.

Let's take a look at how Apache + PHP + Mysql_pconnect works.
Every time a client sends a connection request (including images, HTML, PHP, etc.) to the server, apache will use a thread to accept the request. If the request is a PHP file, if PConnect is used in the PHP file, the current thread will determine whether pconnect has been opened by the current thread. If PConnect has been opened, the original mysql connect is used. If pconnect has not been opened, A new connect is created. After the connection is disconnected, the thread is still running and Mysql connect is maintained. after running in this way for a period of time, it is entirely possible that all apache threads have opened the Php page with Pconnect. Therefore, if apache's ThreadsPerChild = 500, if mysql connections are not closed for all the 500 threads, the connection count of mysql must be greater than or equal to 500, the PHP page prompts a database connection failure.

Therefore, it is concluded that when pconnect is used in Apache + PHP + Mysql, max_connect of mysql must be greater than or equal to the maximum number of threads (processes) of apache. on a site with a large access volume, pconnect may be impractical. The best way is to generate the database content as static files as much as possible without connecting to the database on every page, and use mysql_connect (even if the vast majority of pages are generated as static files but mysql_pconnect still exists, mysql's max_connect is also required to be larger than the number of apache threads, in this case, pconnect is not desirable ).

A permanent database connection is a connection that is not closed when your script ends running. When receiving a permanent connection request. PHP will check whether there is already a permanent connection (previously Enabled. If yes, the connection is directly used. If no, a new connection is established. The "same" connection refers to the connection to the same host with the same user name and password.
Readers who do not fully understand the work and distributed load of WEB servers may mistakenly understand the role of permanent connections. In particular, permanent connections do not provide you with the ability to establish user sessions on the same connection, nor do you provide the ability to effectively establish transactions. In fact, in a strict sense, permanent connections do not provide you with any special features that are not available for non-permanent connections.
Why?
This is related to how the WEB server works. Your WEB server can generate WEB pages using PHP in three ways.
The first method is to use PHP as a "shell ". Run in this way, PHP will generate and end a PHP interpreter thread for each PHP page request submitted to your WEB server. Since this thread ends with the end of each request, any resources used in this thread (such as connections to the SQL database server) will be closed with the end of the thread. In this case, using a permanent connection will not be changed.

Related Article

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.