A detailed explanation of the difference between mysql_connect and Mysql_pconnect

Source: Internet
Author: User
Tags comments pconnect php script socket port number

PHP Mysql_pconnect

The Mysql_pconnect () function opens a persistent connection to the MySQL server.

Mysql_pconnect () and mysql_connect () are very similar, but there are two main differences:

1. When connecting, this function will first attempt to find a (persistent) connection that has been opened with the same username and password on the same host, and if found, returns this connection identity without opening a new connection.
2. Second, when the script completes the connection to the SQL server is not closed, the connection remains open for later use (Mysql_close () does not close the connection established by Mysql_pconnect ()).
Grammar
Mysql_pconnect (server,user,pwd,clientflag) parameter description
Server Optional. Specify the server to connect to.

You can include a port number, such as "Hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.

If the PHP instruction mysql.default_host is undefined (default), the default value is ' localhost:3306 '.

User selectable. User name. The default value is the user name of the server process owner.
PWD Optional. Password. The default value is a blank password.
Clientflag Optional. The Client_flags parameter can be a combination of the following constants:

Mysql_client_ssl-Using SSL encryption
mysql_client_compress-Using compression protocol
mysql_client_ignore_space-Allow the interval after the function name
mysql_client_interactive-Interactive timeout inactive time before the connection is allowed to close

return value
If successful, returns a MySQL persistent connection identifier and returns FALSE if an error occurs.
Tips and comments
Note: Optional parameter clientflag is available from the PHP 4.3.0 version.

Tip: To create a non-persistent connection, use the mysql_connect () function.
Example

The code is as follows Copy Code
<?php
$con = Mysql_pconnect ("localhost", "Mysql_user", "mysql_pwd");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}
?>


PHP mysql_connect


The mysql_connect () function opens a non-persistent MySQL connection.

Grammar
mysql_connect (server,user,pwd,newlink,clientflag) parameter description
Server Optional. Specify the server to connect to.

You can include a port number, such as "Hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.

If the PHP instruction mysql.default_host is undefined (default), the default value is ' localhost:3306 '.

User selectable. User name. The default value is the user name of the server process owner.
PWD Optional. Password. The default value is a blank password.
NewLink Optional. If mysql_connect () is invoked the second time with the same argument, no new connection will be established, and the connection ID that is already open will be returned. The parameter New_link changes this behavior and causes mysql_connect () to always open a new connection, even when mysql_connect () was previously invoked with the same parameters.
Clientflag Optional. The Client_flags parameter can be a combination of the following constants:

Mysql_client_ssl-Using SSL encryption
mysql_client_compress-Using compression protocol
mysql_client_ignore_space-Allow the interval after the function name
mysql_client_interactive-Interactive timeout inactive time before the connection is allowed to close

return value
If successful, returns a MySQL connection identity and returns FALSE if it fails.
Tips and comments
Note: As soon as the script is finished, the connection to the server is closed unless the mysql_close () has been explicitly invoked before.

Tip: To create a persistent connection, use the Mysql_pconnect () function.
Example

The code is as follows Copy Code

<?php
$con = mysql_connect ("localhost", "Mysql_user", "mysql_pwd");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}

Some code ...

Mysql_close ($con);
?>

The difference between mysql_connect and Mysql_pconnect

These two functions used on the poor soon, there is said on the Internet should use Pconnect, Pconnect is a good equipment, but also as pconnect as a scourge, decisive not to use pconnect, there are ambiguous positions. What about the equipment?
Forever link does not mean that the manager opens a connection, and then all the people share the link. Always connect is the same as each client to open a connection, there are 200 people interviewed there are 200 connections. In fact, Mysql_pconnect () itself did not do too much processing punishment, it only does not take the initiative to close the MySQL connection after the PHP operation stopped.
When PHP is running in a CGI format, pconnect and connect are the roots of the root zone. In addition, because the CGI format is a process of every PHP interview, the interview stop the left-behind is stopped, the information is fully released. When PHP is running in the Apache module format, because Apache has an application pool, a httpd process stops and is put back into the process pool, which allows the MySQL connection data that is opened with Pconnect to be released. Then there is the next connection that can be reused when pleading. This makes the interview speed faster when the number of Apache concurrent interviews is small, because pconnect is used, and PHP is frugal in repeatedly connecting DB. This should be a great effort to understand. But in the time of the Apache concurrent interview, if the application of pconnect, because some of the previous httpd process occupied by the MySQL connection does not close, it may be because MySQL has reached the largest even then, So that some of the subsequent entreaties will never be satisfied. If the maximum number of MySQL connections is set to 500, and Apache's maximum number of simultaneous interviews is set to 2000, assuming that all receive the request to receive DB, and the control time will be long, the current 500 supplications httpd have not stopped the hour, After the HTTD process is unable to connect to MySQL (since the maximum number of MySQL connections has been reached). Only the current 500 httpd processes are stopped or reused to connect to MySQL.
When the DB manipulation jumbled, time-consuming, because httpd will fork many concurrent process processing penalty, and the first generation of httpd process does not release DB connection, make the httpd process can not be connected to DB. Because this does not duplicate the other httpd process MySQL connection. So a lot of connection timeouts are generated. When the concurrent interview quantity is not high, the application pconnect can improve the interview speed simply, but when the concurrent quantity increases, whether or not to apply the pconnect will be the choice of the perceptive member.
In my opinion, PHP is now connected to MySQL is not really used to connect the pool, Pconnect is just the same as the use of the Apache process pool, so in the concurrent interview volume of time pconnect not grams of good progress received db effect.
In the actual application, with mysql_pconnect words, each refresh and supplications new pages are faster, and with mysql_connect words, each refresh must be begged from the beginning, when the database connection is slow, you can see the difference. When your database connection is slow, db manipulation is not very jumbled, and your application is conceited enough, will not be able to survive the lock time, or you have the right to handle the above four premises of the random two, then you can use Pconnect.
Pconnect in the script is closed, you can set up in MySQL lifetime, you can also write shell scheduled scan, kill off the long sleep connection. A word summary: To use good pconnect, not only PHP script things also related to the database and server settings.

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.