Home > Database > Mysql Tutorial > body text

Why Does My PHP MySQL Connection Timeout After 60 Seconds?

Linda Hamilton
Release: 2024-11-21 20:59:11
Original
226 people have browsed it

Why Does My PHP MySQL Connection Timeout After 60 Seconds?

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template