首页 > 解决方案 > Strapi 连接到 atlas Mongo 数据库时出错

问题描述

我想将 mongo atlas 与strapi 一起使用。所以我正在读取值.env

这些是设置

connections: {
 default: {
  connector: 'mongoose',
  settings: {
    host: env('DB_MONGO_URL', '127.0.0.1'),
    srv: env.bool('DATABASE_SRV', false),
    port: env.int('DB_PORT', 27017),
    database: env('DATABASE_NAME', 'mydb'),
    username: env('DB_USER_NAME', null),
    password: env('DB_PASSWORD', null)
  },
  options: {
    authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
    ssl: env.bool('DB_SSL_ENABLE', false),
  },
 },

这是价值观

DB_MONGO_URL=myproject.random.mongodb.net/mydbt?ssl_cert_reqs=CERT_NONE
DB_USER_NAME=myuser
DB_PASSWORD=mypasswor

但这显示

[2021-10-12T13:18:25.485Z] debug ⛔️ Server wasn't able to start properly.
[2021-10-12T13:18:25.490Z] error Error connecting to the Mongo database. Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
error Command failed with exit code 1.

在我允许的网络中0.0.0.0,包括任何 IP 地址。

如果我在MongoDB Compass中传递这个 url

mongodb+srv://myproject.random.mongodb.net/mydbt?ssl_cert_reqs=CERT_NONE

那么这有效

我也试过了

DB_MONGO_URL=mongodb+srv://myproject.random.mongodb.net/mydbt?ssl_cert_reqs=CERT_NONE
DB_USER_NAME=myuser
DB_PASSWORD=mypassword

但这表明

[2021-10-13T06:59:01.372Z] debug ⛔️ Server wasn't able to start properly.
[2021-10-13T06:59:01.374Z] error Error connecting to the Mongo database. getaddrinfo ENOTFOUND mongodb+srv
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

如果我尝试启用srv true

[2021-10-13T07:01:20.065Z] debug ⛔️ Server wasn't able to start properly.
[2021-10-13T07:01:20.067Z] error Error connecting to the Mongo database. URI does not have hostname, domain name and tld
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我该如何解决这个问题?

标签: databasemongodbmongoosestrapi

解决方案


推荐阅读