Nodal
Nodal

Reputation: 353

Getting my Mongo connection string correct for remote database hosting with auth

I'm having trouble attempting to replicate this MongoDB connection in NodeJS, using Mongojs:

mongo --host dds-xxxx.mongodb.rds.aliyuncs.com:3717 -u root -p password --authenticationDatabase admin

My current, rather obfuscated, code:

/* MongoDB setup */
// Set parameters:
var aliUser = 'root',
    aliPass = 'password',
    aliHost = 'dds-xxxx.mongodb.rds.aliyuncs.com:3717',
    aliAuth = 'admin',
    aliMyDb = 'mydb';
// Initialise MongoDB object:
var connectionString = aliUser+':'+aliPass+'@'+aliHost+'/'+aliMyDb+'?authSource='+aliAuth,
    db = mongojs(connectionString, ['keywords']);

As far as I can tell, this is not connecting correctly. The code never seems to give any errors or alert the user in any way as to whether or not the connection was successful. However, the GET methods I have which try to read from this database produce no result, so it doesn't appear to work.

I have tried several variations on this and can't quite seem to get it right. Any assistance or insight would be greatly appreciated. Thanks again.

Upvotes: 0

Views: 302

Answers (2)

Nodal
Nodal

Reputation: 353

As it turns out, there was nothing wrong with my code at all! The problem was that my server provider, Aliyun, can only be accessed from its own whitelisted servers. So I had to run my Node app from one of their servers to make it work!

This is fine since we are hosting on Aliyun anyway, but I was testing on local, therefore it appeared not to work! Another mystery solved.

Upvotes: 0

Manish Prasad
Manish Prasad

Reputation: 105

Add 'mongodb://' to your connection string. The examples given for mongojs needs update.

Upvotes: 1

Related Questions