Get the App
SLTechnology News&Howtos  ›  Database  › 

Failure to connect to database error handling scheme when mysql performs a large number of operations

Shulou Source: shulou.com Published: 2022-06-01 18:57:20 10月03日 Update

Next, let's learn about the error handling scheme when mysql can not connect to the database when performing a large number of operations. I believe you will benefit a lot after reading it. The text is not much in essence. I hope that mysql will not be able to connect to the database when performing a large number of operations. This short article is what you want.

Problem: when mysql performs a large number of insertion operations, it reports an error that it cannot connect to the database. The details of the error are as follows:

Fatal error: Uncaught exception 'PDOException' with message' SQLSTATE [HY000] [2003] Can't connect to MySQL server on' 127.0.0.1'(99)'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 (12): conn () # 2 / mnt/Change/www/html/test/ZB/addHDG.php (22): 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: when performing operations on MySQL, you need to connect to the database, the connection needs to occupy the port, and a large number of queries run out of ports, so the database cannot be connected.

Check the reason:

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

Netstat-nt

You will see a large number of connections when the TIME_OUT status

Solution.

Step 1: optimize mysql and close the mysql connection as soon as 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"); / / execute the following statement after getting the data to $pdo=null

Step 2: optimize the linux system

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

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

Explanation:

Net.ipv4.tcp_tw_recycle=1 means to enable fast TIME-WAIT sockets recycling, that is, to quickly recycle connections that are in TIME-WAIT. The default value is 0, that is, off.

Net.ipv4.tcp_fin_timeout: sets the connection timeout (in seconds). Default is 60 seconds.

Net.ipv4.tcp_tw_reuse = 1: allow socket in TIME-WAIT state to be reapplied for new TCP connections. The default value is 0, that is, closed state.

(2) execute in linux

Sysctl-p

Make the above configuration effective

After reading this article about how to deal with the failure to connect to the database when mysql performs a large number of operations, many readers will want to know more about it. For more industry information, you can follow our industry information section.

Tags: Data database error status scheme time out processing content reason more port industry statement affirmation benefit a lot information method unit finished that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB Linux Microsoft Shulou Technology