You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP 7.2 PDO驱动缺失:Laravel连接MySQL失败求助

Fixing "could not find driver" PDO Exception for Laravel & MySQL (PHP 7.2)

Hey, I’ve been in your exact spot before — nothing’s more frustrating than installing extensions, editing php.ini, and still hitting that driver error. Let’s break this down step by step to get your Laravel project connected to MySQL:

1. Double-Check You’re Editing the Right php.ini File

This is the #1 mistake I see. PHP often uses separate php.ini files for the web server (Apache/Nginx) and the command line interface (CLI).

  • To find your CLI php.ini path (which Laravel uses for artisan commands), run this in your terminal:
    php -i | grep php.ini
    
  • Compare this path to the one shown in phpinfo() (your web-facing PHP config). If they’re different, you need to edit both files (or at least the one corresponding to how you’re running Laravel).

2. Correct Your Extension Lines in php.ini

PHP 7.2 requires the correct extension filenames — don’t skip the .so suffix on most Linux systems. Update your php.ini to include these lines (remove any old, incorrect entries):

extension=pdo.so
extension=pdo_mysql.so
extension=mysqli.so

Note: On Windows, the suffix is .dll instead of .so, but it sounds like you’re on a Debian/Ubuntu distro given your package names.

3. Reinstall the MySQL Extension to Fix Corrupted Files

Sometimes the extension installation gets messed up. Reinstall it to ensure all files are in place and properly linked:

sudo apt-get install --reinstall php7.2-mysql

This will refresh the PDO MySQL driver and its dependencies.

4. Restart All Relevant Services

Editing php.ini won’t take effect until you restart your web server and PHP-FPM (if you’re using it):

  • For Apache:
    sudo systemctl restart apache2
    
  • For Nginx + PHP-FPM:
    sudo systemctl restart nginx
    sudo systemctl restart php7.2-fpm
    

Even if you’re only using the CLI, close and reopen your terminal to ensure the new PHP config loads.

5. Verify the Driver Is Loaded

Let’s confirm the extension is actually active:

  • For CLI: Run this command and look for pdo_mysql in the output:
    php -m | grep pdo
    
  • For web: Refresh your phpinfo() page and check the "PDO drivers" line — it should now show mysql.

6. Double-Check Laravel’s .env Configuration

Once the driver is loaded, make sure your .env file has the correct MySQL settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_mysql_user
DB_PASSWORD=your_mysql_password

Run php artisan config:clear to clear Laravel’s config cache after making changes.

If you’ve gone through all these steps and still see the error, let me know — we can dig deeper into system-specific quirks, but this should resolve 99% of cases with PHP 7.2 and Laravel.

内容的提问来源于stack exchange,提问作者Billy Ray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:00:32