极乐门资源网 Design By www.ioogu.com

前言

近期有同学问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副本集原理,mongo副本集搭建,mongodb副本集

极乐门资源网 Design By www.ioogu.com
极乐门资源网 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com

评论“mongodb 4.0副本集搭建的全过程”

暂无mongodb 4.0副本集搭建的全过程的评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。