Arjun Ajith
Arjun Ajith

Reputation: 1920

server returned error on SASL authentication step: Authentication failed

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)

}

Upvotes: 89

Views: 113972

Answers (10)

Georgy Gobozov
Georgy Gobozov

Reputation: 13731

In my case specifying auth database and auth mechanism in uri did help

./mongoimport --uri="mongodb://root:root@localhost:27017/labelDb?authSource=admin&authMechanism=SCRAM-SHA-1" --collection=repo_item --file=/tmp/repo_item.json

Upvotes: 3

Avik
Avik

Reputation: 681

If you are trying to connect your MongoDB Atlas to the Golang Application, the Connection function would be like this:

func getSession() *mgo.Session {

    tlsConfig := &tls.Config{}

    dialInfo := &mgo.DialInfo{
        Addrs: []string{<add your MongoDB shards as string array> }
        Username: "<MongoDB  username>",
        Password: "<MongoDB  password",
    }
    dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
        conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
        return conn, err
    }

    session, err := mgo.DialWithInfo(dialInfo)

    if err != nil {
        panic(err)
    } else {
        fmt.Printf("DB connected")
    }
    return session
}

Upvotes: 0

Adel Afsharipour
Adel Afsharipour

Reputation: 1

In my case, I was missing both --authenticationDatabase & --ssl, so here goes the full syntax for importing a json file into a Mongodb collection over an Atlas cluster (into the primary shard):

./mongoimport --host mycluster-shard-00-02.d0b2r.mongodb.net:27017 --authenticationDatabase admin --username TestUser --db Test --collection Messages --type json --file RAW.json --ssl

Upvotes: 0

e18r
e18r

Reputation: 8161

I got this error while using a connection string from a Heroku app by means of the --uri flag. What solved it in my case was adding the database name with -d:

mongodb_uri="$(heroku config:get MONGODB_URI -a myapp)"
mongorestore --uri=$mongodb_uri -d heroku_7m41q4xs db/

Upvotes: 1

Seunghun Sunmoon Lee
Seunghun Sunmoon Lee

Reputation: 469

I had the same error when using with dokku mongo:import . In my case I included dot(period) in my db name

You shouldn't include dot in your mongodb name when 'dokku mongo:create ' I've changed it to seunghunlee instead of seunghunlee.net now this command works

dokku mongo:import seunghunlee < seunghunlee.net.dump.gz

Hope it helps!

Upvotes: 0

Michael Shang
Michael Shang

Reputation: 756

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!

Upvotes: 10

Anantha
Anantha

Reputation: 2186

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/

Upvotes: 206

Geomy George
Geomy George

Reputation: 179

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

Upvotes: 17

Bestbug
Bestbug

Reputation: 485

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

Upvotes: 1

CrazyCrow
CrazyCrow

Reputation: 4235

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.

Upvotes: 4

Related Questions