MongoDB Connection Dialing Error: SASL Authentication Failure
When attempting to establish a MongoDB connection using the provided GoLang code snippet, a panic occurs with the error message "server returned error on SASL authentication step: Authentication failed." Despite ensuring the validity of username, password, host, and database name, the connection fails.
Solution:
A common reason for this issue in MongoDB is the absence of the --authenticationDatabase parameter when connecting to a remote server. This parameter specifies the database that contains the users' credentials.
To resolve this issue, add the --authenticationDatabase parameter to your code as follows:
mongoDialInfo: = & mgo.DialInfo { Addrs: [] string { dbHost }, Database: dbName, Username: userName, Password: password, **AuthenticationDatabase: "admin",** // Specify the credentials database Timeout: 60 * time.Second, }
With this modification, the connection should succeed as the admin database is the default database where user credentials are stored in MongoDB.
The above is the detailed content of MongoDB GoLang Connection Error: How to Fix SASL Authentication Failure?. For more information, please follow other related articles on the PHP Chinese website!