Golang和MongoDb远程访问失败(SASL身份验证步骤上的服务器返回错误:身份验证失败。)

时间:2016-02-25 18:25:08

标签: mongodb authentication go remote-access mlab

我正在尝试使用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)
}

我该如何解决这个问题?当然,在我的代码中代替星号,我写了我的登录名和密码。

1 个答案:

答案 0 :(得分:4)

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

同时将/<databasename>添加到连接字符串的末尾 - mongodb://*******:*******@ds045795.mongolab.com:45795/databasename

相关问题