Home >Backend Development >Golang >Why Does My GoLang MongoDB Connection Fail with 'Authentication Failed' and How Can I Fix It?
A GoLang MongoDB connection attempt fails with the enigmatic error message "server returned error on SASL authentication step: Authentication failed." Despite ensuring the correctness of credentials and other connection parameters, the issue persists.
In some cases, explicitly specifying the authentication database using the --authenticationDatabase parameter resolves the authentication failure. This parameter denotes the database against which the MongoDB user credentials should be validated. Incorporate it into your code as demonstrated below:
mongoDialInfo := &mgo.DialInfo{ Addrs: []string{dbHost}, Database: dbName, Username: userName, Password: password, Timeout: 60 * time.Second, AuthenticationDatabase: "admin", // Specify authentication database here }
The above is the detailed content of Why Does My GoLang MongoDB Connection Fail with 'Authentication Failed' and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!