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

huangapple go评论118阅读模式
英文:

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

问题

我正在尝试使用mgo库从Go连接到远程MongoDB数据库(Mongolab),但是遇到错误panic: server returned error on SASL authentication step: Authentication failed。以下是我的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. "gopkg.in/mgo.v2/bson"
  6. "log"
  7. )
  8. type Person struct {
  9. Name string
  10. Phone string
  11. }
  12. func main() {
  13. session, err := mgo.Dial("mongodb://<dbusername>:<dbpassword>@ds055855.mlab.com:55855")
  14. if err != nil {
  15. panic(err)
  16. }
  17. defer session.Close()
  18. // Optional. Switch the session to a monotonic behavior.
  19. session.SetMode(mgo.Monotonic, true)
  20. c := session.DB("catalog").C("History")
  21. err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
  22. &Person{"Cla", "+55 53 8402 8510"})
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. result := Person{}
  27. err = c.Find(bson.M{"name": "Ale"}).One(&result)
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. fmt.Println("Phone:", result.Phone)
  32. }

你可以如何解决这个问题?当然,在我的代码中,星号部分应该替换为我的登录名和密码。

英文:

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

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;gopkg.in/mgo.v2&quot;
  5. &quot;gopkg.in/mgo.v2/bson&quot;
  6. &quot;log&quot;
  7. )
  8. type Person struct {
  9. Name string
  10. Phone string
  11. }
  12. func main() {
  13. session, err := mgo.Dial(&quot;mongodb://&lt;dbusername&gt;:&lt;dbpassword&gt;@ds055855.mlab.com:55855&quot;)
  14. if err != nil {
  15. panic(err)
  16. }
  17. defer session.Close()
  18. // Optional. Switch the session to a monotonic behavior.
  19. session.SetMode(mgo.Monotonic, true)
  20. c := session.DB(&quot;catalog&quot;).C(&quot;History&quot;)
  21. err = c.Insert(&amp;Person{&quot;Ale&quot;, &quot;+55 53 8116 9639&quot;},
  22. &amp;Person{&quot;Cla&quot;, &quot;+55 53 8402 8510&quot;})
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. result := Person{}
  27. err = c.Find(bson.M{&quot;name&quot;: &quot;Ale&quot;}).One(&amp;result)
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. fmt.Println(&quot;Phone:&quot;, result.Phone)
  32. }

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

答案1

得分: 5

请检查您是否为Mongolab数据库实例添加了用户(如果您的数据库名称是catalog,请访问https://mongolab.com/databases/catalog#users),因为默认情况下用户列表是空的(帐户用户名/密码!=数据库用户名/密码)。

还请将/&lt;databasename&gt;添加到您的连接字符串的末尾- mongodb://*******:*******@ds045795.mongolab.com:45795/databasename

英文:

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 /&lt;databasename&gt; to the end of your connection string - mongodb://*******:*******@ds045795.mongolab.com:45795/databasename.

huangapple
  • 本文由 发表于 2016年2月26日 02:25:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/35635293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定