服務(wù)器信息
ip | 系統(tǒng) | 配置 | 服務(wù) | 目錄 |
---|---|---|---|---|
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) |
創(chuàng)建新的mongo實例巷送,27019,具體文檔參考mongo單點升級成單節(jié)點副本
這里直接貼出指令:
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節(jié)點)
添加仲裁
rs.addArb('172.24.32.201:27019')
結(jié)果如下:
lugotestrepl:PRIMARY> db.auth('admin','test123')
1
lugotestrepl:PRIMARY> rs.addArb('172.24.32.201:27019')
{ "ok" : 1 }
查看副本集拓撲結(jié)構(gòu)
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
}
驗證數(shù)據(jù)可用性夫凸,插入一條數(shù)據(jù)坠韩,并且查看所有數(shù)據(jù)
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節(jié)點查看數(shù)據(jù)
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"
}
數(shù)據(jù)一致