What is mysql connection?

Source: Internet
Author: User
$ DbnewPDO () at the underlying layer, what is the DB instance generated? In linux, everything is a file. I think this instance should also be a file. 1. What information does this file store? Assume that the connection of {code...} DB is not active for a long time. mysql will change from... $ db = new PDO ()
What is the DB instance generated at the underlying layer?

In linux, everything is a file. I think this instance should also be a file.
1. What information does this file store?

Assume that:

$db = new PDO(...)while(true){sleep(90000)    $db->xx()}

If the DB connection is not active for a long time, mysql will be automatically disconnected. After the connection is closed, the PHP request will report the MySQL server has gone away error.

2. How can I determine whether the db is disconnected before $ DB-> xx?

Reply content:

$ Db = new PDO ()
What is the DB instance generated at the underlying layer?

In linux, everything is a file. I think this instance should also be a file.
1. What information does this file store?

Assume that:

$db = new PDO(...)while(true){sleep(90000)    $db->xx()}

If the DB connection is not active for a long time, mysql will be automatically disconnected. After the connection is closed, the PHP request will report the MySQL server has gone away error.

2. How can I determine whether the db is disconnected before $ DB-> xx?

  1. What information does this file store?
    I didn't find the file. The instance objects are all in the memory.

  2. How can I determine whether the db is disconnected before $ DB-> xx?
    You can throw exceptions:


  query('SELECT * from FOO') as $row) {        print_r($row);    }    $dbh = null;} catch (PDOException $e) {    print "Error!: " . $e->getMessage() . "
"; die();}?>

PHP provides persistent connections
Persistent connection cache can avoid the overhead of creating a new connection between each script and the database, so that the web application can be faster.


   true));?>

RESOURCE

Mysql causes two interactive_timeout and wait_timeout actions.
You can extend the time by modifying the mysql configuration file/etc/my. cnf. The value is set to 10 seconds.
[Mysqld]

wait_timeout=10interactive_timeout=10

Solution:
Change the two parameters:

wait_timeout=100interactive_timeout=100

After the MySQL Server is restarted, check that the settings have taken effect.
Try it

Essentially, the connection to mysql is a commonsocketSocket, so it is alsoFile Descriptor. Since YestcpLink, so both parties have the right to disconnect the link at any time.mysqlThe server defines the connection timeout time to prevent excessive connections.mysqlResource usage.

GenerallyphpNot RequiredsleepWhat?pdoThe use of objects does not require connection timeout,pdoWill maintain everything.

if($db){ $db->xx() }

For more information, see the official documentation.

Essentially a memory buffer

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.