英文:
Is it possible to run mongo replicaset commands using mgo driver?
问题
是的,可以使用mgo驱动程序从Go应用程序运行副本集命令,例如rs.initiate()
和rs.add()
。
要使用mgo驱动程序运行副本集命令,你需要按照以下步骤进行操作:
- 导入mgo包:
import "gopkg.in/mgo.v2"
- 创建mgo.Session对象:
session, err := mgo.Dial("mongodb://localhost:27017")
if err != nil {
// 处理错误
}
defer session.Close()
- 获取mgo.Database对象:
db := session.DB("your_database_name")
- 运行副本集命令:
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)
}
注意以下几点:
-
连接字符串中的
?connect=direct
。如果未指定,连接将超时,可能是因为副本集尚未初始化。
-
session.SetMode(mgo.Monotonic, true)
。会话模式应为
monotonic
,因为mgo使用的默认会话是primary
,它在主节点上执行所有操作。由于副本集尚未初始化,因此不会有主节点,操作(在本例中为replSetInitiate
)将超时。 -
配置中的
"_id": "my_replica_set"
。为了使其工作,Mongo服务器必须使用副本集名称
my_replica_set
启动。一种方法是:mongod --replSet my_replica_set
-
根据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:
?connect=direct
in the connection string.
If not specified, the connection will timeout, probably because the replica set has not been initialized yet.
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
"_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
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论