When MySQL performs a large number of operations, it reports that the database cannot be connected to the error

Source: Internet
Author: User
Tags stack trace

problem: when MySQL performs a large number of socialize operations, the report fails to connect to the database, error details are as follows:

Fatal error:uncaught exception ' pdoexception ' with message ' sqlstate[hy000] [2003] Can ' t connect to MySQL server on ' 127. 0.0.1 ' In/mnt/change/www/html/data/conn.php:5 Stack trace: #0/mnt/change/www/html/data/conn.php (5): pdo->__ Construct (' mysql:host=127 ... ', ' aaa ', ' AAA ') #1/mnt/change/www/html/data/conn.php: Conn () #2/mnt/change/www/ html/test/zb/addhdg.php: Sql_query (' Select Loopdata ... ') #3 {main} thrown in/mnt/change/www/html/data/conn.php on Line 5


operating Environment:Linux+apache+php+mysql

Reason: Yes when MySQL executes the operation, need to connect the database, the connection is required to occupy the port, a large number of queries to run out of ports, the database will not be able to connect

See why:

When performing a large number of MySQL connections, execute in linux

Netstat-nt

You 'll see a lot of connections. time_out Status

Solutions

Step one: Optimize MySQL and close the MySQL connection immediately after you get the data

(1)PDO only needs to set the connection to null

$pdo =new PDO ("Mysql:host=127.0.0.1;dbname=aaa", "AAA", "AAA"), and/or execute the following statement to $pdo=null after getting the data;


Step Two: Optimize your Linux system

(1) write the following statement in the /etc/sysctl.cnf

Net.ipv4.tcp_fin_timeout = 2net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1

Explain:

Net.ipv4.tcp_tw_recycle=1 is to turn on fast time-wait sockets Recovery, that is, to quickly reclaim the connection in time-wait , The default value is 0, which is the off state

Net.ipv4.tcp_fin_timeout : Set the connection time-out, in seconds, the default value is

Net.ipv4.tcp_tw_reuse =1: Allows the re-application of sockets in the time-wait State for new TCP Connection, the default value is 0, which is the off state

(2) execute in linux

Sysctl-p

Make the above configuration effective



This article is from the "Technology" blog, so be sure to keep this source http://heyu1.blog.51cto.com/13158032/1956343

When MySQL performs a large number of operations, it reports that the database cannot be connected to the error

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.