正在将 Laravel 连接到 MongoDB Atlas
Connecting Laravel to MongoDB Atlas
所以我有一个用 Laravel/jenssegers 制作的学校项目,它在本地运行良好,但现在我想将它连接到 Atlas 集群,因为我想用 Heroku 部署该项目。
问题是,我无法让它工作。我快疯了。我明白了:
No suitable servers found (serverSelectionTryOnce
set): [TLS handshake failed: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed calling ismaster on 'cluster0-shard-00-00.mhfxu.mongodb.net:27017']
在我的 config/database.php 中,我有这个:
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_URI', 'mongodb+srv://username:password@cluster0.mhfxu.mongodb.net/database?retryWrites=true&w=majority'),
'database' => env('DB_DATABASE'),
],
我的集群也接受所有 IP。我该怎么办?
尝试关注
在你的 .env 中
MONGO_DB_URI=mongodb+srv://mongodbatlasstring (whatever is yours)
在你的database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => [],
'dsn' => env('MONGO_DB_URI','mongodb://').env('MONGO_DB_HOST', 'localhost')
],
您也可以尝试按照以下方式指定副本和所有节点
'mongodb' => [
'driver' => 'mongodb',
'host' => [
'abc-00-00.mongodb.net',
'abc-00-01.mongodb.net',
'abc-00-02.mongodb.net'
],
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => [
'ssl' => 'true',
'replicaSet' => 'abc-0',
'authSource' => 'admin',
'retryWrites' => 'true',
'w' => 'majority'
]
],
我终于解决了:)
所以......我只需要更新依赖项。我按照此处的说明操作:
所以我有一个用 Laravel/jenssegers 制作的学校项目,它在本地运行良好,但现在我想将它连接到 Atlas 集群,因为我想用 Heroku 部署该项目。
问题是,我无法让它工作。我快疯了。我明白了:
No suitable servers found (
serverSelectionTryOnce
set): [TLS handshake failed: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed calling ismaster on 'cluster0-shard-00-00.mhfxu.mongodb.net:27017']
在我的 config/database.php 中,我有这个:
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_URI', 'mongodb+srv://username:password@cluster0.mhfxu.mongodb.net/database?retryWrites=true&w=majority'),
'database' => env('DB_DATABASE'),
],
我的集群也接受所有 IP。我该怎么办?
尝试关注
在你的 .env 中
MONGO_DB_URI=mongodb+srv://mongodbatlasstring (whatever is yours)
在你的database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => [],
'dsn' => env('MONGO_DB_URI','mongodb://').env('MONGO_DB_HOST', 'localhost')
],
您也可以尝试按照以下方式指定副本和所有节点
'mongodb' => [
'driver' => 'mongodb',
'host' => [
'abc-00-00.mongodb.net',
'abc-00-01.mongodb.net',
'abc-00-02.mongodb.net'
],
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => [
'ssl' => 'true',
'replicaSet' => 'abc-0',
'authSource' => 'admin',
'retryWrites' => 'true',
'w' => 'majority'
]
],
我终于解决了:)
所以......我只需要更新依赖项。我按照此处的说明操作: