Problem: MySQL Server Connection Timeout after 60 Seconds, Error "MySQL server has gone away - in exactly 60 seconds"
The issue described stems from a timeout related to the PHP option mysql.connect_timeout. This option not only affects the connection timeout but also the time it takes to receive the first response from the MySQL server.
Root Cause:
The mysql.connect_timeout option is typically set to 60 seconds by default. When a PHP script executes a query that takes longer than 60 seconds to execute on the MySQL server, the PHP script will time out and throw the "MySQL server has gone away" error.
Solution:
To resolve this issue, increase the mysql.connect_timeout option to a value greater than the expected execution time of your query. This will allow the PHP script to wait longer for a response from the server before timing out.
You can modify the mysql.connect_timeout option using the ini_set() function in your PHP script:
ini_set('mysql.connect_timeout', 300); // Set to 300 seconds ini_set('default_socket_timeout', 300); // Set the socket timeout to match the connect timeout
By increasing the mysql.connect_timeout option, you effectively extend the waiting period for the PHP script to receive a response from the MySQL server, thus preventing the "MySQL server has gone away" error.
The above is the detailed content of Why Does My PHP MySQL Connection Timeout After 60 Seconds?. For more information, please follow other related articles on the PHP Chinese website!