server returned error on SASL authentication step: Authentication failed

93,458

Solution 1

I faced similar error and added --authenticationDatabase parameter and it worked while we connecting to a remote MongoDB

Use the similar below format in your code :

$mongorestore --host databasehost:98761 --username restoreuser
--password restorepwd --authenticationDatabase admin --db targetdb ./path/to/dump/

Solution 2

Often we confused with parameter in the mongoexport command with "Log-In" user. The command expects "Database Username" not Log-in username. This is one possibility to input wrong user name. "Database Username" can be found in "Users" tab for a database

Solution 3

I got my answer from this link: https://newbiedba.wordpress.com/2016/11/21/mongodb-3-2-server-returned-error-on-sasl-authentication-step-authentication-failed/

Except for all answers above, the only unmentioned reason is that my password has a special character '$' in it. I think this is a very common practice to have special characters and this may trip many without this simple tip:

When using command line mongo/mongostat/etc.. Single quote your username or password that has special characters!

Solution 4

mgo returns this error if username, password or database are wrong. Check your credentials twice. There are no other situations when you can see Authentication failed error message.

Solution 5

The error you report seem the cause of the authentication fail is caused by a nil pointer, you should check the data before using them to create the connection

Share:
93,458

Related videos on Youtube

Arjun Ajith
Author by

Arjun Ajith

Updated on March 09, 2022

Comments

  • Arjun Ajith
    Arjun Ajith about 2 years

    The following is my MongoDB connection dial from GoLang. But it's returning a panic "server returned error on SASL authentication step: Authentication failed.". My username, password, hostAddrs and dbName are correct. What am I missing here?

    dbName: = os.Getenv("ENV_DBNAME")
    userName: = os.Getenv("ENV_DBUSER")
    password: = os.Getenv("ENV_DBPASS")
    dbHost: = os.Getenv("ENV_DBHOST")
    mongoDialInfo: = & mgo.DialInfo {
     Addrs: [] string {
      dbHost
     },
     Database: dbName,
     Username: userName,
     Password: password,
     Timeout: 60 * time.Second,
    }
    sess, err: = mgo.DialWithInfo(mongoDialInfo)
    if (err != nil) {
     panic(err)
    
    }
    
  • Arjun Ajith
    Arjun Ajith over 7 years
    server returned error on SASL authentication step: Authentication failed. panic: runtime error: invalid memory address or nil pointer dereference panic: runtime error: invalid memory address or nil pointer dereference
  • CrazyCrow
    CrazyCrow over 7 years
    Yes this is correct way to implement connection. Here is working code gist.github.com/bearburger/718dec31746762684a7a512aed992a37 that connects demo DB.
  • Rav
    Rav almost 7 years
    Thanks, in my case that was exactly the solution.
  • ahaurat
    ahaurat over 6 years
    In my case, I was using my mLab password instead of the db password. Silly mistake but this answer helped me figure it out!
  • vabm
    vabm over 6 years
    I'm confused... what is the --authenticationDatabase parameter? the name of the database you want to access?
  • alaster
    alaster about 6 years
    In my case I had access to only one db on server. So I needed to just add -d <dbname> option. No need of --authenticationDatabase for me
  • rrw
    rrw about 6 years
    I dont know how the up-voter understood you, but I am sure I understood nothing from your answer. Mind elaborating more...?
  • Bestbug
    Bestbug about 6 years
    Well after more than 1 year I don't know exactly what I was thinking. Probably my idea was the Golang application can't retreive one or more env_variable from the host.
  • Zach B.
    Zach B. about 6 years
    Exactly my problem. Thank you.
  • Pax Beach
    Pax Beach about 6 years
    It is the right advice, but I had '%' sign in my password, and in the cmd.exe script the sign has disappeared. I had the same error =)
  • Bruno Fernandes
    Bruno Fernandes over 5 years
    I had a ';' in my password!
  • tyleax
    tyleax about 5 years
    Thanks this was the solution after searching a while for what was wrong.
  • Andrei Radulescu
    Andrei Radulescu almost 5 years
    tl;dr "To solve the issue, simply single quote the password field." e.g. -p 'password'
  • Jigar Prajapati
    Jigar Prajapati over 4 years
    changing few things and it's working thank you so much bhai
  • Jack
    Jack about 4 years
    Thank you, I had a & which was causing the issue
  • DummyBeginner
    DummyBeginner over 3 years
    My problem was using shorthands -u and -p even beside --authenticationDatabase in mongoimport. Replacing them with their long-form respectively ( --username and --password) along with the --authenticationDatabase solved the problem.
  • Yonatan Naor
    Yonatan Naor almost 3 years
    You are my hero!