Auth failed, code 18 when connecting to MongoLab database

Jori picture Jori · Jan 4, 2016 · Viewed 15.4k times · Source

I'm trying to connect to a MongoLab database but keep getting the following error on connection:

{ [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }

The code I'm using to connect is:

var mongoose = require("mongoose");

mongoose.connect("mongodb://username:[email protected]:61474/apitest");

mongoose.connection.on('error', function (err) {
    console.log(err);
});

When I connect using the shell, I have no problems whatsoever. What am I doing wrong?

Answer

tanghao picture tanghao · Jan 12, 2016

I have encountered similar problem when connecting the mongo db using mongoose. After exploring a while I found mongoLab is using SCRAM-SHA-1 authentication. Refer to the question below I tried to upgrade my mongoose to V4.1.11, and then it works for me

Authentication in mongoose using SCRAM-SHA-1