Could not Connect to Mongodb Altas from Linux 8

24 Views Asked by At

I am trying to connect my react/nodejs app to mongodb Atlas in Alma Linux Server 8 by using MONGO_URL = mongodb+srv://username:[email protected]/?retryWrites=true&w=majority

import mongoose from "mongoose";

const connectDatabase = async () => {
    try {
        const connection = await mongoose.connect(process.env.MONGO_URL);
        console.log("mongodb connected")

    } catch (error) {
        console.log("Error:", error)
        process.exit(1)
    }
};

export default connectDatabase;

but it returns an error

[node@server1 clothes-server]$ npm start server.js

> [email protected] start
> node ./server.js server.js

server run in port 5001
Error: MongooseServerSelectionError: 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://www.mongodb.com/docs/atlas/security-whitelist/
    at _handleConnectionErrors (/home/node/clothes-server/node_modules/mongoose/lib/connection.js:809:11)
    at NativeConnection.openUri (/home/node/clothes-server/node_modules/mongoose/lib/connection.js:784:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async connectDatabase (file:///home/node/clothes-server/config/MongoDb.js:5:28) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-apfjsl7-shard-00-01.0pcqj9z.mongodb.net:27017' => [ServerDescription],
      'ac-apfjsl7-shard-00-00.0pcqj9z.mongodb.net:27017' => [ServerDescription],
      'ac-apfjsl7-shard-00-02.0pcqj9z.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-x798m6-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },

code: Error ScreenShoterror message

I have tried to add my ip to the whitelist, also added 0.0.0.0/0 to connect from every where but no use. Please Explain how to resolve this and make the connection successfull to mongodbAtlas

0

There are 0 best solutions below