我正在使用在端口 27017、27018 和 27019 上mtools
启动具有 3 个节点 ( ) 的副本集。mlaunch --replicaset
但是,我实际上很难使用Dial
mgo 包连接到副本集。下面的代码片段会出现以下消息no reachable servers
:
type Person struct {
Name string `bson:"name"`
Age int `bson:"age"`
}
func main() {
session, err := mgo.Dial("localhost:27017,localhost:27018,localhost:27019")
if err != nil {
panic(err)
}
c := session.DB("mydb").C("testCollection")
c.Insert(&Person{Name: "Foo", Age: 20})
}
究竟如何使用 mgo 包连接到副本集?