Mongodb server selection error due network timeout - mongodb

I have a Go program which uses Mongo DB. I run it from my home pc and I keep getting this error very often:
panic: server selection error: server selection timeout, current
topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr:
cluster0-shard-00-00.tvm1o.mongodb.net:27017, Type: Unknown, Last
error: connection() error occurred during connection handshake: dial
tcp
3.216.112.85:27017: i/o timeout }, { Addr: cluster0-shard-00-01.tvm1o.mongodb.net:27017, Type: Unknown, Last
error: connection() error occurred during connection handshake: dial
tcp 34.197.85.254:27017: i/o timeout }, { Addr:
cluster0-shard-00-02.tvm1o.mongodb.net:27017, Type: Unknown, Last
error: connection() error occurred during connection handshake: dial
tcp 18.206.5.2:27017: i/o timeout }, ] }
And this is the exact code where it breaks:
if err := clientMongo.Ping(context.TODO(), readpref.Primary()); err != nil {
panic(err)
}
I understand this is a connection timeout, but I don't understand how can this happen at all during a simple client connection. I made a speedtest and my current upload speed is 22 Mbps, I am not uploading big json arrays or anything. It happens always when I try to connect to the client. So I would like to know if this can be caused because my internet connection or something on Mongo's end?

You might need to add your IP to the whitelist of MongoDB.

A few things --
we would need to see the complete code for creating a connection. I'm going to assume you're using exactly what is in the documentation here?
You should try to connect with mongosh and Compass also. If you have problems with another tool, then the odds are it is your Atlas cluster OR your security settings on the cluster, rather than your application code.
that being said about 95% of the time the issue is whitelist or database users. do you have a user created in database access area of the UI that has admin/read/write any database? is your IP in the whitelist?
if 3 is good, and 2 doesn't work, there is free Atlas support available in the green chat button of the UI in MongoDB.

Related

Motor/PyMongo connection timeout with AWS DocumentDB

I have used Python's Motor library to connect to an AWS DocumentDB (MongoDB) instance. Motor uses PyMongo behind the scenes.
This was all working fine, but suddenly I have been getting a connection timeout anytime I attempt to do anything that requires a database operation:
pymongo.errors.ServerSelectionTimeoutError: aws-documentdb-endpoint:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 628376f4a53a913de7752368, topology_type: Single, servers: [<ServerDescription ('aws-documentdb-endpoint', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('aws-documentdb-endpoint:27017: timed out')>]>
This is how I am creating the connection, which has worked fine for weeks, but now gives this timeout error every time:
from motor.motor_asyncio import AsyncIOMotorClient
client = AsyncIOMotorClient(
host=os.environ["MONGO_URI"],
ssl=True,
ssl_cert_reqs=ssl.CERT_NONE,
ssl_ca_certs=os.path.join(os.environ["ROOT_DIR"], "cert", "aws", "rds-combined-ca-bundle.pem")
)
I have no idea what is going wrong since this has been working fine for so long, but suddenly is not even thought I don't think I've made any changes to this part of the code.

Unable to import json into mongo db atlas

I'm attempting to import json into an atlas db.
The attempt uses this code:
mongoimport --uri "mongodb://usr:pass#myurlname.uia8q.mongodb.net/paintura?retryWrites=true&w=majority" --collection products --file pathtojson
all parameters in the above are correct via the connect docs.
exact error is:
error connecting to host: could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: myurlname.uia8q.mongodb.net:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup paintura.uia8q.mongodb.net: no such host }, ] }
For the user I have atlas admin selected so I assume it should have full privelege.
I also have cleared my ip in the ip list.
Any ideas what I'm missing?

using mongdb in docker with Go

i tried to run MongoDB in docker-compose and connect my Go server with MongoDB in docker seem not to work for me, thought i already use mongodb:MyContanerName:27017
when i run my go server and then i tired to request on my client, the error something like this
2019/09/08 14:10:37 Unexpected error: rpc error: code = Internal desc = internal error: server selection error: server selection timeout
current topology: Type: Unknown
Servers:
Addr: mongo:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection(mongo:27017[-125]) connection is closed
i already did docker-compose up -d for my docker, and it was running on my docker ,
the full code is trigger here golang and mongodb

connect: connection timed out

I have successfully connected to a Postgres database using the go sql package:
...
db, err := sql.Open("postgres", connStr)
I then use the returned database to execute a (long running) query:
rows, err := db.Query(...)
And am getting the error:
dial tcp xx.xxx.xxx.xx:5432: connect: connection timed out
I have a couple of questions regarding this:
why is the connection timing out?
is there anything I can do to prevent it timing out?
sql.Open() may just validate its arguments without creating a connection to
the database. To verify that the data source name is valid, call Ping.
The sql.Open() function has only created an object, your pool is currently empty. In simple words connection with the database hasn't been established yet.
You need to call db.Ping() to make sure your pool has a working connection.

Connection to mongoose causes ECONNREFUSE [duplicate]

I've got a problem to connect my socketIO application (made with nodeJS) with my mongoDB.
I try to connect on an remote server, but its throws me error
Here's my code (there's no user/password set in the mongoDB) :
var url = "mongodb://192.168.1.5:27017/DB"
MongoClient.connect(url, function(err, db) {
console.log("test")
if (!err) {
console.log("test");
}
else {
console.dir(err)
throw err
}
// db.close();
});
And here's when I launch the server and I tried to launch the app in a navigator :
Server listening at port 80:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; })
^
Error
at Error.MongoError (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17)
at Server.destroy (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:629:47)
at Server.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:344:17)
at Db.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:267:19)
at /root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:196:12
at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:226:9)
at g (events.js:180:16)
at emit (events.js:98:17)
at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:238:68)
at g (events.js:180:16)
This error is returned for several errors like :
server is not running
you need to authenticate user
this database does not exists
the mongodb port is not the default port
Check this. Normaly your problem is just one of these causes
For this to work you have to make the changes in /etc/mongod.conf
comment bind_ip=127.0.0.1 As if this line in not commented it Listen to local interface only.
One of the solutions is to change 127.0.0.1 to public ip or whatever the router provided you with in mongodb config file located in /etc
There could be few reasons for ECONNREFUSED error.
Checkpoints
Check if your port is not serving any other process.
Check if your mongod is running.
Check if you localHost is configured correctly with your id or not.
for more you can see this