mongodb副本集配置部署
mongodb副本集部署和配置
版本:mongodb4.4.2
mongodb单机副本集部署步骤如下:
[root@localhost software]# pwd
/data/software
[root@localhost software]# ll
total 87720
-rw-r--r--. 1 root root 16137908 Jan 3 21:58 gcc-9.3.0.tar.gz
-rw-r--r--. 1 root root 71386000 Nov 16 11:21 mongodb-linux-x86_64-rhel70-4.4.2.tgz
-rw-r--r--. 1 root root 25548 Apr 7 2017 mysql57-community-release-el7-10.noarch.rpm
drwxrwxr-x. 7 root root 4096 Jan 3 22:30 redis-6.0.9
-rw-r--r--. 1 root root 2261418 Oct 27 03:14 redis-6.0.9.tar.gz
解压mongodb且进入mongodb目录,创建27011,27012,27013端口对应的储存数据文件夹:
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.2.tgz
mv mongodb-linux-x86_64-rhel70-4.4.2 mongodb-4.4.2
cd mongodb-4.4.2/
mkdir -p /data/software/mongodb-4.4.2/27011/logs/
mkdir -p /data/software/mongodb-4.4.2/27011/data/
mkdir -p /data/software/mongodb-4.4.2/27011/key/
mkdir -p /data/software/mongodb-4.4.2/27011/pid/
mkdir -p /data/software/mongodb-4.4.2/27012/logs/
mkdir -p /data/software/mongodb-4.4.2/27012/data/
mkdir -p /data/software/mongodb-4.4.2/27012/key/
mkdir -p /data/software/mongodb-4.4.2/27012/pid/
mkdir -p /data/software/mongodb-4.4.2/27013/logs/
mkdir -p /data/software/mongodb-4.4.2/27013/data/
mkdir -p /data/software/mongodb-4.4.2/27013/key/
mkdir -p /data/software/mongodb-4.4.2/27013/pid/
创建和修改配置文件:
touch /data/software/mongodb-4.4.2/mongo-27011.conf
touch /data/software/mongodb-4.4.2/mongo-27012.conf
touch /data/software/mongodb-4.4.2/mongo-27013.conf
mongo-27011.conf配置:
[root@localhost mongodb-4.4.2]# cat mongo-27011.conf
systemLog:
destination: file
path: "/data/software/mongodb-4.4.2/27011/logs/mongodb.log"
logAppend: true
storage:
dbPath: "/data/software/mongodb-4.4.2/27011/data"
journal:
enabled: true
# mmapv1:
# smallFiles: true
wiredTiger:
engineConfig:
configString: cache_size=500m
processManagement:
fork: true
net:
port: 27011
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: false
# 开启授权认证
security:
authorization: disabled
replication:
oplogSizeMB: 100
replSetName: sszh
enableMajorityReadConcern: true
mongo-27012.conf配置:
[root@localhost mongodb-4.4.2]# cat mongo-27012.conf
systemLog:
destination: file
path: "/data/software/mongodb-4.4.2/27012/logs/mongodb.log"
logAppend: true
storage:
dbPath: "/data/software/mongodb-4.4.2/27012/data"
journal:
enabled: true
# mmapv1:
# smallFiles: true
wiredTiger:
engineConfig:
configString: cache_size=500m
processManagement:
fork: true
net:
port: 27012
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: false
# 开启授权认证
security:
authorization: disabled
replication:
oplogSizeMB: 100
replSetName: sszh
enableMajorityReadConcern: true
mongo-27013.conf 配置:
[root@localhost mongodb-4.4.2]# cat mongo-27013.conf
systemLog:
destination: file
path: "/data/software/mongodb-4.4.2/27013/logs/mongodb.log"
logAppend: true
storage:
dbPath: "/data/software/mongodb-4.4.2/27013/data"
journal:
enabled: true
# mmapv1:
# smallFiles: true
wiredTiger:
engineConfig:
configString: cache_size=500m
processManagement:
fork: true
net:
port: 27013
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: false
# 开启授权认证
security:
authorization: disabled
replication:
oplogSizeMB: 100
replSetName: sszh
enableMajorityReadConcern: true
创建和修改启动服务shell脚本:
创建start-27011,start-27012,start-27013 shell启动脚本:
touch /data/software/mongodb-4.4.2/start-27011
[root@localhost mongodb-4.4.2]# more start-27011
/data/software/mongodb-4.4.2/bin/mongod --config /data/software/mongodb-4.4.2/mongo-27011.conf
touch /data/software/mongodb-4.4.2/start-27012
[root@localhost mongodb-4.4.2]# more start-27012
/data/software/mongodb-4.4.2/bin/mongod --config /data/software/mongodb-4.4.2/mongo-27012.conf
touch /data/software/mongodb-4.4.2/start-27013
[root@localhost mongodb-4.4.2]# more start-27013
/data/software/mongodb-4.4.2/bin/mongod --config /data/software/mongodb-4.4.2/mongo-27013.conf
创建和修改停止服务shell脚本:
touch /data/software/mongodb-4.4.2/stop-27011
[root@localhost mongodb-4.4.2]# more stop-27011
ps -ef|grep /data/software/mongodb-4.4.2/mongo-27011.conf|grep -v grep|awk '{printf $2}'|xargs kill -9
touch /data/software/mongodb-4.4.2/stop-27012
[root@localhost mongodb-4.4.2]# more stop-27012
ps -ef|grep /data/software/mongodb-4.4.2/mongo-27012.conf|grep -v grep|awk '{printf $2}'|xargs kill -9
touch /data/software/mongodb-4.4.2/stop-27013
[root@localhost mongodb-4.4.2]# more stop-27013
ps -ef|grep /data/software/mongodb-4.4.2/mongo-27013.conf|grep -v grep|awk '{printf $2}'|xargs kill -9
授权限:
给/data/software/mongodb-4.4.2文件夹和全部文件添加权限(生产环境需要酌情考虑是否开放外部权限):
chmod -R 777 /data/software/mongodb-4.4.2/
启动mongodb:
[root@localhost mongodb-4.4.2]# pwd
/data/software/mongodb-4.4.2
[root@localhost mongodb-4.4.2]# sh start-27011
about to fork child process, waiting until server is ready for connections.
forked process: 17307
child process started successfully, parent exiting
[root@localhost mongodb-4.4.2]# echo $?
0
[root@localhost mongodb-4.4.2]# sh start-27012
{"t":{"$date":"2021-01-04T05:39:15.648Z"},"s":"I", "c":"STORAGE", "id":22293, "ctx":"main","msg":"Engine custom option","attr":{"option":"cache_size=500m"}}
about to fork child process, waiting until server is ready for connections.
forked process: 17354
child process started successfully, parent exiting
[root@localhost mongodb-4.4.2]# sh start-27013
about to fork child process, waiting until server is ready for connections.
forked process: 17402
child process started successfully, parent exiting
查看mongodb是否启动成功:出现如下所示,说明启动成功。
[root@localhost mongodb-4.4.2]# ps -ef|grep mongo
root 17307 1 0 00:38 ? 00:00:01 /data/software/mongodb-4.4.2/bin/mongod --config /data/software/mongodb-4.4.2/mongo-27011.conf
root 17354 1 0 00:39 ? 00:00:01 /data/software/mongodb-4.4.2/bin/mongod --config /data/software/mongodb-4.4.2/mongo-27012.conf
root 17402 1 0 00:39 ? 00:00:01 /data/software/mongodb-4.4.2/bin/mongod --config /data/software/mongodb-4.4.2/mongo-27013.conf
root 17453 16723 0 00:42 pts/3 00:00:00 grep --color=auto mongo
添加集群节点:
登录mongodb数据库:192.168.42.60:27011,具体登录命令如下:
[root@localhost bin]# pwd
/data/software/mongodb-4.4.2/bin
[root@localhost bin]# ./mongo --port=27012
MongoDB shell version v4.4.2
connecting to: mongodb://127.0.0.1:27012/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("00efb4f8-29e5-4595-b80e-abef7d619eb0") }
MongoDB server version: 4.4.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2021-01-04T00:39:15.934-05:00: You are running this process as the root user, which is not recommended
2021-01-04T00:39:15.934-05:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2021-01-04T00:39:15.934-05:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
2021-01-04T00:39:15.934-05:00: Soft rlimits too low
2021-01-04T00:39:15.934-05:00: currentValue: 1024
2021-01-04T00:39:15.934-05:00: recommendedMinimum: 64000
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
登入后在 mongodb 命令行模式,执行 ’ > ’ 后面的命令:
> use admin
switched to db admin
> config={_id:"sszh", members:[{_id:0,host:'192.168.42.60:27011'}, {_id:1,host:'192.168.42.60:27012'}, {_id:2,host:'192.168.42.60:27013'}]};
{
"_id" : "sszh",
"members" : [
{
"_id" : 0,
"host" : "192.168.42.60:27011"
},
{
"_id" : 1,
"host" : "192.168.42.60:27012"
},
{
"_id" : 2,
"host" : "192.168.42.60:27013"
}
]
}
> rs.initiate(config)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1609739977, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1609739977, 1)
}
sszh:SECONDARY>
sszh:PRIMARY> rs.status()
{
"set" : "sszh",
"date" : ISODate("2021-01-04T06:01:49.682Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1609740108, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-01-04T06:01:48.093Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1609740108, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-01-04T06:01:48.093Z"),
"appliedOpTime" : {
"ts" : Timestamp(1609740108, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1609740108, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-01-04T06:01:48.093Z"),
"lastDurableWallTime" : ISODate("2021-01-04T06:01:48.093Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1609740108, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-01-04T05:59:48.028Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1609739977, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-01-04T05:59:48.053Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-01-04T05:59:48.994Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.42.60:27011",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 132,
"optime" : {
"ts" : Timestamp(1609740108, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1609740108, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-01-04T06:01:48Z"),
"optimeDurableDate" : ISODate("2021-01-04T06:01:48Z"),
"lastHeartbeat" : ISODate("2021-01-04T06:01:48.159Z"),
"lastHeartbeatRecv" : ISODate("2021-01-04T06:01:49.126Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "192.168.42.60:27012",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
},
{
"_id" : 1,
"name" : "192.168.42.60:27012",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1354,
"optime" : {
"ts" : Timestamp(1609740108, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-01-04T06:01:48Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1609739988, 1),
"electionDate" : ISODate("2021-01-04T05:59:48Z"),
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.42.60:27013",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 132,
"optime" : {
"ts" : Timestamp(1609740108, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1609740108, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-01-04T06:01:48Z"),
"optimeDurableDate" : ISODate("2021-01-04T06:01:48Z"),
"lastHeartbeat" : ISODate("2021-01-04T06:01:48.159Z"),
"lastHeartbeatRecv" : ISODate("2021-01-04T06:01:49.126Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "192.168.42.60:27012",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1609740108, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1609740108, 1)
}
sszh:PRIMARY>
备注:
use admin:使用admin数据库
config={}:临时参数
rs.initiate(config):使用config临时参数初始化副本集
rs.status:查看集群状态
members[0].stateStr:PRIMARY 表示主节点,SECONDARY表示从节点
PRIMARY主节点:可读可写
SECONDARY从节点:可读不可写