Home >Backend Development >Golang >Why Does My GoLang MongoDB Connection Fail with 'Authentication Failed' and How Can I Fix It?

Why Does My GoLang MongoDB Connection Fail with 'Authentication Failed' and How Can I Fix It?

DDD
DDDOriginal
2024-12-05 07:03:111026browse

Why Does My GoLang MongoDB Connection Fail with

Unable to Authenticate with MongoDB via SASL

Problem

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.

Solution

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!

Statement:
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