Laravel SSL connection to MySQL not working

Viewed 1491

I'm working on a project where I have to use SSL to connect to the MySQL database. This is working in vanilla PHP, but not when I try to use Laravel.

Working PHP-code:

<?php
$host = 'xxx';
$db = 'xxx';
$user = 'xxx';
$pass = 'xxx';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
    PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
    PDO::MYSQL_ATTR_SSL_CA => "../rootCA.pem",
];

try {
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
    throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

$data = $pdo->query('SELECT * FROM x LIMIT 100')->fetchAll(PDO::FETCH_ASSOC);
var_export($data);

But when I try to connect via Laravel, then i get the following error:

C:\*\*\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 330
[Fri Sep 14 11:35:05 2018] ::1:54869 [500]: /data - Allowed memory size of 134217728 bytes exhausted (tried to allocate 4096 bytes) in C:\*\*\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 330

My Laravel (5.7) config for the connection:

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'xxx'),
        'port' => env('DB_PORT', 'xxx'),
        'database' => env('DB_DATABASE', 'xxx'),
        'username' => env('DB_USERNAME', 'xxx'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
        'options' => array(
            PDO::MYSQL_ATTR_SSL_CA  => '../rootCA.pem',
            PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
        ),
    ],

Any ideas? =)

2 Answers

You need to provide full path of files.

'options' => [
    PDO::MYSQL_ATTR_SSL_KEY => base_path('ssl/client-key.pem'),
    PDO::MYSQL_ATTR_SSL_CERT => base_path('ssl/client-cert.pem'),
    PDO::MYSQL_ATTR_SSL_CA => base_path('ssl/ca-cert.pem')
]

Please see link: How do I connect to a MySQL database over SSL with Laravel 5.3

The problem was not with the connectcion it self. It was that I tried to fetch to many rows from my SQL-query which (really strange) gave me error messages like:

SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number

etc. So my bad - but I was hoping that the error messages could have been better.

Related