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

Laravel9安装Sanctum后执行migrate报Unsupported driver [mongodb]异常

Laravel 9 执行php artisan migrate报"Unsupported driver [mongodb]"的原因分析

你遇到的问题是明明配置了MySQL,但迁移时却尝试使用MongoDB驱动,常见原因如下:

  • 配置缓存未清除:如果之前项目配置过MongoDB,Laravel会缓存旧的数据库配置。即使修改了.env文件,系统仍会读取缓存的旧配置。执行php artisan config:clear清除配置缓存即可解决。

  • config/database.php配置异常:检查该文件中'default'项的设置,是否被错误设为'mongodb',或者connections里的默认连接指向了MongoDB配置。比如错误写法:'default' => env('DB_CONNECTION', 'mongodb'),正确应该是'mysql'作为默认值。

  • 第三方包配置冲突:安装Sanctum或其他扩展后,部分包可能会自定义数据库连接。检查config/sanctum.php或其他第三方包的配置文件,是否有强制指定使用MongoDB驱动的逻辑。

  • 环境变量覆盖问题:确认是否存在.env.local、.env.testing等环境文件,这些文件会覆盖.env的配置,导致DB_CONNECTION被设为mongodb;或者.env中DB_CONNECTION被注释,而config/database.php的默认值设为了mongodb。

报错代码片段:

protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
{
    if ($resolver = Connection::getResolver($driver)) {
        return $resolver($connection, $database, $prefix, $config);
    }

    return match ($driver) {
        'mysql' => new MySqlConnection($connection, $database, $prefix, $config),
        'pgsql' => new PostgresConnection($connection, $database, $prefix, $config),
        'sqlite' => new SQLiteConnection($connection, $database, $prefix, $config),
        'sqlsrv' => new SqlServerConnection($connection, $database, $prefix, $config),
        default => throw new InvalidArgumentException("Unsupported driver [{$driver}]."),
    };
}

内容的提问来源于stack exchange,提问作者Sahar Parsaifar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 01:35:14