Golang and MongoDb remote access fail (server returned error on SASL authentication step: Authentication failed.)

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I am trying to connect to remote MongoDB database (Mongolab) from Go with mgo library but getting error panic: server returned error on SASL authentication step: Authentication failed. Here is my code

package main  import (     "fmt"     "gopkg.in/mgo.v2"     "gopkg.in/mgo.v2/bson"     "log" )  type Person struct {     Name  string     Phone string }  func main() {     session, err := mgo.Dial("mongodb://<dbusername>:<dbpassword>@ds055855.mlab.com:55855")      if err != nil {         panic(err)     }     defer session.Close()      // Optional. Switch the session to a monotonic behavior.     session.SetMode(mgo.Monotonic, true)      c := session.DB("catalog").C("History")     err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},         &Person{"Cla", "+55 53 8402 8510"})     if err != nil {         log.Fatal(err)     }      result := Person{}     err = c.Find(bson.M{"name": "Ale"}).One(&result)     if err != nil {         log.Fatal(err)     }      fmt.Println("Phone:", result.Phone) } 

How can I fix this? And of course instead of stars in my code i write my login and password.

回答1:

Please check if you have added users for your Mongolab database instance (https://mongolab.com/databases/catalog#users in case if your DB name is catalog) because by default your users list is empty (account user/password != database user/password).

Also add /<databasename> to the end of your connection string - mongodb://*******:*******@ds045795.mongolab.com:45795/databasename.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!