ホーム > Laravel > How to Resolve 'could not find driver' Error in Laravel
Laravel

How to Resolve 'could not find driver' Error in Laravel

Thank you for your continued support.
This article contains advertisements that help fund our operations.

Why 'could not find driver' happens in Laravel and how to fix it — what actually worked for me on Amazon Linux, plus researched notes for Ubuntu/macOS/Docker.

Error Message

Illuminate\Database\QueryException
could not find driver (Connection: mysql, SQL: ...)

This shows up when Laravel tries to connect to the database — typically on php artisan migrate or the first request that touches the DB.

Cause

This error means PHP is missing (or hasn't enabled) the PDO driver for your database.

Laravel connects to the database through PDO internally, so besides PHP itself, you need a separate extension for whichever database you're connecting to.

DatabaseRequired extension
MySQL / MariaDBpdo_mysql
PostgreSQLpdo_pgsql
SQLitepdo_sqlite
SQL Serverpdo_sqlsrv

In most cases, config/database.php is set up for a mysql connection, but pdo_mysql simply isn't installed.

How to Check

First, check whether the extension is actually loaded:

php -m | grep pdo

If you don't see an entry for your database (e.g. pdo_mysql), that's the cause.

CLI and your web server (php-fpm, etc.) can sometimes run different PHP binaries, so if you want to check what the web server sees, output phpinfo() for a more reliable check.

Solution (What Actually Fixed It for Me)

I hit this error on Amazon Linux 2023 (dnf), and installing php-mysqlnd fixed it.

sudo dnf install -y php-mysqlnd

Reference: Other Environments

The underlying cause is the same regardless of environment (a missing PDO driver), so installing the matching package should fix it elsewhere too.

I haven't personally tested these, but here's what I found while researching (swap mysql for pgsql etc. if you're connecting to a different database).

Ubuntu / Debian-based

apt install -y php-mysql reportedly does the same job.

sudo apt install -y php-mysql

You may need a version-specific package (e.g. php8.3-mysql) — apparently it's worth matching this to the PHP version from php -v.

macOS (Homebrew)

Homebrew's php package reportedly bundles pdo_mysql already.

If it's still missing from php -m, reinstalling reportedly fixes it in most cases.

brew reinstall php

Docker

You apparently need to install the extension explicitly in your Dockerfile.

RUN docker-php-ext-install pdo_mysql

docker-php-ext-install is only available on the official PHP images (php:*-fpm, etc.), from what I can tell.

If you're on a different base image, the package install command for that distribution should work instead.

After Installing

Installing the package alone doesn't always take effect immediately.

  • Restart php-fpm / Apache / Nginx
    sudo systemctl restart php-fpm
  • Check php.ini to make sure the extension isn't commented out (;extension=pdo_mysql)
  • For Docker, rebuild the container — a cached image layer can leave the old state in place

Run php -m | grep pdo again afterward to confirm the driver now shows up.

Background

While deploying a Laravel project on AWS EC2 (Amazon Linux 2023), I ran the following command and hit the error at the top of this post.

php artisan migrate

Beforehand, I had only run this to install PHP itself.

sudo dnf install -y php8.3

Unlike yum, dnf prints logs that make it look like it's automatically pulling in every related driver with a single command, so I assumed that was enough.

In reality, the PDO driver for MySQL (php-mysqlnd) needed to be installed as a separate package.

sudo dnf install -y php-mysqlnd

Running this and restarting php-fpm resolved it.

What to Include When Asking an AI Chat Tool

If you're asking an AI chat tool (ChatGPT, Claude chat, etc. — not an agent that can run commands itself), you'll need to describe your situation yourself.

Pasting the following gets you a much more accurate answer:

  • The full error message (including the stack trace, don't trim it)
  • Output of php -v (your PHP version)
  • Output of php -m (loaded extensions, especially anything starting with pdo_)
  • Your environment (OS and version — Amazon Linux 2023 / Ubuntu 22.04 / macOS + Homebrew / Docker, etc.)
  • The relevant connection block in config/database.php (which connection you're actually using)
  • The .env value for DB_CONNECTION (redact any secrets)
  • If you're using Docker, the PHP install section of your Dockerfile
  • Which fix from this article you already tried, and what happened (including whether the error message changed)

The php -m output and your environment are almost always essential for narrowing down the cause — send those two first if nothing else.

Summary

could not find driver doesn't mean PHP itself is broken — it almost always means the PDO extension for your target database isn't installed.

Check what's loaded with php -m, install the right package for your environment, and restart the service.

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!