Beginner issue with database connect

Hi,
New to Mongo environment, I am trying to build an express app with mongo using mongoose:

After requiring mongoose, I use the following:

""mongoose.connect(

uri,

{ useNewUrlParser: true, useUnifiedTopology: true },

err => {

console.log("mongo db connection", err);

}

);“”

However, after running my app.js I get the following error:
{Error: querySrv ETIMEOUT _mongodb._tcp.cluster0-rsadq.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:197:19)
errno: ‘ETIMEOUT’,
code: ‘ETIMEOUT’,
syscall: ‘querySrv’,
hostname: ‘_mongodb._tcp.cluster0-rsadq.mongodb.net’ }

Note that I already whitelisted my IP and that I use the standard uri format where I replaced test with the db nama as well as password…

Thank you!

Are you able to connect by shell?
Make sure no firewall/antivirus/vpn blocking your request

Hello @sam23 wellcome to the community!

As Ramachandra mentioned: can you please check if you can connect via the mongo shell to make sure that you can reach the database? In this MongoDB Doc you find further information how to connect. In case you get stuck feel free to post further information.

Since you start with MongoDB and want to use Mongoose I like to point you also to this great article Do You Need Mongoose When Developing Node.js and MongoDB Applications from @ado

Cheers,
Michael

Thank you for your recommandations. I will try to switch to mongo over mongoose. Concerning shell connection, as you excepected I have a problem:

C:\Users\hp>mongo
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-06-23T16:48:50.665+0000 E QUERY [js] Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Aucune connexion n�a pu �tre �tablie car l�ordinateur cible l�a express�ment refus�e. :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-06-23T16:48:50.676+0000 F - [main] exception: connect failed
2020-06-23T16:48:50.677+0000 E - [main] exiting with code 1

What do you suggest for resolving this problem?

1 Like

When you type mongo, you are trying to connect locally.

Since your cluster is cluster0-rsadq.mongodb.net, you should try

mongo 'mongodb+srv://cluster0-rsadq.mongodb.net'

Check the usage on the first link provided by @michael_hoeller, to see how to supply your credentials.

2 Likes

Finally, I get this error:

DNSHostNotFound: Failed to look up service “”:Cette op├®ration sÔÇÖest termin├®e car le d├®lai dÔÇÖattente a expir├®.
try ‘C:\mongodb\bin\mongo.exe --help’ for more information

I followed the docs but this time I get this :

C:\Users\hp>mongo ‘mongodb+srv://cluster0-rsadq.mongodb.net’ -u my_username -p password
MongoDB shell version v4.2.6
2020-06-23T20:52:01.514+0000 F - [main] exception: No digits
2020-06-23T20:52:01.516+0000 E - [main] exiting with code 1

Note that my_username and password were replaced and I get the same log

Use normal double or single quotes. Yours are new web version.

1 Like

That didn’t change anything, I keep getting an error related to: DNSHostNotFound: Failed to look up service

But your earlier post gives different error with SRV string(no digits)

Did your try with doble quotes as Steeve suggested?

or create a test user and share pwd for us to check

I tried your string with some id/pwd
It gave authentication error

or try long form of connect string which has names of all 3 nodes of your cluster with replicaset name
It is possible your are using a DNS resolver that does not support DNS seedlist
Also make sure no vpn/firewall,anti virus issues blocking your connection

1 Like

Hello @sam23

the connection string seems to be ok, quote or double quote should not matter. I can reach your DB, but not authenticate since I used my_username / password

mi01@LU30:~$ mongo 'mongodb+srv://cluster0-rsadq.mongodb.net' -u my_username -p password
MongoDB shell version v3.6.8
connecting to: mongodb+srv://cluster0-rsadq.mongodb.net
2020-06-24T07:05:20.706+0200 I NETWORK  [thread1] Starting new replica set monitor for Cluster0-shard-0/cluster0-shard-00-02-rsadq.mongodb.net.:27017,cluster0-shard-00-01-rsadq.mongodb.net.:27017,cluster0-shard-00-00-rsadq.mongodb.net.:27017
2020-06-24T07:05:20.793+0200 I NETWORK  [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to cluster0-shard-00-00-rsadq.mongodb.net.:27017 (1 connections now open to cluster0-shard-00-00-rsadq.mongodb.net.:27017 with a 5 second timeout)
2020-06-24T07:05:20.793+0200 I NETWORK  [thread1] Successfully connected to cluster0-shard-00-02-rsadq.mongodb.net.:27017 (1 connections now open to cluster0-shard-00-02-rsadq.mongodb.net.:27017 with a 5 second timeout)
2020-06-24T07:05:20.813+0200 I NETWORK  [thread1] changing hosts to Cluster0-shard-0/cluster0-shard-00-00-rsadq.mongodb.net:27017,cluster0-shard-00-01-rsadq.mongodb.net:27017,cluster0-shard-00-02-rsadq.mongodb.net:27017 from Cluster0-shard-0/cluster0-shard-00-00-rsadq.mongodb.net.:27017,cluster0-shard-00-01-rsadq.mongodb.net.:27017,cluster0-shard-00-02-rsadq.mongodb.net.:27017
2020-06-24T07:05:20.880+0200 I NETWORK  [thread1] Successfully connected to cluster0-shard-00-00-rsadq.mongodb.net:27017 (1 connections now open to cluster0-shard-00-00-rsadq.mongodb.net:27017 with a 5 second timeout)
2020-06-24T07:05:20.887+0200 I NETWORK  [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to cluster0-shard-00-02-rsadq.mongodb.net:27017 (1 connections now open to cluster0-shard-00-02-rsadq.mongodb.net:27017 with a 5 second timeout)
2020-06-24T07:05:20.965+0200 I NETWORK  [thread1] Successfully connected to cluster0-shard-00-01-rsadq.mongodb.net:27017 (1 connections now open to cluster0-shard-00-01-rsadq.mongodb.net:27017 with a 5 second timeout)
Implicit session: session { "id" : UUID("55b56a74-945f-47d9-923d-dabe2ab99112") }
MongoDB server version: 4.2.8
WARNING: shell and server versions do not match
2020-06-24T07:05:21.141+0200 I NETWORK  [thread1] Marking host cluster0-shard-00-02-rsadq.mongodb.net:27017 as failed :: caused by :: Location8000: can't authenticate against replica set node cluster0-shard-00-02-rsadq.mongodb.net:27017: Authentication failed.
2020-06-24T07:05:21.179+0200 I NETWORK  [thread1] Marking host cluster0-shard-00-00-rsadq.mongodb.net:27017 as failed :: caused by :: Location11002: can't authenticate against replica set node cluster0-shard-00-00-rsadq.mongodb.net:27017: socket exception [CONNECT_ERROR] for cluster0-shard-00-00-rsadq.mongodb.net:27017
2020-06-24T07:05:21.216+0200 I NETWORK  [thread1] Marking host cluster0-shard-00-01-rsadq.mongodb.net:27017 as failed :: caused by :: Location11002: can't authenticate against replica set node cluster0-shard-00-01-rsadq.mongodb.net:27017: socket exception [CONNECT_ERROR] for cluster0-shard-00-01-rsadq.mongodb.net:27017
2020-06-24T07:05:21.259+0200 I NETWORK  [thread1] Marking host cluster0-shard-00-02-rsadq.mongodb.net:27017 as failed :: caused by :: Location8000: can't authenticate against replica set node cluster0-shard-00-02-rsadq.mongodb.net:27017: Authentication failed.
2020-06-24T07:05:21.259+0200 E QUERY    [thread1] Error: can't authenticate against replica set node cluster0-shard-00-02-rsadq.mongodb.net:27017: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1608:20
@(auth):6:1
@(auth):1:2
exception: login failed
mi01@LU30:~$

As Ramachandra noted, this seems to be more of an infrastructure problem.

1 Like

You could try using google’s DNS servers at 8.8.8.8 and 8.8.4.4.

1 Like

I think I am close to establishing the connection: I verified whitelisted IPs, removed antivirus but this time:

connecting to: mongodb://cluster0-rsadq.mongodb.net:27017/?compressors=disabled&gssapiServiceName=mongodb

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

2020-06-24T15:45:21.281+0000 E QUERY [js] Error: couldn’t connect to server cluster0-rsadq.mongodb.net:27017, connection attempt failed: HostNotFound: Could not find address for cluster0-rsadq.mongodb.net:27017: SocketException: H�te inconnu. :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-06-24T15:45:21.296+0000 F - [main] exception: connect failed
2020-06-24T15:45:21.296+0000 E - [main] exiting with code 1

I get a Host Not Found error!

By the way thank you for your indications, I learned a lot of concepts by looking up these issues.

The URI you are using is wrong. You are using part old style and part SRV. The DNS name cluster9-rsadq.mongodb.net does not correspond to a host. It correspond to a cluster. It has the following DNS entry:

;; ANSWER SECTION:
cluster0-rsadq.mongodb.net. 59  IN      TXT     "authSource=admin&replicaSet=Cluster0-shard-0"

So your hosts are:

;; ANSWER SECTION:
_mongodb._tcp.cluster0-rsadq.mongodb.net. 59 IN SRV 0 0 27017 cluster0-shard-00-00-rsadq.mongodb.net.
_mongodb._tcp.cluster0-rsadq.mongodb.net. 59 IN SRV 0 0 27017 cluster0-shard-00-01-rsadq.mongodb.net.
_mongodb._tcp.cluster0-rsadq.mongodb.net. 59 IN SRV 0 0 27017 cluster0-shard-00-02-rsadq.mongodb.net.

To use SRV, remove the port number and use mongodb+srv:// rather than mongodb:// like it was given in the examples.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.