MySQL persistent connection (mysql_connect and mysql_pconnect)

Source: Internet
Author: User

In some cases, mysql_pconnect () is not applicable.

---------------------------

Status 1:

One Web server and one MySQL Server (the two may be on the same host) are used, while the Web server is fixed to only access a database on the MySQL server.

Because every time you access a database, the same database job on MySQL is used by the same account on the web side. If we change the "number of concurrent connections" between MySQL and web server to 200, it seems that MySQL has always had 200 "waiters" and is always waiting to receive 200 "customers" from the web. After the "customer" leaves, the "waiter" will not take a rest, always standing at the door waiting to receive the next "customer 」.

In this case, you only need to make sure that the "number of concurrent connections" of MySQL is higher or equal than that of the Web server, and you will find that using mysql_pconnect () is a good choice.
---------------------------

Status 2:

One Web server and one MySQL Server (the two may be on the same host) are used, while the Web server performs access operations on the two databases on the MySQL server.

When a data access request is raised from the Web server, sometimes it is for 1st databases (db1) and sometimes for 2nd databases (DB2 ). If we change the "number of concurrent connections" between MySQL and web server to 200, it is as if there are 200 "waiters" in MySQL 」, but there are two "bars" (db1 and DB2) at the same time, and "customers" may be up to 200.

At the beginning, the "bar" of db1 was quite popular, and MySQL sent 150 "waiters" to serve. Similarly, when "customers" left, the 150 "waiters" are still watching db1. Later, DB2 was also busy. There were more and more "customers". MySQL had to dispatch "waiters". How many experts were there? The answer is 50!

Why is the labor allocation of the "waiter" too limited? That's because the Web Side uses mysql_pconnect () to establish online. At the beginning, the "waiter" is specified to work at the "bar" and will stay there without turning the table 」.
---------------------------

Please note that when you use continuous online, each established online is only from the same web server, using the same group of accounts, and accessing the user services of the same database.

In this case, assume that the "number of concurrent connections" of each web server is 200, and what if two Web servers are used simultaneously? From web1 to 50 "customers", first to db1, and then to DB2, how many "waiters" need to receive them? 100 (web1-> db1: 50 web1-> DB2: 50 )! Another 50 "customers" came from web2, and the same action was done (web2-> db1: 50 web2-> DB2: 50 ). After that, are there other "waiters" idle? Who will deal with more than 50 "customers" from web1 or web2 at the same time?

If you are using a multi-process Web server such as Apache (a parent process coordinates a group of children processes), a Children process creates continuous online 」, it cannot be shared with other children processes. (the "waiter" serves only the "customer" previously received ). In this case, the idle processes on MySQL will accumulate more (many "waiters" stand at the door and wait for "old customers" to come to the door, instead of "new customers 」).
Is mysql_pconnect () the best choice? I don't think so.
--- Difference between the two -----
Mysql_pconnect () and mysql_connect () are very similar, but there are two main differences.
First, when the connection is established, this function will first try to find a (persistent) connection that has been opened with the same user name and password on the same host. If yes, the connection will be returned.

Id instead of opening a new connection.
Second, after the script is executed, the connection to the SQL server will not be closed, and the connection will remain open for future use (mysql_close () will not be closed

Mysql_pconnect () connection ).
The client_flags parameter is available in PHP 4.3.0 and later versions.
This connection is called "persistent ".
Here, write a code to test it.

/*
* Pconnect_test.php
*/
$ Link = mysql_pconnect ("localhost", "mysql_user", "mysql_password ")
Or die ("cocould not connect:". mysql_error ());
Print ("connected successfully ");

By refreshing the web page, we can find that the number of MySQL processes is increased every time this code is executed. I can't help but have doubts here. Mysql_pconnect

When using the number, it does not mean "when connected, this function will first try to find a (persistent) connection that has been opened with the same user name and password on the same host.

Is this identifier returned without a new connection? Why does it open a new connection to me every time I refresh the page?

Considering that this may be a PHP bug, I went to the PHP bug list to find entries related to too connector connections.
There are three main topics:

#11966 mysql_pconnect opens new connections with the same parameters
#26117 persistent connection not reused
#13589 persistent connections stay open and accumulate

The description is long and I will not post it here. You can view the specific content on your own. The key point is "when a process opens a MySQL persistent connection, as long as the process still exists, this persistent connection

The connection will not be disconnected, and each process will open a persistent connection to MySQL, rather than a persistent connection opened by other processes ".

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.