Configure Replica Set Tag Sets配置副本集标记集
On this page本页内容
A replica set member or members can be configured with 一个或多个副本集成员可以使用tags
:tags
进行配置:
{ "<tag1>": "<string1>", "<tag2>": "<string2>",... }
For read operations, you can specify a tag set in the read preferences to help direct read operations to members that have specific tag(s).对于读取操作,可以在读取首选项中指定一个标记集,以帮助将读取操作定向到具有特定标记的成员。
For write operations, you can use the tags to create a custom write concern.对于写入操作,可以使用标记创建自定义写入关注。
Use Tag Sets in Read Preference在读取首选项中使用标记集
If a replica set member or members are associated with 如果一个或多个复制副本集成员与tags
, you can specify a tag set in the read preference to target those members. tags
相关联,则可以在读取首选项中指定一个标记集以针对这些成员。A tag set is an array of documents, where each document contains the tag and value pair(s). 标记集是一个文档数组,其中每个文档都包含标记和值对。The specifications are tried in order until a match is found. Once found, that specification is used to find all eligible matching members.将按顺序尝试这些规范,直到找到匹配项。一旦找到,该规范将用于查找所有符合条件的匹配成员。
For example, a replica set has the following replica set configuration (some of the fields have been omitted for brevity):例如,副本集具有以下副本集配置(为了简洁起见,省略了一些字段):
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{ "_id" : 0, "host" : "mongodb0.example.net:27017", ..., "tags": { }, ... },
{ "_id" : 1, "host" : "mongodb1.example.net:27017", ..., "tags": { }, ... },
{ "_id" : 2, "host" : "mongodb2.example.net:27017", ..., "tags": { }, ... }
],
"settings" : {
...
}
}
Add tags to the members.向成员添加标记。Connect将mongosh
to the replica set and users.reconfig()
to add tags to the members:mongosh
连接到副本集,并使用rs.reconfig()
为成员添加标记:conf = rs.conf();
conf.members[0].tags = { "dc": "east", "usage": "production" };
conf.members[1].tags = { "dc": "east", "usage": "reporting" };
conf.members[2].tags = { "dc": "west", "usage": "production" };
rs.reconfig(conf);Verify the replica set configuration.验证复制副本集配置。Run运行rs.conf()
to verify the replica set configuration (some of the fields have been omitted for brevity). Thers.conf()
returns a document similar to the following:rs.conf()
来验证副本集配置(为了简洁起见,省略了一些字段)。rs.conf()
返回一个类似于以下内容的文档:{
"_id" : "rs0",
"version" : 2,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017",
...
"tags" : {
"dc": "east",
"usage": "production"
},
...
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017",
...
"tags" : {
"dc": "east",
"usage": "reporting"
},
...
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017",
...
"tags" : {
"dc": "west",
"usage": "production"
},
...
}
],
"settings" : {
...
}
}Specify tag sets in the read preference.在读取首选项中指定标记集。To direct read operations to the secondaries tagged with a particular tag(s), in the要将读取操作定向到用特定标记标记的辅助文件,在连接到副本集的mongo shell中,可以使用mongo
shell connected to the replica set, you can use thereadPref()
method to specify the read preference mode and the tag set.readPref()
方法指定读取首选项模式和标记集。For example,例如:To direct read operations to the secondary tagged with both要将读取操作定向到同时标记有"dc": "east"
and"usage": "production"
, include the following tag set:"dc": "east"
和"usage": "production"
的辅助,请包括以下标记集:db.collection.find({}).readPref( "secondary", [ { "dc": "east", "usage": "production" } ] )
To direct a read operation to the secondaries tagged with要将读取操作定向到标记有"dc": "east"
, and if not found, to secondaries tagged with"usage": "production"
, include the following tag set:"dc": "east"
的辅助设备,如果找不到,则定向到标记为"usage": "production"
的辅助系统,请包括以下标记集:db.collection.find({}).readPref( "secondary", [ { "dc": "east"}, { "usage": "production" } ] )
TipSee also:另请参阅:
Custom Multi-Datacenter Write Concerns自定义多数据中心写入关注
If a replica set member or members are associated with 如果一个或多个副本集成员与tags
, you can configure the replica set's settings.getLastErrorModes
setting to create a custom write concern.tags
关联,则可以配置副本集的settings.getLastErrorModes
设置以创建自定义写入关注。
Given a five member replica set with members in two data centers:给定一个由五个成员组成的复制副本集,其成员位于两个数据中心:
a facility标记为VA
taggeddc_va
dc_VA
的设施VA
a facility设施CA
taggeddc_ca
CA
标记为dc_CA
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{ "_id" : 0, "host" : "mongodb0.example.net:27017", ..., "tags": { }, ... },
{ "_id" : 1, "host" : "mongodb1.example.net:27017", ..., "tags": { }, ... },
{ "_id" : 2, "host" : "mongodb2.example.net:27017", ..., "tags": { }, ... }
{ "_id" : 3, "host" : "mongodb3.example.net:27017", ..., "tags": { }, ... }
{ "_id" : 4, "host" : "mongodb4.example.net:27017", ..., "tags": { }, ... }
],
"settings" : {
...
}
}
Add tags to the replica set members.向复制副本集成员添加标记。Connect将mongosh
to the replica set and users.reconfig()
to add tags to the members:mongosh
连接到副本集,并使用rs.reconfig()
为成员添加标记:conf = rs.conf();
conf.members[0].tags = { "dc_va": "rack1"};
conf.members[1].tags = { "dc_va": "rack2"};
conf.members[2].tags = { "dc_ca": "rack1"};
conf.members[3].tags = { "dc_ca": "rack2"};
conf.members[4].tags = { "dc_va": "rack1"};
rs.reconfig(conf);Create a custom write concern.创建自定义写入关注。In the replica set configuration, define a custom write concern in the在副本集配置中,在settings.getLastErrorModes
setting. For example, the following defines the custom write concernMultipleDC
that requires the write to propagate to two members with differentdc_va
tag values and one member with anydc_ca
tag value.settings.getLastErrorModes
设置中定义自定义写入关注。例如,以下定义了自定义写操作关注MultipleDC
,它要求写操作传播到具有不同dc_va
标记值的两个成员和具有任何dc_ca
标签值的一个成员。conf = rs.conf();
conf.settings = { getLastErrorModes: { MultipleDC : { "dc_va": 2, "dc_ca": 1 } } };
rs.reconfig(conf);NoteThe如果写入传播到具有相同MultipleDC
write concern is not satisfied if the write propagates to two members with the same"dc_va"
tag."dc_va"
标记的两个成员,则不满足MultipleDC
写入关注。For example, if the write has only propagated to例如,如果写入仅传播到members[0]
andmembers[4]
,"dc_va": 2
is not satisfied since they have the same tag value"rack1"
.members[0]
和members[4]
,则不满足"dc_va": 2
,因为它们具有相同的标记值"rack1"
。Use the custom write concern.使用自定义写入关注。To use the custom write concern, pass in the write concern name to the要使用自定义写入关注,请将写入关注名称传递给写入关注中的w
Option in the write concern:w
选项:db.collection.insertOne(
{ id: "xyz", status: "A" },
{ writeConcern: { w: "MultipleDC" } }
)