Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to MongoDB in Azure

I have a MongoDB on Azure and I am trying to connect to it using the npm module mongodb:

var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://myuser:[email protected]:10355/?ssl=true", function (err, db) {
  db.close();
});

Password

My password has the following characteristics:

  • Contains letters, lowercase, uppercase
  • No white space
  • Contains numbers
  • Contains special characters like =, @, $ and so on

Error

I get the following when executing the code above:

Error: Password contains an illegal unescaped character
    at parseConnectionString (C:\Users\myuser\Documents\myproj\node_modules\mongodb\lib\url_parser.js:280:13)

However the documentation does not tell much about how to solve this issue. I guess it is an encoding problem. How to fix this?

like image 241
Andry Avatar asked Dec 30 '17 00:12

Andry


People also ask

Why is my MongoDB not connecting?

Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

Is MongoDB compatible with Azure?

The Azure Cosmos DB's API for MongoDB is compatible with MongoDB server version 3.6 by default for new accounts. The supported operators and any limitations or exceptions are listed below. Any client driver that understands these protocols should be able to connect to Azure Cosmos DB's API for MongoDB.

How does MongoDB connect to Azure Data Studio?

To connect to a MongoDB database, select Add Connection and enter the connection details for the database then Connect, the default is a local MongoDB server at mongodb://127.0.0.1:27017 . You can also enter a connection string, click the "connect with a connection string" link and paste the connection string.


2 Answers

Characters like @ are restricted as they mess up the structure of the URL. The reason for this is because MongoDB interprets it as the @ separator. Instead of this:

var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://myuser:myp@[email protected]:10355/?ssl=true", function (err, db) {
  db.close();
});

use this

mongoClient.connect("mongodb://myuser:myp%[email protected]:10355/?ssl=true", { 
  uri_decode_auth: true 
}, function (err, db) {
  db.close();
});

To encode the password, use encodeURIComponent(password)

You can also use this syntax.

mongoClient.connect("mongodb://myhost.documents.azure.com:10355/?ssl=true", 
 {user: 'username', pass: 'p@ssword'}, function (err, db) {
  db.close();
});

On later versions, use

auth: {
       user: 'username',
       password: 'p@ssword',
    }

as below

mongoClient.connect("mongodb://myhost.documents.azure.com:10355/?ssl=true", {
  auth: {
   user: 'username',
   password: 'p@ssword',
  }}, function (err, db) {
  db.close();
});
like image 59
Solver Avatar answered Oct 02 '22 07:10

Solver


MongoDB can now use a password with special characters. To do this, add an option to the connection { useNewUrlParser: true }:

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

const uri = 'mongodb://mydbname:pa$s;w@[email protected]:27017/admin';

MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
    assert.strictEqual(null, err);
    // ...
    db.close();
});
like image 21
Eugene Gubar Avatar answered Oct 02 '22 06:10

Eugene Gubar