Differences between mysql_connect and mysql_pconnect

Source: Internet
Author: User
Tags pconnect
In mysql, we can see two common database connection modes: persistent connection and disconnection after page access, next I will introduce the differences between mysql_connect and mysql_pconnect respectively. you need to know more... in mysql, we can see two common database connection modes: persistent connection and disconnection after page access, next I will introduce the differences between mysql_connect and mysql_pconnect respectively. if you need to know, refer.

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 try to find a (persistent) connection that has been opened with the same user name and password on the same host. if it is found, the connection id is returned without a new connection.

2. second, after the script is executed, the connection to the SQL Server will not be closed. this connection will be enabled for future use (mysql_close () will not close the connection established by mysql_pconnect ).

Syntax:Mysql_pconnect (server, user, pwd, clientflag)

Parameter description

Optional. Specifies the server to be connected.

It can include the port number, such as "hostname: port", or the path to the local socket. for example, for localhost, ":/path/to/socket ".

If the PHP command mysql. default_host is not defined (default), the default value is 'localhost: 123456 '.

(Optional) user name. The default value is the user name of the server process owner.

Pwd (optional) Password. the default value is null.

Clientflag is optional. the client_flags parameter can be a combination of the following constants:

• MYSQL_CLIENT_SSL-SSL encryption

• MYSQL_CLIENT_COMPRESS-use the compression protocol

• MYSQL_CLIENT_IGNORE_SPACE-interval after function name

• MYSQL_CLIENT_INTERACTIVE-allow interaction timeout and inactivity before closing the connection

Return value,If the connection succeeds, a MySQL persistent connection identifier is returned. If an error occurs, FALSE is returned.

Tips and comments

Note:The clientflag is available in PHP 4.3.0.

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

The code is as follows:

 

PHP mysql_connect

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

Syntax:Mysql_connect (server, user, pwd, newlink, clientflag)

Parameter description

Server (optional) specifies the server to be connected.

It can include the port number, for example, "hostname: port", or the path to the local socket, for example, ":/path/to/socket" for localhost ".

If the PHP command mysql. default_host is not defined (default), the default value is 'localhost: 123456 '.

(Optional) user name. The default value is the user name of the server process owner.

Pwd (optional) Password. the default value is null.

Newlink (optional). if mysql_connect () is called for the second time with the same parameter, a new connection will not be established, but an opened Connection ID will be returned. the new_link parameter changes this behavior and causes mysql_connect () always open a new connection, even when mysql_connect () was previously called with the same parameter.

Clientflag is optional. The client_flags parameter can be a combination of the following constants:

• MYSQL_CLIENT_SSL-SSL encryption

• MYSQL_CLIENT_COMPRESS-use the compression protocol

• MYSQL_CLIENT_IGNORE_SPACE-interval after function name

• MYSQL_CLIENT_INTERACTIVE-allow interaction timeout and inactivity before closing the connection

Return value:If the connection succeeds, a MySQL connection id is returned. if the connection fails, FALSE is returned.

Tips and comments

Note:When the script ends, the connection to the server is closed, unless mysql_close () has been explicitly called before.

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

The code is as follows:

 

Differences between mysql_connect and mysql_pconnect

The usage of these two functions is not long ago. on the Internet, pconnect should be used. pconnect is a good equipment, and pconnect may be regarded as a flood of animals, so pconnect cannot be used, there are also vague positions. what about this equipment?

The persistent connection does not mean that the server opens a connection, and all people share the link. The same is true for the connection, there are 200 connections for 200 people. In fact, mysql_pconnect () itself has not done much punishment, it only does not take the initiative to close the mysql connection after the php stops running.

When php is run in cgi format, pconnect and connect are the root of the root node. In addition, because the cgi format is a process for every php contact, the process of stopping the contact will be stopped, all information is available.

When php runs in apache modular format, because apache has an application process pool, an httpd process will be put back into the process pool after it is stopped, this prevents the mysql connection data opened with pconnect from being released, so the data can be reused when there is a connection request. this makes the apache concurrency meet a small amount of time, because the application of pconnect, php frugal repeatedly connected to the database, making the interview speed faster.

This should be hard to understand, but in the apache concurrency interview a large amount of time, if the application of pconnect, because some of the previous httpd process occupied by the mysql connection is not close, it may be because mysql has reached the maximum connection, and some subsequent requests will never be satisfied. if the maximum number of connections in mysql is set to 500, and the maximum number of simultaneous accesses in apache is set to 2000, it is assumed that all accesses will receive db requests, and the operation will take a long time, the current 500 httpd requests have not been stopped, and subsequent httd processes cannot connect to mysql (because the maximum number of mysql connections has been reached ), mysql can be connected only when the current 500 httpd processes are stopped or reused.

When db operations are incorrect and take a long time, because httpd will fork a lot of concurrent processes to handle the penalty, the first httpd process will not open the db connection, making the subsequent httpd process unable to connect to the db. because there is no reuse of mysql connections in other httpd processes, a lot of connection timeout occurs. when the number of concurrent accesses is not high, the application of pconnect can simply improve the speed of access, however, when the concurrency increases, it is necessary to determine whether to apply pconnect again.

I personally think that php does not actually use the connection pool for mysql connections today. pconnect is just equivalent to using the apache process pool, therefore, pconnect is not inferior to the db efficiency in the case of a large number of concurrent interviews.

In actual applications, if mysql_pconnect is used, every time you refresh and pray for a new page, you will be fighting fast. if mysql_connect is used, every time you refresh the page, you will start from scratch, when the database connection is slow, you can see the difference. When your database connection is slow, DB operations are not very complicated, and your law is sufficient to be self-confident, you will not be able to lock your survival, or you have the right to grasp the server, to meet the above four premise random efficiency two, then you can use pconnect.

Pconnect does not need to be closed in the script. you can set the life time in mysql, or you can write a shell to scan on schedule, kill the connection with too long sleep. in one sentence, you should use pconnect, it is not only about php scripts, but also about database and server settings.

Address:

Reprinted at will, but please attach the article address :-)

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.