Home > Backend Development > Golang > MongoDB GoLang Connection Error: How to Fix SASL Authentication Failure?

MongoDB GoLang Connection Error: How to Fix SASL Authentication Failure?

Susan Sarandon
Release: 2024-12-10 16:33:20
Original
457 people have browsed it

MongoDB GoLang Connection Error: How to Fix SASL Authentication Failure?

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,
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template