mongodb单节点副本集添加仲裁

服务器信息

ip 系统 配置 服务 目录
172.24.32.201 centos7.7 2c4g mongo-primary /var/lib/mongo (the data directory) /var/log/mongodb (the log directory)
172.24.32.201 centos7.7 2c4g mongo-secondary /var/lib/mongo27018 (the data directory) /var/log/mongodb27018 (the log directory)
172.24.32.201 centos7.7 2c4g mongo-abiter /var/lib/mongo27019 (the data directory) /var/log/mongodb27019 (the log directory)

创建新的mongo实例,27019,具体文档参考mongo单点升级成单节点副本
这里直接贴出指令:

mkdir -p /var/log/mongodb27019/
mkdir -p /var/lib/mongo27019
chown mongod:mongod /var/log/mongodb27019/ &&chmod 0755 /var/log/mongodb27019/
chown mongod:mongod /var/lib/mongo27019/ &&chmod 0755 /var/lib/mongo27019/

-------------------------------------------------------------------------
cat >>/etc/mongod27019.conf<>/usr/lib/systemd/system/mongod27019.service<

启动并且开机自启动

systemctl daemon-reload
systemctl start mongod27019
systemctl enable mongod27019

添加仲裁

进入mongo27017(primary节点)

添加仲裁

rs.addArb('172.24.32.201:27019')

结果如下:

lugotestrepl:PRIMARY> db.auth('admin','test123')
1
lugotestrepl:PRIMARY> rs.addArb('172.24.32.201:27019')
{ "ok" : 1 }

查看副本集拓扑结构

rs.status()
lugotestrepl:PRIMARY> rs.status()
{
    "set" : "lugotestrepl",
    "date" : ISODate("2020-05-14T08:10:49.745Z"),
    "myState" : 1,
    "term" : NumberLong(2),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589443843, 1),
            "t" : NumberLong(2)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589443843, 1),
            "t" : NumberLong(2)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589443843, 1),
            "t" : NumberLong(2)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.24.32.201:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 3989,
            "optime" : {
                "ts" : Timestamp(1589443843, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2020-05-14T08:10:43Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1589439871, 1),
            "electionDate" : ISODate("2020-05-14T07:04:31Z"),
            "configVersion" : 3,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.24.32.201:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 3978,
            "optime" : {
                "ts" : Timestamp(1589443843, 1),
                "t" : NumberLong(2)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589443843, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2020-05-14T08:10:43Z"),
            "optimeDurableDate" : ISODate("2020-05-14T08:10:43Z"),
            "lastHeartbeat" : ISODate("2020-05-14T08:10:49.635Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T08:10:49.672Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.24.32.201:27017",
            "syncSourceHost" : "172.24.32.201:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 3
        },
        {
            "_id" : 2,
            "name" : "172.24.32.201:27019",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 78,
            "lastHeartbeat" : ISODate("2020-05-14T08:10:49.635Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T08:10:46.739Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : 3
        }
    ],
    "ok" : 1
}

验证数据可用性,插入一条数据,并且查看所有数据

lugotestrepl:PRIMARY> use lugotest;
switched to db lugotest
lugotestrepl:PRIMARY> db.auth('lugo','test123')
1
lugotestrepl:PRIMARY> show collections;
movie
lugotestrepl:PRIMARY> db.movie.insert({"moviename":"杀死比尔","points":"9.0"});

查看

lugotestrepl:PRIMARY> db.movie.find().pretty()
{
    "_id" : ObjectId("5ebbbca5692adbface98b2af"),
    "moviename" : "大侦探福尔摩斯",
    "points" : "9.5"
}
{
    "_id" : ObjectId("5ebbbca5692adbface98b2b0"),
    "moviename" : "掠夺",
    "points" : "9.2"
}
{
    "_id" : ObjectId("5ebbbca5692adbface98b2b1"),
    "moviename" : "摇滚黑帮",
    "points" : "9.9"
}
{
    "_id" : ObjectId("5ebbbca5692adbface98b2b2"),
    "moviename" : "两杆大烟枪",
    "points" : "9.1"
}
{
    "_id" : ObjectId("5ebcff34df72ed1bb0505234"),
    "moviename" : "杀死比尔",
    "points" : "9.0"
}

去secondary节点查看数据

lugotestrepl:SECONDARY> rs.slaveOk();
lugotestrepl:SECONDARY> db.auth('lugo','test123')
1
lugotestrepl:SECONDARY> db.movie.find().pretty()
{
    "_id" : ObjectId("5ebbbca5692adbface98b2af"),
    "moviename" : "大侦探福尔摩斯",
    "points" : "9.5"
}
{
    "_id" : ObjectId("5ebbbca5692adbface98b2b0"),
    "moviename" : "掠夺",
    "points" : "9.2"
}
{
    "_id" : ObjectId("5ebbbca5692adbface98b2b1"),
    "moviename" : "摇滚黑帮",
    "points" : "9.9"
}
{
    "_id" : ObjectId("5ebbbca5692adbface98b2b2"),
    "moviename" : "两杆大烟枪",
    "points" : "9.1"
}
{
    "_id" : ObjectId("5ebcff34df72ed1bb0505234"),
    "moviename" : "杀死比尔",
    "points" : "9.0"
}

数据一致

你可能感兴趣的:(mongodb单节点副本集添加仲裁)