Search code examples
mongodbmgomongodb-atlasmongo-shellmongodb-compass

Couldnt connect to mongo atlas from the mongo shell, mongo compass and go mgo Driver


I'm trying to connect mongo shell with my mongo atlas M0 cluster and ending up getting this error

Error Message

Solution I tried which didnt work:

I upgraded the monog version to 4.2 to be insync with mongo version in atlas, didnt solve the problem getting this error mongo

"mongodb://cluster0-shard-00-02.79t7i.azure.mongodb.net:27017,cluster0-shard-00-00.79t7i.azure.mongodb.net:27017,cluster0-shard-00-01.79t7i.azure.mongodb.net:27017/testdatabase?tls=true" MongoDB shell version v4.2.8 connecting to: mongodb://cluster0-shard-00-02.79t7i.azure.mongodb.net:27017,cluster0-shard-00-00.79t7i.azure.mongodb.net:27017,cluster0-shard-00-01.79t7i.azure.mongodb.net:27017/testdatabase?compressors=disabled&gssapiServiceName=mongodb&tls=true

*** It looks like this is a MongoDB Atlas cluster. Please ensure that your IP whitelist allows connections from your network.

2020-08-16T00:14:57.693+0545 E QUERY [js] Error: couldn't connect to server cluster0-shard-00-01.79t7i.azure.mongodb.net:27017, connection attempt failed: HostUnreachable: Connection reset by peer : connect@src/mongo/shell/mongo.js:341:17 @(connect):2:6 2020-08-16T00:14:57.695+0545 F - [main] exception: connect failed 2020-08-16T00:14:57.695+0545 E - [main] exiting with code 1

On trying with mgo go driver i'm getting the error as:

time="2020-08-17T15:32:32+05:45" level=debug msg="mongo host and port : mongodb://cluster0-shard-00-00.akidp.azure.mongodb.net:27017,cluster0-shard-00-01.akidp.azure.mongodb.net:27017,cluster0-shard-00-02.akidp.azure.mongodb.net:27017" time="2020-08-17T15:33:32+05:45" level=error msg="Unable to connect to mongo no reachable servers" panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x8f3a02]

goroutine 1 [running]: gopkg.in/mgo%2ev2.(*Session).Copy(0x0, 0x7fbebca1b008) /home/sgupta/go/pkg/mod/gopkg.in/mgo.v2@v2.0.0-20180705113604-9856a29383ce/session.go:1593 +0x22 github.optum.com/claredi/oti.git/ds.Copy(0xc001ae3a30, 0x0, 0x0) /home/sgupta/go/src/github.optum.com/claredi/oti.git/ds/ds.go:166 +0x3c github.optum.com/claredi/oti.git/ds/announcements.indexes() /home/sgupta/go/src/github.optum.com/claredi/oti.git/ds/announcements/announcements.go:47 +0xff github.optum.com/claredi/oti.git/ds.Init(0xc0000f8000, 0xc0001efe68) /home/sgupta/go/src/github.optum.com/claredi/oti.git/ds/ds.go:98 +0x52e main.startAPIServer(0x0, 0xc000098360) /home/sgupta/go/src/github.optum.com/claredi/oti.git/api/main.go:83 +0xe98 main.main() /home/sgupta/go/src/github.optum.com/claredi/oti.git/api/main.go:26 +0x4d exit status 2

Hoping to get a resolution from the experts. Thanks in Advance :)


Solution

  • First, in Atlas:

    1. Ensure the cluster itself looks healthy in "Clusters"
    2. Go to "Network Access" and ensure your IP is allowed
    3. Just to be safe, go to "Database Access" and create a new user with proper permissions

    If your problem is similar to Cannot connect to mongodb atlas through mongo shell, it might be that your IP changed or something, and you need to allow your current IP.