Yii2迁移报错:Could not find driver 问题求助
Hey there, let's work through this migration issue you're hitting with Yii2 Advanced Template on your XAMPP setup. Looking at your error stack trace, the problem starts when Yii tries to connect to the database and load the migration table schema—so we'll focus on database connectivity and related extensions first.
Step 1: Verify Basic Database Setup
First, let's rule out the simplest issues:
- Check if MySQL is running: Open your XAMPP control panel and make sure the MySQL module is started (status should show "Running").
- Confirm the database exists: Yii migrations don't automatically create your database. Head to phpMyAdmin (via XAMPP control panel) and check if the
hopordropdatabase is already created. If not, create it manually first. - Validate credentials: Double-check that your
rootusername and empty password are correct. XAMPP defaults to an empty root password, but if you've ever modified it, update yourcommon/config/main-local.phpaccordingly.
Step 2: Enable the Missing PDO MySQL Extension
Looking at your php -m output, I notice you have mysqlnd but no pdo_mysql extension—and Yii2 relies on PDO for database connections. This is almost certainly a key issue. Here's how to fix it:
- Open your XAMPP PHP configuration file: In the XAMPP control panel, click "Config" next to Apache, then select
php.ini. - Find the line that says
;extension=pdo_mysql(it might be;extension=php_pdo_mysql.dllon Windows). - Remove the leading semicolon (
;) to uncomment the line. - Save the file, then restart both Apache and MySQL services from the XAMPP control panel.
- Run
php -magain in your terminal—you should now seepdo_mysqllisted under PHP Modules.
Step 3: Test Direct Database Connection
To confirm your database credentials and extension are working, create a quick test script:
- In your project's
frontend/webfolder, create a file namedtest-db.phpwith this code:<?php $dsn = 'mysql:host=127.0.0.1;dbname=hopordrop'; $username = 'root'; $password = ''; try { $pdo = new PDO($dsn, $username, $password); echo "✅ Database connection successful!"; } catch (PDOException $e) { echo "❌ Connection failed: " . $e->getMessage(); } - Access this file via your browser (e.g.,
http://localhost/hoppordropp/frontend/web/test-db.php). If it fails, the error message will give you specific details (like wrong password, missing database, etc.).
Step 4: Ensure You're Running Migrations Correctly
- Make sure you're executing the migration command from your project root directory (where the
yiiconsole script is located). The command should be:php yii migrate - For Yii2 Advanced Template, if you're working in a specific environment (like dev), you can specify it explicitly:
php yii migrate --env=dev
Once you've worked through these steps, try running the migration command again. Most likely, enabling pdo_mysql and confirming your database exists will resolve the issue.
内容的提问来源于stack exchange,提问作者Miloš Vukadinović

