3

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.

CrazyCrow
  • 4,125
  • 1
  • 28
  • 39

1 Answers1

5

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.

CrazyCrow
  • 4,125
  • 1
  • 28
  • 39
  • I tried ur answer (added user database, added databasename to connection), but nothing is happened :( – Влад Дарьев Feb 25 '16 at 19:38
  • @ВладДарьев this is impossible. Try this connection string `mongodb://souser:123456@ds055855.mlab.com:55855/catalog` – CrazyCrow Feb 25 '16 at 19:41
  • the same error:( `session, err := mgo.Dial("mongodb://souser:123456@ds055855.mlab.com:55855/catalog")` **panic: server returned error on SASL authentication step: Authentication failed.** – Влад Дарьев Feb 25 '16 at 19:46
  • @ВладДарьев I'm not sure if your issue is repeatable. Here is result of your code execution http://i.imgur.com/ucc42Vm.png There is nothing added or changed (except connection string) as you can see and it works. Can you connect to your Mongolab instance with tools like RoboMongo? – CrazyCrow Feb 25 '16 at 20:02
  • 1
    yeah... It is good. I found excess string of code, but problem was not there, i think ur advice about creating user for database was solution. THANK YOU VERY MUCH!) – Влад Дарьев Feb 25 '16 at 20:21