Auth failed, code 18 when connecting to MongoLab database
Asked Answered
M

4

14

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?

Morphia answered 4/1, 2016 at 14:31 Comment(2)
Can you check if by accident you are using your mongolab.com username and password, instead of the database username and password?Workaday
Also, are you possibly using an old shell version to connect to mongodb newer version?Workaday
T
12

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

Templin answered 12/1, 2016 at 2:59 Comment(1)
upgrading mongoose version helped me..thanks :) refer this github.com/mongolab/hello-mongoose/commit/…Barroom
E
4

I faced the same issue while I try to import data from the locale to server.

Those 2 parameters can be important, it worked after I put them:

--authenticationMechanism 'MONGODB-CR' 
--authenticationDatabase "admin"

Be careful about the auth mechanism, can be a different one. Check this part of documentation: https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-authenticationmechanism

Emmaemmalee answered 17/9, 2018 at 20:15 Comment(0)
A
1

Had this error myself, turns out I did two things incorrectly (thanks Idos):

  1. Used the mongolab.com username instead of the database one.
  2. Tried to connect to a mongo 3.4 database using a 2.6 shell provided through Ubuntu's repositories. mongo --version to check.

Follow the instructions from this MongoDB page to add their keys and repositories to your APT sources in order to upgrade and keep your MongoDB installation updated going forward.

Authorize answered 15/12, 2016 at 3:47 Comment(0)
G
1

i had a similar error in that case. put authSourse=admin and ssl=true to your connection

e.g

mongodb://username:[email protected]:61474/apitest?authSourse=admin&ssl=true
Gunwale answered 25/7, 2022 at 7:34 Comment(1)
You have a typo, should be authSource not authSourseScutum

© 2022 - 2024 — McMap. All rights reserved.