mongodb 4.0副本集搭建

时间:2022-05-29 00:37:01

近期有同学问mongodb副本集难不难部署,我的回答是不难,很快,几分钟搞定,比mysql MHA简单的不止一点半点。 那么到底如何部署呢?请看下文。

1.  准备工作

1.1 下载软件

选择版本并下载mongodb的软件,注意操作系统版本等。本次我选用的是percona分支的mongodb 4.2.8版本搭建,操作系统为centos6

cd /usr/local/
wget https://www.percona.com/downloads/percona-server-mongodb-LATEST/percona-server-mongodb-4.2.8-8/binary/tarball/percona-server-mongodb-4.2.8-8-centos6-x86_64.tar.gz
tar -zxvf  percona-server-mongodb-4.2.8-8-centos6-x86_64.tar.gz
ln -s percona-server-mongodb-4.2.8-8 mongodb

1.2  规划各节点角色

各节点角色如下

IP  port  role
192.168.128.208 27017 PRIMARY
192.168.128.209 27017 SECONDARY
192.168.128.209 28017 ARBITER

2  部署

2.1  创建各节点相关目录

创建数据目录、日志目录等相关目录

[root@m1 ~]# mkdir -p /data/mongodb//{data,logs,tmp,etc,keyfile}
[root@m1 ~]# cd /data/mongodb//
[root@m1 ]# ll
总用量
drwxr-xr-x root root 8月 : data
drwxr-xr-x root root 8月 : etc
drwxr-xr-x root root 8月 : keyfile
drwxr-xr-x root root 8月 : logs
drwxr-xr-x root root 8月 : tmp

2.2  配置文件

创建配置文件,并添加对应的配置信息,注意仲裁节点的端口

[root@m1 ]# cd  /data/mongodb//etc/
[root@m1 etc]# vim mongod.conf
添加如下内容 storage:
dbPath: /data/mongodb//data
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /data/mongodb//logs/mongod.log
processManagement:
fork: true
pidFilePath: /data/mongodb//tmp/mongod.pid
net:
port:
bindIp: 0.0.0.0
replication:
  replSetName: "test1" # 副本集名称

2.3   启动各节点

以其中一个节点为例,其他节点修改对应配置文件即可

[root@m1 local]#  /usr/local/mongodb/bin/mongod   -f /data/mongodb//etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process:
child process started successfully, parent exiting

2.4  初始化副本集

初始化副本集,指定各节点id及角色,注意副本集的名称和配置文件里一致。

在任意节点执行如下语句

> use admin
switched to db admin
> rs.initiate( {
... _id: "test1",
... members: [
... { _id: , host: "192.168.128.208:27017" },
... { _id: , host: "192.168.128.209:27017" },
... { _id: , host: "192.168.128.209:28017",arbiterOnly:true }
... ] })
{
"ok" : ,
"$clusterTime" : {
"clusterTime" : Timestamp(, ),
"signature" : {
"hash" : BinData(,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong()
}
},
"operationTime" : Timestamp(, )
}

完成后可以查看各集群状态,如下,可见当前192.168.128.208:27017 为PRIMARY节点

test1:PRIMARY> rs.status()
{
"set" : "test1",
"date" : ISODate("2020-08-07T11:09:24.454Z"),
"myState" : ,
"term" : NumberLong(),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -,
"heartbeatIntervalMillis" : NumberLong(),
"majorityVoteCount" : ,
"writeMajorityCount" : ,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"lastCommittedWallTime" : ISODate("2020-08-07T11:09:23.663Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"readConcernMajorityWallTime" : ISODate("2020-08-07T11:09:23.663Z"),
"appliedOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"durableOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"lastAppliedWallTime" : ISODate("2020-08-07T11:09:23.663Z"),
"lastDurableWallTime" : ISODate("2020-08-07T11:09:23.663Z")
},
"lastStableRecoveryTimestamp" : Timestamp(, ),
"lastStableCheckpointTimestamp" : Timestamp(, ),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-08-07T09:31:33.409Z"),
"electionTerm" : NumberLong(),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(, ),
"t" : NumberLong(-)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(, ),
"t" : NumberLong(-)
},
"numVotesNeeded" : ,
"priorityAtElection" : ,
"electionTimeoutMillis" : NumberLong(),
"numCatchUpOps" : NumberLong(),
"newTermStartDate" : ISODate("2020-08-07T09:31:33.444Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-08-07T09:31:34.050Z")
},
"members" : [
{
"_id" : ,
"name" : "192.168.128.208:27017",
"health" : ,
"state" : ,
"stateStr" : "PRIMARY",
"uptime" : ,
"optime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"optimeDate" : ISODate("2020-08-07T11:09:23Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -,
"infoMessage" : "",
"electionTime" : Timestamp(, ),
"electionDate" : ISODate("2020-08-07T09:31:33Z"),
"configVersion" : ,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : ,
"name" : "192.168.128.209:27017",
"health" : ,
"state" : ,
"stateStr" : "SECONDARY",
"uptime" : ,
"optime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"optimeDurable" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"optimeDate" : ISODate("2020-08-07T11:09:23Z"),
"optimeDurableDate" : ISODate("2020-08-07T11:09:23Z"),
"lastHeartbeat" : ISODate("2020-08-07T11:09:23.672Z"),
"lastHeartbeatRecv" : ISODate("2020-08-07T11:09:22.804Z"),
"pingMs" : NumberLong(),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.128.208:27017",
"syncSourceHost" : "192.168.128.208:27017",
"syncSourceId" : ,
"infoMessage" : "",
"configVersion" :
},
{
"_id" : ,
"name" : "192.168.128.209:28017",
"health" : ,
"state" : ,
"stateStr" : "ARBITER",
"uptime" : ,
"lastHeartbeat" : ISODate("2020-08-07T11:09:23.672Z"),
"lastHeartbeatRecv" : ISODate("2020-08-07T11:09:22.952Z"),
"pingMs" : NumberLong(),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -,
"infoMessage" : "",
"configVersion" :
}
],
"ok" : ,
"$clusterTime" : {
"clusterTime" : Timestamp(, ),
"signature" : {
"hash" : BinData(,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong()
}
},
"operationTime" : Timestamp(, )
}
test1:PRIMARY>

3  修改为认证模式

为考虑安全性等,需要开启认证模式(需要通过用户名/密码方式登录)

3.1 创建用户

创建超级管理员账号

test1:PRIMARY> db.createUser({user: "root", pwd: "Root#123", roles: [{role: "root", db: "admin"}]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}

3.2  创建key文件

在主库上执行脚本生成key文件,然后将结拷贝到另外2个节点

[root@m1 mongodb]# cd /data/mongodb//keyfile/
[root@m1 keyfile]# openssl rand -base64 > mongo.key
[root@m1 keyfile]# chmod mongo.key # 必须修改为600权限,否则无法启动

以上完成后,将文件复制到另外2个节点

3.3  修改配置文件

配置文件中 添加如下内容,注意不同节点的文件路径
security:
authorization: enabled
clusterAuthMode: keyFile
keyFile: /data/mongodb//keyfile/mongo.key

3.4  重启后并用认证模式登陆

# 关闭mongodb
[root@m1 keyfile]# /usr/local/mongodb/bin/mongod -f /data/mongodb//etc/mongod.conf -shutdown
killing process with pid:
# 启动mongodb
[root@m1 keyfile]# /usr/local/mongodb/bin/mongod -f /data/mongodb//etc/mongod.conf about to fork child process, waiting until server is ready for connections.
forked process: child process started successfully, parent exiting # 认证模式登陆
[root@m1 keyfile]#
[root@m1 keyfile]# /usr/local/mongodb/bin/mongo -u root -p 'Root#123' --authenticationDatabase admin

至此,mongodb 副本集搭建完毕,你会了吗?

想了解更多内容或参与技术交流可以关注微信公众号【数据库干货铺】或进技术交流群沟通。

mongodb 4.0副本集搭建