If you are developing a Laravel application on Windows and suddenly encounter this error:
PDOException: could not find driver Don't panic. You probably don't need to reinstall Laravel, Composer, or even PHP.
I recently encountered this issue while working on my Laravel project, SamuPOS, after migrating my development environment to Windows 11.
The problem was related to PHP database extensions, specifically the missing or disabled pdo_mysql extension.
In this tutorial, I'll walk you through how to identify the problem, enable the required PHP extension, and verify that Laravel can communicate with your MySQL database.
Whether you are a beginner or an experienced developer setting up Laravel on Windows, this guide should help you troubleshoot the issue.
1. Understanding the Error
The error looked something like this:
C:\Users\Backend-Nerd\OneDrive\Desktop\Development\samupos\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:65 PDOException: could not find driver But what does it actually mean?
Laravel uses PHP's PDO (PHP Data Objects) to communicate with databases.
PDO provides a common interface for connecting to different database systems, including:
- MySQL
- PostgreSQL
- SQLite
- SQL Server
However, PDO alone is not enough. PHP also needs the appropriate database driver.
For example:
| Component | Purpose |
|---|---|
| PDO | Provides the database connection interface |
| pdo_mysql | Allows PHP to communicate with MySQL through PDO |
| mysqli | Provides another way to communicate with MySQL |
| mysqlnd | MySQL native driver used by PHP |
In my case, PHP had PDO installed, but the MySQL PDO driver was missing or disabled.
This prevented Laravel from establishing a database connection.
2. Identify the PHP Configuration File
Before changing anything, we need to establish which PHP configuration file our terminal is using.
Open your VS Code terminal or PowerShell and execute:
php --ini My output was:
Configuration File (php.ini) Path: Loaded Configuration File: C:\php\php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none) The important line is:
Loaded Configuration File: C:\php\php.ini This tells us that PHP is loading its configuration from:
C:\php\php.ini Why is this important?
Windows developers sometimes have multiple PHP installations on their computers.
For example, you might have installed PHP through:
- XAMPP
- Laragon
- Herd
- The official PHP website
Editing the wrong php.ini file will not solve the problem because your terminal may be using a different PHP installation.
Always identify the active configuration file first.
3. Open the PHP Configuration File
Now that we know which configuration file PHP is using, let's open it.
You can open it directly from PowerShell:
notepad C:\php\php.ini This launches Notepad with your PHP configuration file.
Alternatively, you can open the file using VS Code.
4. Enable the PDO MySQL Extension
Inside php.ini, press:
Ctrl + F Search for:
;extension=pdo_mysql You might find that the line begins with a semicolon.
In PHP configuration files, a semicolon comments out a configuration line.
This means PHP ignores that setting.
Change:
;extension=pdo_mysql To:
extension=pdo_mysql We have now enabled the MySQL PDO driver.
You can also enable the MySQLi extension:
extension=mysqli Although MySQLi is not required for Laravel's PDO connection, it can be useful for other PHP applications.
Save the configuration file using:
Ctrl + S 5. Verify That PHP Has Loaded the Extensions
After saving your changes, close your existing terminal and open a new PowerShell terminal.
Execute:
php -m The php -m command displays all PHP extensions currently loaded.
In my case, the relevant part of the output looked like this:
[PHP Modules] bcmath calendar Core ctype curl date dom filter hash iconv json libxml mbstring mysqli mysqlnd openssl pcre PDO pdo_mysql Phar random readline Reflection session SimpleXML SPL standard tokenizer xml xmlreader xmlwriter zlib Pay attention to these entries:
PDO pdo_mysql mysqli mysqlnd If pdo_mysql appears in your output, PHP has successfully loaded the MySQL PDO extension.
However, let's perform one more verification.
6. Confirm That PDO Recognizes MySQL
Run this command in PowerShell:
php -r "print_r(PDO::getAvailableDrivers());" This command asks PHP to display all database drivers available through PDO.
Expected output:
Array ( [0] => mysql ) The important part is:
mysql This confirms that PDO recognizes MySQL as an available database driver.
If the output is empty, PHP has not successfully loaded the required database driver.
Check your php.ini configuration and PHP extension directory.
7. Clear Laravel's Configuration Cache
Now that PHP can recognize MySQL, let's return to our Laravel project.
Navigate to your project directory:
cd C:\Users\Backend-Nerd\OneDrive\Desktop\Development\samupos Execute:
php artisan config:clear This clears Laravel's cached configuration.
You can also clear the application cache:
php artisan cache:clear Why clear the configuration?
Laravel can cache configuration values. Clearing the configuration ensures that Laravel reads the current application settings.
Note that clearing Laravel's cache does not install or enable PHP extensions. That must be handled at the PHP level.
8. Restart and Test Laravel
Finally, start your Laravel development server:
php artisan serve If everything is configured correctly, Laravel should start its development server.
You should see something similar to:
INFO Server running on [http://127.0.0.1:8000]. Try the operation that previously produced the database driver error.
If the error persists, check whether your terminal, Artisan, and web server are using the same PHP installation.
You can check the active PHP executable using:
where.exe php And check its version using:
php -v Multiple PHP installations can cause inconsistencies between your terminal and web server.
9. What If the Error Still Persists?
If you have enabled pdo_mysql but Laravel continues reporting could not find driver, investigate these possibilities:
1. Wrong PHP configuration file
You might have edited a different php.ini from the one your application uses.
Check again:
php --ini 2. Incorrect extension directory
Check whether your PHP configuration contains a valid extension directory:
extension_dir = "ext" The directory must point to the location containing PHP's extension DLL files.
3. Multiple PHP installations
Check which PHP executable Windows is loading:
where.exe php 4. Different PHP configuration in your web server
PHP CLI and PHP used by Apache or another web server may load different configurations.
Check the configuration used by the actual runtime producing the error.
10. Important Lessons for Beginners
This troubleshooting experience taught me several important things about PHP and Laravel development.
PDO and database drivers are different things. Having PDO installed does not automatically mean PHP can communicate with MySQL.
Always check your active
php.ini. Editing the wrong configuration file can waste a lot of development time.Understand your PHP extensions. Laravel depends on PHP extensions to perform many operations, including database connections.
Don't reinstall everything immediately. Sometimes a small configuration change is all that is required.
Verify before moving forward. Commands such as
php -mandPDO::getAvailableDrivers()help establish whether PHP is correctly configured.
Conclusion
The Laravel PDOException: could not find driver error can be frustrating, especially when you are setting up a development environment on Windows for the first time.
Fortunately, the solution is often straightforward: identify the active PHP configuration file, enable pdo_mysql, verify that PHP recognizes MySQL, and test your Laravel application again.
For me, troubleshooting this issue while developing SamuPOS was another opportunity to understand how Laravel, PHP, PDO, and database drivers work together.
Remember: Before reinstalling your development tools, understand the error and investigate the actual cause.
Happy coding! 🚀
Useful Commands Reference
| Task | Command |
|---|---|
| Check PHP version | php -v |
| Find active PHP configuration | php --ini |
| List loaded PHP extensions | php -m |
| Check PDO database drivers | php -r "print_r(PDO::getAvailableDrivers());" |
| Find PHP executable | where.exe php |
| Clear Laravel configuration | php artisan config:clear |
| Clear Laravel cache | php artisan cache:clear |
| Start Laravel server | php artisan serve |
