Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

php artisan migrate stops with could not find driver

Errorcould not find driver (Connection: mysql, SQL: select * from information_schema.tables ...)

Short answer

Laravel talks to the database through a PDO driver, and this error means the PHP that runs artisan has none for your DB_CONNECTION. List the drivers that PHP has, install the matching extension (pdo_mysql for MySQL or MariaDB), and make sure a cached config is not still pointing at an old connection.

  • Applies to PHP 8.3, Laravel 12, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the PHP that runs artisan which PDO drivers it has, then compare with the connection Laravel is using. The driver for your database must be in the list.

    php -r 'print_r(PDO::getAvailableDrivers());'
    grep '^DB_CONNECTION' .env
  2. 2Match what you see, then jump to its fix

    1. 1The driver list from the first step is empty or has no mysql entry while DB_CONNECTION=mysql or mariadb.The PDO driver extension for your database is not installedFix
    2. 2Your .env says sqlite, pgsql or sqlsrv but only another PDO driver is loaded.DB_CONNECTION names a driver whose extension you never installedFix
    3. 3You changed .env but artisan keeps using the previous driver.A cached config still holds the old connectionFix

What does the error look like?

  • could not find driver (Connection: mysql, SQL: select * from information_schema.tables ...)

Cause 1 of 3

The PDO driver extension for your database is not installed

1Check

On Linux the PHP packages often leave the database extensions as a separate package. Confirm that pdo_mysql is not loaded.

php -m | grep -i pdo
php -r 'var_dump(extension_loaded("pdo_mysql"));'

2Fix

Install the MySQL extension package that matches your PHP build. The PHP manual says the extension libraries may need an additional package on top of PHP. The manual names php-mysql on CentOS and a php5-mysql package on an old Ubuntu, so look up the name for your PHP release; on a source build, configure PHP with --with-pdo-mysql.

# example only: the package name depends on your distribution and PHP release
sudo apt install php-mysql

3Verify

The driver list now contains mysql.

php -r 'print_r(PDO::getAvailableDrivers());'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

DB_CONNECTION names a driver whose extension you never installed

1Check

Laravel supports MySQL, MariaDB, PostgreSQL, SQLite and SQL Server. Each one needs its own PHP extension, and the SQL Server connection needs both sqlsrv and pdo_sqlsrv. SQLite is an equally valid setup, so a project set up for it fails on a server that only has the MySQL driver.

grep -E '^DB_(CONNECTION|HOST|DATABASE)' .env
php artisan db:show

2Fix

Either set DB_CONNECTION (and the host, database, user and password) to the database you really run, or install the extension for the one you named. For SQLite also create the database file the docs describe.

# .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=app
DB_USERNAME=app
DB_PASSWORD=change-me

3Verify

Laravel can now open the connection and describe it.

php artisan db:show

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

A cached config still holds the old connection

1Check

Once config:cache has run, Laravel stops reading .env, so edits there have no effect until the cache is rebuilt.

ls bootstrap/cache/

2Fix

Clear the cached files that the optimize command created, then run the migration again.

php artisan optimize:clear
php artisan migrate

3Verify

The migration runs against the connection written in .env.

php artisan migrate:status

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • List PDO::getAvailableDrivers() on every new server before the first deploy.
  • Check the command-line PHP and the PHP your web server uses separately; they can be different installs.
  • Keep the database settings in .env.example so a new machine starts from the same connection.
  • Run config:cache only in deployment, and clear it when you change environment values.

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.