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

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

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 (
	&quot;fmt&quot;
	&quot;gopkg.in/mgo.v2&quot;
	&quot;gopkg.in/mgo.v2/bson&quot;
	&quot;log&quot;
)

type Person struct {
	Name  string
	Phone string
}

func main() {
	session, err := mgo.Dial(&quot;mongodb://&lt;dbusername&gt;:&lt;dbpassword&gt;@ds055855.mlab.com:55855&quot;)

	if err != nil {
		panic(err)
	}
	defer session.Close()

	// Optional. Switch the session to a monotonic behavior.
	session.SetMode(mgo.Monotonic, true)

	c := session.DB(&quot;catalog&quot;).C(&quot;History&quot;)
	err = c.Insert(&amp;Person{&quot;Ale&quot;, &quot;+55 53 8116 9639&quot;},
		&amp;Person{&quot;Cla&quot;, &quot;+55 53 8402 8510&quot;})
	if err != nil {
		log.Fatal(err)
	}

	result := Person{}
	err = c.Find(bson.M{&quot;name&quot;: &quot;Ale&quot;}).One(&amp;result)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(&quot;Phone:&quot;, 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),因为默认情况下用户列表是空的(帐户用户名/密码!=数据库用户名/密码)。

还请将/&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:

确定