Strapi Error connecting to the atlas Mongo database

System Information
  • Strapi Version: 3.6.8
  • Operating System: Windows 10
  • Database: MongoDB atlas
  • Node Version: 14
  • NPM Version: 6.14.9
  • Yarn Version: 1.22.11

I want to use mongo atlas with strapi. SO I am reading the values from .env

these are the settings

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),
  },
 },

and here are the values

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

but this is showing

[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.

In network i have allowed, 0.0.0.0 that includes any IP address.

If i pass this url in MongoDB Compass

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

then this works

I tried the same as well

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

but this shows

[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.

If i tried enable 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.

how can i solve this?

i have same issue can anyone know how it fix ?

We no longer support MongoDB as of v4

@DMehaffy i have used 3.6 version

I think this is the issue. You should pass only the cluster name. So it should be something like myproject.random.mongodb.net, without all the other params. And make sure you do have a DB called mydb in your cluster, since I don’t see an env variable DATABASE_NAME.

That being said, I think it would be better to start a project with SQL DB, since Strapi no longer supports MongoDB, and version 3.6 has only a few months of support left. Personally I now have to rewrite my entire project from scratch to work with PostgreSQL and run on v4.

Can you please provide database.js format on mongo for the above DB_MONGO_URL ?

Sure:

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'mongoose',
      settings: {
        host: env('DATABASE_HOST'),
        srv: env.bool('DATABASE_SRV', true),
        port: env.int('DATABASE_PORT', 27017),
        database: env('DATABASE_NAME'),
        username: env('DATABASE_USERNAME'),
        password: env('DATABASE_PASSWORD'),
      },
      options: {
        authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
        ssl: env.bool('DATABASE_SSL', true),
      },
    },
  },
})

On Mongo Atlas site make sure you allow connection from your IP (I don’t remember exactly where it’s done).

I have set up your code on my side and it’s coming to " 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://do MongoDB: the application data platform | MongoDB
"

That’s exactly what I said: make sure you allow connection from your IP. On the MongoDB Atlas site when you’re logged in, look at the left side panel, click on Network Access, then on the green “Add IP Address” button. In the opened window add your IP address and click Confirm. Wait until the status becomes active.

I convert Mongo DB. Now I set up mongo on the server locally and it’s working now.