在Ddev搭建Drupal项目后访问出现PDOException及表不存在错误
Hey there, let's work through these frustrating errors you're seeing with your Drupal project on Ddev. The PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.semaphore' doesn't exist and the Uncaught exception thrown in shutdown function issues almost always stem from a missing or misconfigured Drupal database. Here's how to fix it step by step:
1. Verify Your Database State
First, let's confirm if your Drupal database actually has the required tables:
- SSH into your Ddev container:
ddev ssh - Connect to the database:
mysql -u db -pdb db - Run this command to list tables:
SHOW TABLES;
If you don't see core Drupal tables like semaphore, users, or node, your database is empty—this is the root cause.
2. Install Drupal (For New Projects)
If this is a fresh setup, you haven't completed the Drupal installation yet. You have two options:
- Web Installer: Visit your project URL (usually
https://<your-project-name>.ddev.site) and follow the installation wizard. Choose the "Standard" profile, and Drupal will automatically create all necessary database tables. - Drush (Command-Line): For a faster setup, run:
ddev drush site:install standard --account-name=admin --account-pass=admin --site-name="My Drupal Site" -y
3. Fix Database Connection Configuration
Sometimes the database connection settings are misconfigured. Check your Drupal config files:
- Open
sites/default/settings.phporsettings.ddev.php(Ddev's auto-generated config file) and ensure these parameters are correct:$databases['default']['default'] = [ 'database' => 'db', 'username' => 'db', 'password' => 'db', 'host' => 'db', 'port' => '3306', 'driver' => 'mysql', 'prefix' => '', ]; - If you've modified this file accidentally, regenerate the Ddev config with:
ddev config
4. Resolve the Shutdown Function Exception
This error is almost always a side effect of the missing database tables. Once you fix the database issue, this exception should disappear on its own. If it lingers:
- Clear Drupal's cache via Drush (even if the site is inaccessible):
ddev drush cr
5. Import a Database Backup (For Migrated Projects)
If you're moving an existing Drupal project to Ddev, you need to import your database backup:
- Run this command (replace the file path with your actual backup location):
ddev import-db --file=/path/to/your/drupal-db-backup.sql - After importing, clear the cache:
ddev drush cr
Quick Troubleshooting Checks
- Ensure Ddev services are running:
ddev start(restart withddev restartif needed) - Check logs for hidden issues:
- Database logs:
ddev logs -s db - Web server logs:
ddev logs -s web
- Database logs:
内容的提问来源于stack exchange,提问作者rfay

