使用mgo驱动程序是否可以运行MongoDB副本集命令?

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

Is it possible to run mongo replicaset commands using mgo driver?

问题

是的,可以使用mgo驱动程序从Go应用程序运行副本集命令,例如rs.initiate()rs.add()

要使用mgo驱动程序运行副本集命令,你需要按照以下步骤进行操作:

  1. 导入mgo包:
import "gopkg.in/mgo.v2"
  1. 创建mgo.Session对象:
session, err := mgo.Dial("mongodb://localhost:27017")
if err != nil {
    // 处理错误
}
defer session.Close()
  1. 获取mgo.Database对象:
db := session.DB("your_database_name")
  1. 运行副本集命令:
result := bson.M{}
err = db.Run(bson.D{{"command", "rs.initiate()"}}, &result)
if err != nil {
    // 处理错误
}

你可以根据需要修改命令和参数。确保在运行命令之前,你已经建立了与MongoDB副本集的连接。

希望这可以帮助到你!

英文:

Is it possible to run replica set commands like rs.initiate() and rs.add() using mgo driver from a golang application?

If yes, how??

答案1

得分: 5

感谢@alex-blex的答案给了我一个起点。但是以下是最终对我有用的代码:

session, err := mgo.Dial("rs1.example.net?connect=direct")
if err != nil {
    panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)

config := bson.M{
    "_id": "my_replica_set",
    "members": []bson.M{
        {"_id": 0, "host": "rs1.example.net:27017"},
        {"_id": 1, "host": "rs2.example.net:27017", "priority": 2},
        {"_id": 2, "host": "rs3.example.net", "arbiterOnly": true},
    },
}
result := bson.M{}
if err := session.Run(bson.M{"replSetInitiate": config}, &result); err != nil {
    panic(err)
}

注意以下几点:

  1. 连接字符串中的?connect=direct

    如果未指定,连接将超时,可能是因为副本集尚未初始化。

  2. session.SetMode(mgo.Monotonic, true)

    会话模式应为monotonic,因为mgo使用的默认会话是primary,它在主节点上执行所有操作。由于副本集尚未初始化,因此不会有主节点,操作(在本例中为replSetInitiate)将超时。

  3. 配置中的"_id": "my_replica_set"

    为了使其工作,Mongo服务器必须使用副本集名称my_replica_set启动。一种方法是:

    mongod --replSet my_replica_set
    
  4. 根据Mgo文档,在"admin"数据库上使用session.Run()执行命令。

英文:

Thanks to @alex-blex's answer which gave me the start. But this is what finally worked for me:

session, err := mgo.Dial("rs1.example.net?connect=direct")
if err != nil {
    panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)

config := bson.M{
	"_id": "my_replica_set",
	"members": []bson.M{
		{"_id": 0, "host": "rs1.example.net:27017"},
		{"_id": 1, "host": "rs2.example.net:27017", "priority": 2},
		{"_id": 2, "host": "rs3.example.net", "arbiterOnly": true},
	},
}
result := bson.M{}
if err := session.Run(bson.M{"replSetInitiate": config}, &result); err != nil {
	panic(err)
}

Notice the following:

  1. ?connect=direct in the connection string.

If not specified, the connection will timeout, probably because the replica set has not been initialized yet.

  1. session.SetMode(mgo.Monotonic, true)

Session mode should be monotonic as the default session used by mgo is primary which performs all operations on primary. Since the replica set has not been initialized yet, there wont be a primary and the operation (in this case, replSetInitiate) will just timeout

  1. "_id": "my_replica_set" in the config

For this to work, the mongo servers will have to be started with the replica set name my_replica_set. One way of doing that would be:

    mongod --replSet my_replica_set
  1. As per Mgo docs use session.Run() for commands on the "admin" database

答案2

得分: 4

rs.initiate 命令的示例:

session, err := mgo.Dial("rs1.example.net")
if err != nil {
    panic(err)
}
defer session.Close()

config := bson.M{
    "_id": "my_replica_set",
    "members": []bson.M{
        {"_id": 0, "host": "rs1.example.net:27017"},
        {"_id": 1, "host": "rs2.example.net:27017", "priority": 2},
        {"_id": 2, "host": "rs3.example.net", "arbiterOnly": true},
    },
}
result := bson.M{}
if err := session.DB("admin").Run(bson.M{"replSetInitiate": config}, &result); err != nil {
    panic(err)
}

rs.add 和其他 rs.* 助手命令也是类似的用法。

英文:

An example for rs.initiate command:

session, err := mgo.Dial("rs1.example.net")
if err != nil {
    panic(err)
}
defer session.Close()

config := bson.M{
	"_id": "my_replica_set",
	"members": []bson.M{
		{"_id": 0, "host": "rs1.example.net:27017"},
		{"_id": 1, "host": "rs2.example.net:27017", "priority": 2},
		{"_id": 2, "host": "rs3.example.net", "arbiterOnly": true},
	},
}
result := bson.M{}
if err := session.DB("admin").Run(bson.M{"replSetInitiate": config}, &result); err != nil {
	panic(err)
}

Same for rs.add and any other rs.* helpers.

huangapple
  • 本文由 发表于 2017年5月26日 15:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/44196113.html
匿名

发表评论

匿名网友

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

确定