server returned error on SASL authentication step:

2019-01-31 18:15发布

问题:

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)

}

回答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/


回答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



回答3:

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.



回答4:

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



回答5:

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!