英文:
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
。以下是我的代码:
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)
}
你可以如何解决这个问题?当然,在我的代码中,星号部分应该替换为我的登录名和密码。
英文:
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
得分: 5
请检查您是否为Mongolab数据库实例添加了用户(如果您的数据库名称是catalog
,请访问https://mongolab.com/databases/catalog#users),因为默认情况下用户列表是空的(帐户用户名/密码!=数据库用户名/密码)。
还请将/<databasename>
添加到您的连接字符串的末尾- 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 /<databasename>
to the end of your connection string - mongodb://*******:*******@ds045795.mongolab.com:45795/databasename
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论