Home > Database > Mysql Tutorial > body text

How to Fix the \'General Error: 2006 MySQL Server Has Gone Away\' During Bulk Operations?

Linda Hamilton
Release: 2024-10-30 22:20:30
Original
124 people have browsed it

How to Fix the

Resolving the "General Error: 2006 MySQL Server Has Gone Away" Issue

When performing bulk operations, such as inserting hundreds of records into a MySQL database, you may encounter the following error:

[PDOException] SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Copy after login

This error typically arises due to a timeout issue where the MySQL server disconnects your connection after a set period of inactivity. To resolve this issue, focus on adjusting two primary settings:

1. wait_timeout

The wait_timeout setting determines the duration (in seconds) that MySQL will wait before terminating an inactive connection. By default, this value is set to a relatively low 30 seconds on shared hosting environments.

To increase the wait_timeout, issue the following query at the start of your operation:

SET session wait_timeout=28800;
Copy after login

This command sets the wait_timeout to 28800 seconds, which is approximately 8 hours.

2. interactive_timeout (Optional)

In some cases, you might also need to adjust the interactive_timeout setting. This setting specifies the number of seconds before MySQL terminates an interactive connection.

To modify interactive_timeout, execute the following query after adjusting wait_timeout:

SET session interactive_timeout=28800;
Copy after login

Verification

To confirm that the settings have been successfully modified, you can execute the following query before and after making the changes:

<code class="php">$results = $db->query("SHOW VARIABLES LIKE '%timeout%'", TRUE);</code>
Copy after login

Cautions

While increasing the timeout settings can resolve the immediate issue, it's important to consider the following:

  • Setting wait_timeout to a very high value (e.g., 8 hours) may lead to excessive waiting for idle connections.
  • It's generally recommended to keep wait_timeout within a reasonable range, such as 300 seconds.

The above is the detailed content of How to Fix the \'General Error: 2006 MySQL Server Has Gone Away\' During Bulk Operations?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!