Auth failed, code 18 when connecting to MongoLab database

19,113

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
19,113
Jori
Author by

Jori

Currently a second year student Media Technology on the UAS of Rotterdam. Just have two years of programming experience, but over these two years, I've been liking Javascript the most. I like to try loads of different new technologies, currently trying to get the hang of Meteor.

Updated on June 06, 2022

Comments

  • Jori
    Jori about 2 years

    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?

  • rtvalluri
    rtvalluri over 7 years
    upgrading mongoose version helped me..thanks :) refer this github.com/mongolab/hello-mongoose/commit/…