DEV Community

Vincent Tommi
Vincent Tommi

Posted on

Fixing PDO::MYSQL_ATTR_SSL_CA Deprecation Errors in PHP 8.5 and Laravel

If you've recently upgraded to PHP 8.5 and started seeing deprecation warnings in a Laravel application, you may run into an error similar to this:

Deprecated: Constant PDO::MYSQL_ATTR_SSL_CA is deprecated since 8.5,
use Pdo\Mysql::ATTR_SSL_CA instead
Enter fullscreen mode Exit fullscreen mode

In my case, the warning was pointing to:

config/database.php
Enter fullscreen mode Exit fullscreen mode

at line 62.

Here's how I fixed it and what I learned along the way.

The Problem

After upgrading to PHP 8.5, I ran:

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

Instead of clearing the configuration cache, Laravel returned:

Deprecated: Constant PDO::MYSQL_ATTR_SSL_CA is deprecated since 8.5,
use Pdo\Mysql::ATTR_SSL_CA instead

In database.php line 62:
Undefined constant PDO::mysql_ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

The important part is:

Undefined constant PDO::mysql_ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

This happened because the constant had been changed incorrectly.

The Old Constant

Older PHP versions commonly use:

PDO::MYSQL_ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

PHP 8.5 deprecates this form and recommends the MySQL-specific class instead.

The new form is:

Pdo\Mysql::ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

Notice that PHP class and constant names are case-sensitive.

Incorrect

PDO::mysql_ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

Correct

Pdo\Mysql::ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

The difference is important:

PDO::mysql_ATTR_SSL_CA
^^^  ^^^^^^^^^^^^^^^

Pdo\Mysql::ATTR_SSL_CA
^^^  ^^^^^ ^^^^^^^^^
Enter fullscreen mode Exit fullscreen mode

Fixing database.php

Open your Laravel project's database configuration:

config/database.php
Enter fullscreen mode Exit fullscreen mode

Look for:

PDO::MYSQL_ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

Replace it with:

Pdo\Mysql::ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

For example, if you have:

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

change it to:

'options' => extension_loaded('pdo_mysql') ? array_filter([
    Pdo\Mysql::ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
Enter fullscreen mode Exit fullscreen mode

What If You Can't Edit the File With Nano?

If you're using Ubuntu and nano isn't working for you, there are several alternatives.

Using VS Code

If VS Code is installed:

code config/database.php
Enter fullscreen mode Exit fullscreen mode

Or use the full path:

code /home/tommi/Desktop/Development/programmer/samupos/config/database.php
Enter fullscreen mode Exit fullscreen mode

Then replace:

PDO::MYSQL_ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

with:

Pdo\Mysql::ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

Using the Terminal

If you only need to replace the constant, you can use sed:

sed -i 's/PDO::mysql_ATTR_SSL_CA/Pdo\\Mysql::ATTR_SSL_CA/g' config/database.php
Enter fullscreen mode Exit fullscreen mode

If you get a permission error, you can use:

sudo sed -i 's/PDO::mysql_ATTR_SSL_CA/Pdo\\Mysql::ATTR_SSL_CA/g' config/database.php
Enter fullscreen mode Exit fullscreen mode

Then verify the change:

grep -n "SSL_CA" config/database.php
Enter fullscreen mode Exit fullscreen mode

You should see:

Pdo\Mysql::ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

Verify That PHP Supports the New Constant

Before trying Laravel again, you can check whether PHP recognizes the new constant:

php -r 'var_dump(Pdo\Mysql::ATTR_SSL_CA);'
Enter fullscreen mode Exit fullscreen mode

If everything is working correctly, PHP should return an integer value.

For example:

int(10023)
Enter fullscreen mode Exit fullscreen mode

The exact integer isn't the important part. What matters is that PHP recognizes the constant without throwing an error.

Clear Laravel's Configuration Cache

Once the constant has been corrected, go back to your Laravel project:

cd /home/tommi/Desktop/Development/programmer/samupos
Enter fullscreen mode Exit fullscreen mode

Then run:

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

You can also rebuild the configuration cache:

php artisan config:cache
Enter fullscreen mode Exit fullscreen mode

If those commands complete successfully, Laravel should no longer complain about:

PDO::MYSQL_ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

Another PHP 8.5 Warning

During this process, I also encountered another warning:

Deprecated: Using null as an array offset is deprecated, use an empty string instead
Enter fullscreen mode Exit fullscreen mode

It pointed to:

vendor/laravel/framework/src/Illuminate/Log/LogManager.php
Enter fullscreen mode Exit fullscreen mode

This is a different issue from the PDO constant.

It's important not to directly edit files inside vendor/.

The vendor directory contains dependencies installed by Composer, so changes made there can be overwritten the next time you run:

composer install
Enter fullscreen mode Exit fullscreen mode

or:

composer update
Enter fullscreen mode Exit fullscreen mode

Instead, check your Laravel version:

php artisan --version
Enter fullscreen mode Exit fullscreen mode

and your PHP version:

php -v
Enter fullscreen mode Exit fullscreen mode

If you're running an older Laravel version with PHP 8.5, you may need to update Laravel or one of its dependencies to a version that properly supports PHP 8.5.

Don't Mix Ubuntu PPAs Just to Fix This

Another potential problem I encountered while setting up PHP was an Ubuntu repository error involving the Ondřej PHP PPA:

The repository 'https://ppa.launchpadcontent.net/ondrej/php/ubuntu resolute Release'
does not have a Release file.
Enter fullscreen mode Exit fullscreen mode

If you're running a newer Ubuntu release and the PPA doesn't provide packages for that release, don't simply change the distribution name in the repository from something like:

resolute
Enter fullscreen mode Exit fullscreen mode

to:

noble
Enter fullscreen mode Exit fullscreen mode

just to make apt update work.

Mixing packages from different Ubuntu releases can cause dependency problems.

Instead, verify what PHP version and packages your current Ubuntu release provides.

Useful Diagnostic Commands

When troubleshooting PHP and Laravel compatibility issues, these commands are useful:

Check PHP version

php -v
Enter fullscreen mode Exit fullscreen mode

Check Laravel version

php artisan --version
Enter fullscreen mode Exit fullscreen mode

Check whether PDO MySQL is installed

php -m | grep pdo_mysql
Enter fullscreen mode Exit fullscreen mode

Expected output:

pdo_mysql
Enter fullscreen mode Exit fullscreen mode

Check the new PHP 8.5 constant

php -r 'var_dump(Pdo\Mysql::ATTR_SSL_CA);'
Enter fullscreen mode Exit fullscreen mode

Find the SSL constant in Laravel's configuration

grep -n "SSL_CA" config/database.php
Enter fullscreen mode Exit fullscreen mode

Final Fix

The key change is simply:

- PDO::MYSQL_ATTR_SSL_CA
+ Pdo\Mysql::ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

Then clear Laravel's configuration:

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

and, if necessary:

php artisan config:cache
Enter fullscreen mode Exit fullscreen mode

Conclusion

PHP upgrades can expose deprecated functionality that previously worked without any problems. PHP 8.5 is a good example of this: code using the old PDO::MYSQL_ATTR_SSL_CA constant can start producing deprecation warnings.

The important thing is to distinguish between a deprecation warning, an actual PHP error, and a framework compatibility problem.

For this particular issue, the solution is straightforward:

Pdo\Mysql::ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

instead of:

PDO::MYSQL_ATTR_SSL_CA
Enter fullscreen mode Exit fullscreen mode

And when another warning points into vendor/, resist the temptation to edit the dependency directly. Check your Laravel and package versions first and upgrade them where appropriate.

Hopefully this saves someone else a few minutes when upgrading a Laravel project to PHP 8.5.

Top comments (0)