Docs HomeMongoDB Manual

Configure Replica Set Tag Sets配置副本集标记集

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.将按顺序尝试这些规范,直到找到匹配项。一旦找到,该规范将用于查找所有符合条件的匹配成员。

Note

You cannot specify a tag set when specifying read preference mode primary.在指定读取首选项模式primary时,不能指定标记集。

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" : {
...
}
}
  1. Add tags to the members.向成员添加标记。

    Connect mongosh to the replica set and use rs.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);
  2. Verify the replica set configuration.验证复制副本集配置。

    Run rs.conf() to verify the replica set configuration (some of the fields have been omitted for brevity). The rs.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" : {
    ...
    }
    }
  3. Specify tag sets in the read preference.在读取首选项中指定标记集。

    To direct read operations to the secondaries tagged with a particular tag(s), in the mongo shell connected to the replica set, you can use the readPref() method to specify the read preference mode and the tag set. 要将读取操作定向到用特定标记标记的辅助文件,在连接到副本集的mongo shell中,可以使用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" } ] )
    Tip

    See also: 另请参阅:

    Mongo.setReadPref()

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:给定一个由五个成员组成的复制副本集,其成员位于两个数据中心:

  1. a facility VA tagged dc_va标记为dc_VA的设施VA
  2. a facility CA tagged dc_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" : {
...
}
}
  1. Add tags to the replica set members.向复制副本集成员添加标记。

    Connect mongosh to the replica set and use rs.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);
  2. 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 concern MultipleDC that requires the write to propagate to two members with different dc_va tag values and one member with any dc_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);
    Note

    The 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] and members[4], "dc_va": 2 is not satisfied since they have the same tag value "rack1".例如,如果写入仅传播到members[0]members[4],则不满足"dc_va": 2,因为它们具有相同的标记值"rack1"

  3. 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" } }
    )