在Golang中连接到远程MongoDB服务器失败,出现身份验证错误。

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

connection to remote mongodb server failed in golang, giving authentication error

问题

我正在尝试在Golang中连接到远程MongoDB服务器并向数据库中添加数据。它给我返回以下错误:
服务器在SASL认证步骤上返回错误:认证失败。

代码:

package main

import (
	"fmt"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"log"
	// "os"
)

type Person struct {
	Name  string
	Phone string
}

func main() {

	session, err := mgo.Dial("mongodb://<dbuser>:<dbpassword>@ds041154.mongolab.com:41154/location")

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("Session created")
	}

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

	c := session.DB("location").C("people")
	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 server in golang and adding data in database. Its giving me error as follows:
server returned error on SASL authentication step: Authentication failed.

Code:

package main

import (
	&quot;fmt&quot;
	&quot;gopkg.in/mgo.v2&quot;
	&quot;gopkg.in/mgo.v2/bson&quot;
	&quot;log&quot;
	// &quot;os&quot;
)

type Person struct {
	Name  string
	Phone string
}

func main() {

	session, err := mgo.Dial(&quot;mongodb://&lt;dbuser&gt;:&lt;dbpassword&gt;@ds041154.mongolab.com:41154/location&quot;)

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(&quot;Session created&quot;)
	}

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

	c := session.DB(&quot;location&quot;).C(&quot;people&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)

}

Any help on this is appreciated.

答案1

得分: 6

我遇到了类似的错误,但后来发现我输入了错误的登录凭据。

以下是适用于我的代码:

package main

import (
	"fmt"
	"time"

	"gopkg.in/mgo.v2"
)

// MongoDB的详细信息
const (
	hosts      = "ds026491.mongolab.com:26491"
	database   = "messagingdb"
	username   = "admin"
	password   = "youPassword"
	collection = "messages"
)

func main() {

	info := &mgo.DialInfo{
		Addrs:    []string{hosts},
		Timeout:  60 * time.Second,
		Database: database,
		Username: username,
		Password: password,
	}

	session, err1 := mgo.DialWithInfo(info)
	if err1 != nil {
		panic(err1)
	}

	col := session.DB(database).C(collection)

	count, err2 := col.Count()
	if err2 != nil {
		panic(err2)
	}
	fmt.Println(fmt.Sprintf("Messages count: %d", count))
}

你也可以在Github上找到这段代码。

英文:

I was getting similar error, but I found that I had entered wrong login credentials.

This code worked for me:

package main

import (
    &quot;fmt&quot;
    &quot;time&quot;

    &quot;gopkg.in/mgo.v2&quot;
)

//const MongoDb details
const (
    hosts      = &quot;ds026491.mongolab.com:26491&quot;
    database   = &quot;messagingdb&quot;
    username   = &quot;admin&quot;
    password   = &quot;youPassword&quot;
    collection = &quot;messages&quot;
)

func main() {

    info := &amp;mgo.DialInfo{
	    Addrs:    []string{hosts},
	    Timeout:  60 * time.Second,
	    Database: database,
	    Username: username,
	    Password: password,
    }

    session, err1 := mgo.DialWithInfo(info)
    if err1 != nil {
	    panic(err1)
    }

    col := session.DB(database).C(collection)

    count, err2 := col.Count()
    if err2 != nil {
	    panic(err2)
    }
    fmt.Println(fmt.Sprintf(&quot;Messages count: %d&quot;, count))
}

It is also on Github

答案2

得分: 4

你需要在需要进行身份验证的数据库上调用.Login(user, pass string)方法:

if err := session.DB(authDB).Login(user, pass); err != nil {
  panic(err)
}

请注意,这将对session进行身份验证,因此从它进行.Copy().Clone()的任何其他会话也将被身份验证。

英文:

You need to call .Login(user, pass string) on the database you need to authenticate with:

if err:= session.DB(authDB).Login(user, pass); err != nil {
  panic(err)
}

Note that this authenticates the session, so each other session you .Copy() or .Clone() from it is also authenticated.

huangapple
  • 本文由 发表于 2015年10月19日 07:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/33204589.html
匿名

发表评论

匿名网友

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

确定