mongodb单节点副本集添加仲裁

2020-05-25  本文已影响0人  OrangeLoveMilan

服务器信息

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<<EOF
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb27019/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongo27019
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb27019/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27019
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.
setParameter:
    enableLocalhostAuthBypass: true


#security:

#operationProfiling:

#replication:
replication:
   replSetName: lugotestrepl

#sharding:

## Enterprise-Only Options
security:
    keyFile: /etc/mongokey/mongodb-keyfile
    clusterAuthMode: keyFile

sharding:
  clusterRole: shardsvr

EOF

-----------------------------------------------------------------------------
cat >>/usr/lib/systemd/system/mongod27019.service<<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod27019.conf"
ExecStart=/usr/bin/mongod $OPTIONS 
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb27019
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb27019
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb27019
PermissionsStartOnly=true
PIDFile=/var/run/mongodb27019/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target

EOF

启动并且开机自启动

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"
}

数据一致

上一篇 下一篇

猜你喜欢

热点阅读