Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback API connecting to MongoDB Atlas, fails

Looks like there are about 5 of these types of questions, all unanswered, maybe someone who's done it will have some time to share a solution.

I've got a loopback API app running locally, and it connects fine locally to mongoDB - I've got "loopback-connector-mongodb": "^3.3.1" installed, and as mentioned - it all works fine

So here is the progression I went thru (As I'm unsure which driver is being used I tried both syntax, syntaxi? syntaxes?)

The local connection works

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "localhost",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "",
    "password": "",
}

My first attempt at connecting to ALAS. This SEEMED to work, but right after the feedback it errors.

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "mongodb://adminUser:[email protected]:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "",
    "password": ""
}

Resulted in: Web server listening at: http://localhost:3000 Browse your REST API at http://localhost:3000/explorer

c:\loopback-test\node_modules\mongodb\lib\replset.js:345 process.nextTick(function() { throw err; }) ^ MongoError: database names cannot contain the character '/'

I figured it didn't like '/test...', So I removed it

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "mongodb://adminUser:[email protected]:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "",
    "password": ""
}

Resulted in: Error: Cannot create data source "loopback-test": Cannot initialize connector "mongodb": double colon in host identifier

    "loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "cluster0-shard-00-00-xxx.mongodb.net:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "adminUser",
    "password": "pwd"
}

Still Resulted in: Error: Cannot create data source "loopback-test": Cannot initialize connector "mongodb": double colon in host identifier

So by now Im starting to think Im using the 3.6 driver, but I want to try one more And I almost thought I had it - as it took a few seconds after the "Browse your REST API..." message, for the error to pop...but it did.

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "cluster0-shard-00-00-xxx.mongodb.net,cluster0-shard-00-01-xxx.mongodb.net,cluster0-shard-00-02-xxx.mongodb.net",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "adminUser",
    "password": "pwd"
}

Still Resulted in:

Web server listening at: http://localhost:3000 Browse your REST API at http://localhost:3000/explorer Connection fails: MongoError: no mongos proxy available It will be retried for the next request.

c:\loopback-test\node_modules\mongodb\lib\mongo_client.js:421 throw err ^ MongoError: no mongos proxy available

So now IM onto the 3.6 driver syntax

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "mongodb+srv://adminUser:[email protected]",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "",
    "password": ""
}

Web server listening at: http://localhost:3000 Browse your REST API at http://localhost:3000/explorer Connection fails: MongoError: failed to connect to server [cluster0-xxx.mongodb.net:27017] on first connect [MongoError: getaddrinfo ENOTFOUND cluster0-xxx.mongodb.net cluster0-xxx.mongodb.net:27017] It will be retried for the next request.

And lastly - I tried

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "cluster0-xxx.mongodb.net",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "adminUser",
    "password": "pwd"
}
like image 407
j-p Avatar asked Jan 03 '18 21:01

j-p


People also ask

Why MongoDB Atlas is not connecting?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.

Is MongoDB Atlas no longer free?

It's easy to get started with MongoDB Atlas, and it's free.

How to troubleshoot connection issues to MongoDB Atlas?

Troubleshooting connection issues to MongoDB Atlas 1 Troubleshooting Steps 1. Find the connection string with the older, non-SRV connection string 2. Try connecting with the older connection string format 3. Ensure accuracy of connection string 4. ... 2 Frequently Asked Questions 3 Still Having Problems?

How do I install loopback on MongoDB?

The official MongoDB connector for the LoopBack framework. In your application root directory, enter this command to install the connector: This installs the module from npm and adds it as a dependency to the application’s package.json file.

Why does MongoDB fail to connect to my cluster?

If you try to connect when you are at this limit, MongoDB displays an error stating connection refused because too many open connections. For a detailed comparision of cluster tiers and their maximum concurrent connections, see Connection Limits and Cluster Tier.

Why is my SRV record not working with MongoDB?

Some DNS servers do not fully support SRV records, so using the non-SRV connection string may be a better option for you. You may be using a driver that’s not compatible with MongoDB 3.6.x, and SRV support was introduced in version 3.6.x.


3 Answers

This one worked for me:

 "db": {
    "port": 27017,
    "url": "mongodb+srv://user:[email protected]",
    "database": "collectionName",
    "name": "mongodb",
    "connector": "mongodb"
  },
like image 167
itdoesntwork Avatar answered Oct 16 '22 11:10

itdoesntwork


Well, hopefully I didn't waste anyones time, Since I do keep looking after I post a question... I scoured the docs for the connector - not sure why I didn't before.... But using the URL argument and passing the connection string as a URL and bypassing the other arguments, worked just fine.

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "url": "mongodb://adminUser:[email protected]:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin"
}

I hope that helps anyone else banging their head on the database.json arguments.

cheers - and as always - thanks for the help.

like image 29
j-p Avatar answered Oct 16 '22 10:10

j-p


To clarify, the problem was caused by MongoDB Connection String URI being inserted into the host parameter instead of the url parameter.

The host parameter should only accept MongoDB server address, which can either be a hostname, IP address or UNIX domain socket.

You can find more information about the parameters on Loopback/MongoDB Connection Properties.

like image 39
Gaurav Gupta Avatar answered Oct 16 '22 09:10

Gaurav Gupta