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

localpathcomp's avatar

PDO SSL Connection Reset By Peer

Hey everyone. Have a qq.

I started using a cert bundle to force our connection to mariadb to use ssl. Everything works ok but a few times a day I get this weird exception from the DB facade.

Illuminate\Database\Connection::setPdo(): SSL: Connection reset by peer

Is using DB::Purge() even necessary in modern laravel when switching a db connection for a tenant?

I see we can use reconnect. Is purge maybe not needed and reconnect would connect to the instance ok?

Just wondering if there's any tips when using ssl and a multitenant setup.

Laravel 9.x Mariadb 10.6 PHP 8.0

The databases are all in the same RDS instance

0 likes
1 reply
LaryAI's avatar
Level 58

It sounds like you're encountering intermittent SSL connection issues with your MariaDB database when using Laravel's database abstraction layer. The error "SSL: Connection reset by peer" typically indicates that the connection to the database server was unexpectedly closed by the server.

Here are a few tips and potential solutions to address this issue:

  1. Persistent Connections: Check if you're using persistent connections, which can sometimes cause issues with SSL. You can disable persistent connections in your database configuration to see if that resolves the issue.

  2. Reconnect vs. Purge: In modern Laravel applications, using DB::reconnect($connectionName) is often sufficient to re-establish a database connection that has been lost. The reconnect method will close the current connection and immediately attempt to open a new one. The DB::purge($connectionName) method, on the other hand, removes the connection from the connection pool entirely, which is not usually necessary unless you're changing configuration settings.

  3. Database Configuration: Ensure that your database configuration is correctly set up for SSL. In your config/database.php, you should have something like this for your MariaDB connection:

'mysql' => [
    // ...
    'options'   => [
        PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca-cert.pem',
        PDO::MYSQL_ATTR_SSL_CERT => '/path/to/client-cert.pem',
        PDO::MYSQL_ATTR_SSL_KEY => '/path/to/client-key.pem',
        // Add any other SSL options you need
    ],
    // ...
],
  1. Error Handling: Implement error handling that can catch PDOException and attempt a reconnection if an SSL error occurs. This can be done in a middleware or wherever you handle your database logic.

  2. Server Configuration: Ensure that your MariaDB server is properly configured to handle SSL connections. Check the server logs to see if there are any indications of why the connection is being reset.

  3. Connection Limits: Check if you're hitting any connection limits on your RDS instance that might be causing connections to be dropped.

  4. Update Dependencies: Make sure you're using the latest versions of PHP, PDO, and any other relevant dependencies, as there may be bug fixes related to SSL connections.

  5. Laravel Events: Listen to Laravel's database events to log when connections are made or lost. This can help you identify patterns or specific circumstances that lead to the "Connection reset by peer" error.

Here's an example of how you might handle a reconnection attempt in a middleware:

public function handle($request, Closure $next)
{
    try {
        return $next($request);
    } catch (\Illuminate\Database\QueryException $e) {
        if ($this->shouldReconnect($e)) {
            DB::reconnect();
            return $next($request);
        }

        throw $e;
    }
}

protected function shouldReconnect($exception)
{
    $message = $exception->getMessage();
    return Str::contains($message, ['server has gone away', 'no connection to the server', 'Lost connection', 'is dead or not enabled', 'SSL: Connection reset by peer']);
}

Remember to import the necessary classes at the top of your middleware file.

Finally, if none of these solutions work, consider reaching out to the support for your RDS instance or consulting the Laravel community for more specific advice related to your setup.

Please or to participate in this conversation.