3

When I tried to install Laravel 5.8 it throws Error

In database.php line 58:

Undefined class constant 'MYSQL_ATTR_SSL_CA'

enter image description here

After this I have tried to run the application on server. It works fine sometimes. Sometimes it throws the same error. I couldn't run any commands on Artisan too. Why this happens and How to solve this?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Karthik SWOT
  • 1,129
  • 1
  • 11
  • 15
  • 1
    Finally its cleared now when i just disable this following 3 line by commenting in database.php //'options' => array_filter([ //PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), //]), – Karthik SWOT Feb 28 '19 at 17:56
  • I answered you, but good to see you've found the solution yourself! – KeitelDOG Feb 28 '19 at 18:13

2 Answers2

8

New Laravel releases have this error. Look in config/database.php, you will see something like :

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => array_filter([
             PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]),

Secured Applications have an environment file that contains data for specific machine and software configurations like Database name and password, Email and password, value to tell if it's for development or production, etc.

Laravel loads them in constant accessible via global function env(). There is a file .env that contains those special values in Laravel. So open it and at the bottom of Database section, add your certificate path value:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog-db
DB_USERNAME=root
DB_PASSWORD=fakepass
MYSQL_ATTR_SSL_CA=relative/or/absolute/path/to/certificate.extension

Or if you're not planning to use SSL certificate for MySQL connection like most of us, then just comment it in config/database.php :

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        //'options' => array_filter([
        //     PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        //]),

Like it is currently at Laravel/Laravel master : https://github.com/laravel/laravel/blob/master/config/database.php

KeitelDOG
  • 4,750
  • 4
  • 18
  • 33
1

If you don't have MySQL extension, you should use:

'options' => extension_loaded('pdo_mysql') ? array_filter([
    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],

in config/database.php instead of

'options' => array_filter([
     PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]),

It's now included by default in Laravel 5.8 https://raw.githubusercontent.com/laravel/laravel/master/config/database.php (though it's probably not released yet).

So just update code with this above and you are good to go.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291