如何在golang中连接mongodb 3.0

时间:2015-04-20 10:24:30

标签: mongodb go mgo

当我从mongodb3.0服务器升级到版本2.6时,无法通过gogo使用mgo进行连接。

我在连接字符串中添加'authMechanism=SCRAM-SHA-1',但仍然无法连接到服务器。我得到的错误是SASL support not enabled during build (-tags sasl)

2 个答案:

答案 0 :(得分:3)

我有类似的问题。令人误解的是,我发现网络周围必须包含“labix.org/v2/mgo”软件包,尽管在官方网站上http://labix.org/mgo(在阅读时)它有更新和更新的信息,指向至少为我工作的包“gopkg.in/mgo.v2”。

我希望这可以提供帮助,因为我没有成功完成相同的步骤,然后我更改了包引用。 这段代码适用于我的情况:

  package main

  import (
    "fmt"
    "time"

    "gopkg.in/mgo.v2"
  )

  //const MongoDb details
  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

答案 1 :(得分:0)

更改服务器配置:

class PaymentsController < ApplicationController def payment @payment=Vendor.new @add_payment=AddPayment.new respond_to do |format| format.html format.js end end def check_type if params[:commit]=="submit" @vendor_type = PaymentVendor.where(:v_name => params[:v_name]).map{|v|[v.v_catagory ,v.Receipt_No]} #@vendor_type = PaymentVendor.where(:v_name => params[:v_name]).pluck(:v_catagory) output=[] @result=[] @vendor_type.each do |i| if i.first == params[:payment][:s_catagory] output << i[1] end end output.each_with_index{|val, index| #puts "#{val} => #{index}" #puts output1[index] @result << PaymentVendor.find_by_Receipt_No(output[index]) } else @v_name=Vendor.where(:s_catagory => params[:payment][:s_catagory] ).pluck(:v_name) end end def add_payment if params[:commit] else if params[:add_payment][:p_catagory]=="Cheque" end end end end
> var schema = db.system.version.findOne({"_id" : "authSchema"})
> schema.currentVersion = 3

https://jira.mongodb.org/browse/SERVER-17459