mongodb 4.0副本集搭建的全过程
程序员文章站
2022-04-05 15:10:21
前言近期有同学问mongodb副本集难不难部署,我的回答是不难,很快,几分钟搞定,比mysql mha简单的不止一点半点。 那么到底如何部署呢?请看下文。1. 准备工作1.1 下载软件选择版本并下载m...
前言
近期有同学问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/27017/{data,logs,tmp,etc,keyfile} [root@m1 ~]# cd /data/mongodb/27017/ [root@m1 27017]# ll 总用量 20 drwxr-xr-x 2 root root 4096 8月 7 16:59 data drwxr-xr-x 2 root root 4096 8月 7 16:59 etc drwxr-xr-x 2 root root 4096 8月 7 16:59 keyfile drwxr-xr-x 2 root root 4096 8月 7 16:59 logs drwxr-xr-x 2 root root 4096 8月 7 16:59 tmp
2.2 配置文件
创建配置文件,并添加对应的配置信息,注意仲裁节点的端口
[root@m1 27017]# cd /data/mongodb/27017/etc/ [root@m1 etc]# vim mongod.conf 添加如下内容 storage: dbpath: /data/mongodb/27017/data journal: enabled: true systemlog: destination: file logappend: true path: /data/mongodb/27017/logs/mongod.log processmanagement: fork: true pidfilepath: /data/mongodb/27017/tmp/mongod.pid net: port: 27017 bindip: 0.0.0.0replication: replsetname: "test1" # 副本集名称
2.3 启动各节点
以其中一个节点为例,其他节点修改对应配置文件即可
[root@m1 local]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/etc/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 30019 child process started successfully, parent exiting
2.4 初始化副本集
初始化副本集,指定各节点id及角色,注意副本集的名称和配置文件里一致。
在任意节点执行如下语句
> use admin switched to db admin > rs.initiate( { ... _id: "test1", ... members: [ ... { _id: 0, host: "192.168.128.208:27017" }, ... { _id: 1, host: "192.168.128.209:27017" }, ... { _id: 2, host: "192.168.128.209:28017",arbiteronly:true } ... ] }) { "ok" : 1, "$clustertime" : { "clustertime" : timestamp(1596792682, 1), "signature" : { "hash" : bindata(0,"aaaaaaaaaaaaaaaaaaaaaaaaaaa="), "keyid" : numberlong(0) } }, "operationtime" : timestamp(1596792682, 1) }
完成后可以查看各集群状态,如下,可见当前192.168.128.208:27017 为primary节点
test1:primary> rs.status() { "set" : "test1", "date" : isodate("2020-08-07t11:09:24.454z"), "mystate" : 1, "term" : numberlong(1), "syncingto" : "", "syncsourcehost" : "", "syncsourceid" : -1, "heartbeatintervalmillis" : numberlong(2000), "majorityvotecount" : 2, "writemajoritycount" : 2, "optimes" : { "lastcommittedoptime" : { "ts" : timestamp(1596798563, 1), "t" : numberlong(1) }, "lastcommittedwalltime" : isodate("2020-08-07t11:09:23.663z"), "readconcernmajorityoptime" : { "ts" : timestamp(1596798563, 1), "t" : numberlong(1) }, "readconcernmajoritywalltime" : isodate("2020-08-07t11:09:23.663z"), "appliedoptime" : { "ts" : timestamp(1596798563, 1), "t" : numberlong(1) }, "durableoptime" : { "ts" : timestamp(1596798563, 1), "t" : numberlong(1) }, "lastappliedwalltime" : isodate("2020-08-07t11:09:23.663z"), "lastdurablewalltime" : isodate("2020-08-07t11:09:23.663z") }, "laststablerecoverytimestamp" : timestamp(1596798513, 1), "laststablecheckpointtimestamp" : timestamp(1596798513, 1), "electioncandidatemetrics" : { "lastelectionreason" : "electiontimeout", "lastelectiondate" : isodate("2020-08-07t09:31:33.409z"), "electionterm" : numberlong(1), "lastcommittedoptimeatelection" : { "ts" : timestamp(0, 0), "t" : numberlong(-1) }, "lastseenoptimeatelection" : { "ts" : timestamp(1596792682, 1), "t" : numberlong(-1) }, "numvotesneeded" : 2, "priorityatelection" : 1, "electiontimeoutmillis" : numberlong(10000), "numcatchupops" : numberlong(0), "newtermstartdate" : isodate("2020-08-07t09:31:33.444z"), "wmajoritywriteavailabilitydate" : isodate("2020-08-07t09:31:34.050z") }, "members" : [ { "_id" : 0, "name" : "192.168.128.208:27017", "health" : 1, "state" : 1, "statestr" : "primary", "uptime" : 5950, "optime" : { "ts" : timestamp(1596798563, 1), "t" : numberlong(1) }, "optimedate" : isodate("2020-08-07t11:09:23z"), "syncingto" : "", "syncsourcehost" : "", "syncsourceid" : -1, "infomessage" : "", "electiontime" : timestamp(1596792693, 1), "electiondate" : isodate("2020-08-07t09:31:33z"), "configversion" : 1, "self" : true, "lastheartbeatmessage" : "" }, { "_id" : 1, "name" : "192.168.128.209:27017", "health" : 1, "state" : 2, "statestr" : "secondary", "uptime" : 5882, "optime" : { "ts" : timestamp(1596798563, 1), "t" : numberlong(1) }, "optimedurable" : { "ts" : timestamp(1596798563, 1), "t" : numberlong(1) }, "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(0), "lastheartbeatmessage" : "", "syncingto" : "192.168.128.208:27017", "syncsourcehost" : "192.168.128.208:27017", "syncsourceid" : 0, "infomessage" : "", "configversion" : 1 }, { "_id" : 2, "name" : "192.168.128.209:28017", "health" : 1, "state" : 7, "statestr" : "arbiter", "uptime" : 5882, "lastheartbeat" : isodate("2020-08-07t11:09:23.672z"), "lastheartbeatrecv" : isodate("2020-08-07t11:09:22.952z"), "pingms" : numberlong(0), "lastheartbeatmessage" : "", "syncingto" : "", "syncsourcehost" : "", "syncsourceid" : -1, "infomessage" : "", "configversion" : 1 } ], "ok" : 1, "$clustertime" : { "clustertime" : timestamp(1596798563, 1), "signature" : { "hash" : bindata(0,"aaaaaaaaaaaaaaaaaaaaaaaaaaa="), "keyid" : numberlong(0) } }, "operationtime" : timestamp(1596798563, 1) } 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/27017/keyfile/ [root@m1 keyfile]# openssl rand -base64 756 > mongo.key [root@m1 keyfile]# chmod 600 mongo.key # 必须修改为600权限,否则无法启动
以上完成后,将文件复制到另外2个节点
3.3 修改配置文件
配置文件中 添加如下内容,注意不同节点的文件路径
security: authorization: enabled clusterauthmode: keyfile keyfile: /data/mongodb/27017/keyfile/mongo.key
3.4 重启后并用认证模式登陆
# 关闭mongodb [root@m1 keyfile]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/etc/mongod.conf -shutdown killing process with pid: 30675 # 启动mongodb [root@m1 keyfile]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/etc/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 2430 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副本集搭建的文章就介绍到这了,更多相关mongodb4.0副本集搭建内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
详解CentOS的SVN服务器搭建与自动部署全过程
-
以实现MongoDB副本集状态的监控为例,看Telegraf系统中Exec输入插件如何编写部署
-
MongoDB搭建高可用集群的完整步骤(3个分片+3个副本)
-
聊聊MongoDB 带访问控制的副本集部署问题
-
聊聊MongoDB 带访问控制的副本集部署问题
-
MongoDB副本集及C#程序的连接配置
-
Docker下搭建mongodb副本集
-
基于IDEA搭建SpringMVC+Spring+Mybatis的全过程,并深入了解整个运行流程和项目配置
-
详解MongoDB中用sharding将副本集分配至服务器集群的方法
-
MongoDB的主从复制及副本集的replSet配置教程