PHP Script with PDO Encountering "MySQL Server Has Gone Away" Error
A script performing nightly tasks is facing an issue where PDO prepared statements fail with the "MySQL server has gone away" error after several successful executions. The root cause of this issue often lies in exceeding the maximum allowed packet size set for MySQL.
Solution: Adjusting Server Configuration
To resolve the error, it is necessary to adjust the max_allowed_packet setting in the MySQL configuration file (my.ini). This setting specifies the maximum size of packets that the server can handle. In the given scenario, the issue likely arises when attempting to insert a BLOB larger than the permitted size.
Error Messages and Logger Output
When this issue occurs, the client-side displays the error message "MySQL server has gone away." Additionally, the server log may contain the following error if error logging is enabled:
Error 1153 Got a packet bigger than 'max_allowed_packet' bytes
Configuration Adjustment
To fix this issue, determine the size of the largest BLOB that will be inserted and set max_allowed_packet in my.ini accordingly. For instance, if the largest BLOB size is estimated to be 200MB, the configuration can be adjusted as follows:
[mysqld] ... max_allowed_packet = 200M ...
The above is the detailed content of Why does my PHP script using PDO keep getting a 'MySQL server has gone away' error after several successful executions?. For more information, please follow other related articles on the PHP Chinese website!