PDO::MYSQL_ATTR_INIT_COMMAND is an undefined class constant
Fatal error: Uncaught Error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'Short answer
PDO only defines the MYSQL_ATTR_ constants when the pdo_mysql driver is loaded in the PHP process that runs your code. If the constant is undefined, the extension is missing there, or the constant is written with the wrong class name. PHP 8.4 added Pdo\Mysql::ATTR_INIT_COMMAND, and the old name is deprecated in PHP 8.5.
Fix it fast
1Find out which cause you have
Ask the same PHP that runs your site, not only the terminal, which drivers it has. The web server and the command line can load different php.ini files.
<?php print_r(PDO::getAvailableDrivers()); var_dump(extension_loaded('pdo_mysql'), PHP_VERSION);2Match what you see, then jump to its fix
- 1
mysql is missing from the driver list, and extension_loaded('pdo_mysql') is falseThe pdo_mysql extension is not loaded in this PHPFix - 2
The driver is loaded, but the error names a class such as Pdo\Mysql with a MYSQL_ATTR_ name, or a bare MYSQL_ATTR_INIT_COMMANDThe constant is spelled for a different class than the one you useFix - 3
a fatal error saying the Pdo\Mysql class cannot be found, while PHP_VERSION is below 8.4The Pdo\Mysql class is used on a PHP older than 8.4Fix
- 1
What does the error look like?
Fatal error: Uncaught Error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'Undefined constant PDO::MYSQL_ATTR_INIT_COMMANDPDOException: could not find driver
Cause 1 of 3
The pdo_mysql extension is not loaded in this PHP
1Check
The MYSQL_ATTR_ constants belong to the pdo_mysql driver. Without it there is no such constant, and the connection itself fails with could not find driver.
php -m | grep -i pdo
php --ini2Fix
Install or enable the extension for the PHP version you run. On Debian and Ubuntu the distribution package that provides it is usually php-mysql; on a source build it is the --with-pdo-mysql option. Then reload the PHP service.
sudo apt install php-mysql
sudo systemctl reload php8.3-fpm3Verify
mysql now appears in the list of drivers, in the web request as well.
php -r 'print_r(PDO::getAvailableDrivers());'Confirmed by php.net
Cause 2 of 3
The constant is spelled for a different class than the one you use
1Check
The classic name is PDO::MYSQL_ATTR_INIT_COMMAND. From PHP 8.4 the same option is Pdo\Mysql::ATTR_INIT_COMMAND, without the MYSQL_ prefix. A bare name, or a mix of the two forms, is undefined.
grep -rn "MYSQL_ATTR_INIT_COMMAND\|ATTR_INIT_COMMAND" app/ src/ config/2Fix
Use one valid form. This one works on old and new PHP: it picks the new constant when it exists. The option must be passed in the constructor's options array, because it runs when the connection opens.
$init = defined('Pdo\Mysql::ATTR_INIT_COMMAND')
? Pdo\Mysql::ATTR_INIT_COMMAND
: PDO::MYSQL_ATTR_INIT_COMMAND;
$pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'secret', [
$init => 'SET NAMES utf8mb4',
]);3Verify
Run the script and select something to prove the connection opens.
php -r 'require "connect.php"; echo $pdo->query("SELECT 1")->fetchColumn();'Confirmed by php.net
Cause 3 of 3
The Pdo\Mysql class is used on a PHP older than 8.4
1Check
The Pdo\Mysql class is available from PHP 8.4.0. Older versions only know the PDO::MYSQL_ATTR_ names.
php -v2Fix
On PHP 8.3 and older keep PDO::MYSQL_ATTR_INIT_COMMAND, or use the version-safe snippet above. Do not copy 8.4-only code into an older runtime.
$pdo = new PDO($dsn, $user, $pass, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4']);3Verify
The script connects without a fatal error.
php -l connect.php && php connect.phpConfirmed by php.net
How do I stop it coming back?
- Pin the PHP version and the extension list in your Dockerfile or server provisioning, so the web and CLI runtimes match.
- Use the version-safe snippet in shared code, because the PDO::MYSQL_ constants are deprecated as of PHP 8.5.
- Set the connection charset in the DSN (charset=utf8mb4) when that is all the init command did.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
PHP throws "could not find driver" when it opens a PDO connection
Fatal error: Uncaught PDOException: could not find driver in /var/www/app/db.php:12
PDO is only a layer; each database needs its own driver extension, such as pdo_mysql or pdo_pgsql, and the DSN prefix must name a driver that is loaded.
4 causes, each with a fixBack end
WordPress says PHP is missing the MySQL extension
Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
WordPress lists mysqli among the PHP extensions it requires, because it uses it to talk to MySQL or MariaDB.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
CodeIgniter 4 cannot write its cache files
CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/
CodeIgniter 4's file cache needs a cache directory that the web server can really write to.
2 causes, each with a fix