如何在Golang中连接MongoDB 3.0?

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

how to connect mongodb 3.0 in golang

问题

当我将我的mongodb服务器从版本2.6升级到3.0时,无法使用mgo从golang连接。

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

英文:

When I upgrade my mongodb server to version 3.0 from 2.6 it can't connect from golang use mgo.

I add 'authMechanism=SCRAM-SHA-1' in connection string, and it still can't connect to the server. The error that I get is SASL support not enabled during build (-tags sasl)

答案1

得分: 3

我遇到了类似的问题。在网络上误导性地发现,尽管在官方网站http://labix.org/mgo(阅读时)上有更新和更新的信息,指向至少对我有效的包"gopkg.in/mgo.v2",但必须包含"labix.org/v2/mgo"包。

我希望这可以帮助,因为我按照与你相同的步骤进行了尝试,但没有成功,然后我更改了包的引用。
以下是在我的情况下工作的代码:

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上。

英文:

I had similar issue. Misleadingly I found around the network that has to be included the "labix.org/v2/mgo" package despite the fact that on the official site http://labix.org/mgo (at the time of the reading) it has newer and updated information that points to at least working for me package "gopkg.in/mgo.v2".

I hope this can help since I got to the same steps as you without success and then I have changed the package reference.
This code worked in my case:

  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))
  }

It is also on Github

答案2

得分: 0

更改服务器配置:

> var schema = db.system.version.findOne({"_id" : "authSchema"})
<br/>
&gt; schema.currentVersion = 3
<br/>
&gt; db.system.version.save(schema)

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

英文:

change server configurations:

&gt; var schema = db.system.version.findOne({&quot;_id&quot; : &quot;authSchema&quot;})
<br/>
&gt; schema.currentVersion = 3
<br/>
&gt; db.system.version.save(schema)

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

huangapple
  • 本文由 发表于 2015年4月20日 18:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/29745280.html
匿名

发表评论

匿名网友

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

确定