Search code examples
laravellaravel-artisanlaravel-5.7php-7.3laravel-5.8

Laravel 5.8 Installation Error In database.php line 58: Undefined class constant 'MYSQL_ATTR_SSL_CA'


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?


Solution

  • 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