我在我的生产和开发环境中使用不同的 mongo 数据库。
我有不同的连接配置,因为 mongodb(生产连接名称)在 mongo atlas 集群上,所以它使用 dsn 连接,但是 mongodb1(开发连接名称)安装在本地,所以我使用主机和端口连接它。
我想为不同的环境使用不同的数据库连接,所以我在两种情况下都在 env 文件中设置了不同的 DB_CONNECTION。 我的制作是
DB_CONNECTION=mongodb
为了发展它
DB_CONNECTION=mongodb1
在我的 config/database.php 文件中
'default' => env('DB_CONNECTION', 'mysql'),
在我的连接数组中
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('MONGO_DB_DSN'),
'database' => env('MONGO_DB_DATABASE', 'uspl'),
'username' => env('MONGO_DB_USERNAME', 'uspl'),
'password' => env('MONGO_DB_PASSWORD', 'unfoldingskies'),
'options' => array(
'db' => 'uspl'
)
],
'mongodb1' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'options' => []
],
我的开发环境出现错误 ->
数据库 [mongodb] 未配置。在文件中
/var/www/html/devBackend/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php 第 152 行
我试过 php artisan config:clear, cache:clear, composer dump-autoload 但没有效果
最佳答案
Laravel 不支持开箱即用的 MongoDB,因此您需要使用第 3 方包才能使用它。
As of this writing Laravel has the following database drivers:
- MySQL 5.6+(版本政策)
- PostgreSQL 9.4+(版本政策)
- SQLite 3.8.8+
- SQL Server 2017+(版本政策)
我在网上快速搜索了一下 this 3rd party package.
Laravel MongoDB
An Eloquent model and Query builder with support for MongoDB, using the original Laravel API. This library extends the original Laravel classes, so it uses exactly the same methods.
关于linux - Laravel 没有从 env 文件中获取默认的 DB_CONNECTION 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58502346/