Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AndreeaX's avatar

General error: 2006 MySQL server has gone away VAPOR

Hi. Has anyone experienced issues with a VAPOR server when the lambda time limit is hit throwing "SQLSTATE[HY000]: General error: 2006 MySQL server has gone away" and just taking the whole server down? The vapor setup is: RDS MySQL 8.0db.t3.large50GB From what I can tell it happens if a job on the queue takes longer than 5 minutes. I rewrote/ split/batched/moved on separate queues so that is not an issue as they should not really last that long but I am trying to figure how can I ensure it just fails more gracefully as opposed to taking the full server down if that happens? Thank you

0 likes
1 reply
LaryAI's avatar
Level 58

It sounds like you're encountering a timeout issue with your MySQL server when running long jobs on AWS Lambda through Laravel Vapor. The "MySQL server has gone away" error typically happens when the connection between your application and the database server times out or is closed by the server.

Here are a few steps you can take to handle this more gracefully:

  1. Increase the MySQL wait_timeout and max_allowed_packet settings: These settings determine how long the server will wait for a connection to become active before closing it, and the maximum packet size for a single SQL statement or result set, respectively.

    You can adjust these settings in your RDS instance's parameter group. Here's an example of how to set them in the AWS RDS console:

    SET GLOBAL wait_timeout = 28800;
    SET GLOBAL max_allowed_packet = 1073741824; -- 1GB
    

    Remember to adjust these values according to your needs and monitor the performance.

  2. Implement job retries with backoff: Ensure that your queue jobs are set up to retry if they fail. You can specify the number of times a job should be retried and the delay between attempts. This can be done in the job class itself by setting the $tries and $backoff properties.

    public $tries = 3;
    public $backoff = 60; // Delay in seconds
    
  3. Catch the exception: Modify your job's code to catch the PDOException or QueryException that occurs when the MySQL server goes away. You can then handle it by either retrying the job or logging the error without taking down the server.

    try {
        // Your job logic here
    } catch (\PDOException $e) {
        if ($e->getCode() == 2006) {
            // Handle the server has gone away error
        }
    } catch (\Illuminate\Database\QueryException $e) {
        if ($e->getCode() == 2006) {
            // Handle the server has gone away error
        }
    }
    
  4. Optimize your jobs: Since you've mentioned that you've already split and batched your jobs, make sure they are optimized to run within the time limits imposed by AWS Lambda. If a job is likely to take longer, consider breaking it down further or offloading it to a different process that isn't time-bound.

  5. Monitor and alert: Set up monitoring and alerting for your RDS instance and Lambda functions. AWS CloudWatch can be used to monitor metrics and set alarms for when certain thresholds are reached, allowing you to respond quickly to issues.

  6. Database reconnection logic: In your database configuration, you can set up reconnection logic to attempt to reconnect to the database if the connection is lost. Laravel supports this out of the box with the retry_after option in the queue configuration.

    'connections' => [
        'mysql' => [
            // ...
            'options' => [
                PDO::ATTR_PERSISTENT => false,
                PDO::ATTR_EMULATE_PREPARES => true,
                PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'
            ],
        ],
    ],
    

Remember to test these changes in a staging environment before deploying to production to ensure that they have the desired effect and do not introduce new issues.

Please or to participate in this conversation.