PHP about MySQL Long connection problem

Source: Internet
Author: User

1, when the function mysql_connect the first three parameters ( server username password)相同,并且第四个参数(new_link)不传递时候,重复调用 mysql_connect will return the same connection.

PHP code

<? PHP $db mysql_connect (' localhost ', ' root ', ' root '); Var_dump ($db); $db 2 mysql_connect (' localhost ', ' root ', ' root '); Var_dump ($db 2); Sleep (10);

Page printing Information

Number of MySQL connections

Mysql> Show Full processlist;+----+------+-----------------+------+---------+------+-------+------------------ -----+| Id | User | Host            | db   | Command | Time | State | Info                  |+----+------+-----------------+------+---------+------+-------+-----------------------+| | root | localhost:54408 | NULL | Query   |    0 | NULL  | show full Processlist | | | | root | localhost:55278 | NULL | Sleep   |    3 |       | NULL                  |+----+------+-----------------+------+---------+------+-------+-----------------------+2 rows in Set

2, when the function mysql_connect passed the fourth parameter ( new_link ), and then repeated calls it.

PHP code

<? PHP $db mysql_connect (' localhost ', ' root ', ' root ', 1); Var_dump ($db); $db 2 mysql_connect (' localhost ', ' root ', ' root ', 1); Var_dump ($db 2); Sleep (10);

Page printing Information

Number of MySQL connections

Mysql> Show Full processlist;+----+------+-----------------+------+---------+------+-------+------------------ -----+| Id | User | Host            | db   | Command | Time | State | Info                  |+----+------+-----------------+------+---------+------+-------+-----------------------+| | root | localhost:54408 | NULL | Query   |    0 | NULL  | show full Processlist | | | | root | localhost:55320 | NULL | Sleep   |    4 |       | NULL                  | | | | root | localhost:55321 | NULL | Sleep   |    4 |       | NULL                  |+----+------+-----------------+------+---------+------+-------+-----------------------+

3. Mysql_pconnect Persistent Connection

PHP code

<? PHP $db Mysql_pconnect (' localhost ', ' root ', ' root '); Var_dump ($db); $db 2 Mysql_pconnect (' localhost ', ' root ', ' root '); Var_dump ($db 2);

Page printing Information

Number of MySQL connections

Mysql> Show Full processlist;+----+------+-----------------+------+---------+------+-------+------------------ -----+| Id | User | Host            | db   | Command | Time | State | Info                  |+----+------+-----------------+------+---------+------+-------+-----------------------+|  1 | Root | localhost:55391 | NULL | Query   |    0 | NULL  | show full Processlist | |  2 | Root | localhost:55393 | NULL | Sleep   |    5 |       | NULL                  |+----+------+-----------------+------+---------+------+-------+-----------------------+

4, Mysql_pconnect plus the fourth parameter (New_link) can establish multiple persistent connections

PHP code

<? PHP $db Mysql_pconnect (' localhost ', ' root ', ' root ', 1); Var_dump ($db); $db 2 Mysql_pconnect (' localhost ', ' root ', ' root ', 1); Var_dump ($db 2);

Page printing Information

Number of MySQL connections

Mysql> Show Full processlist;+----+------+-----------------+------+---------+------+-------+------------------ -----+| Id | User | Host            | db   | Command | Time | State | Info                  |+----+------+-----------------+------+---------+------+-------+-----------------------+|  1 | Root | localhost:55391 | NULL | Query   |    0 | NULL  | show full Processlist | |  2 | Root | localhost:55393 | NULL | Sleep   |  |       | NULL                  | |  3 | Root | localhost:55418 | NULL | Sleep   |    4 |       | NULL                  |+----+------+-----------------+------+---------+------+-------+-----------------------+3 rows in Set

5. Summary:

The persistent connection created by the function mysql_pconnetc after the PHP script ends, the MySQL connection does not end immediately. When it ends, it is managed by MySQL itself. The Mysql_colse function cannot close the MYSQL_PCONNETC connection.

The connection created by the function mysql_connetc will end when the PHP script ends, and the MySQL connection ends accordingly.

PHP about MySQL Long connection problem

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.