下载MongoDB
MongoDB的官网是:http://www.mongodb.org/
MongoDB最新版本下载路径:https://www.mongodb.com/download-center
此次安装选择的版本是:mongodb-linux-x86_64-3.2.8.tgz
MongoDB安装
$ tar zxf mongodb-linux-x86_64-3.2.8.tgz -C /opt/modules/
$ ln -s mongodb-linux-x86_64-3.2.8/ mongodb
复制集节点架构
创建复制集
采用一台主机中实例四个节点arbiter, passive, primary, secondary(表:配置复制集):
启动服务器成员
$ cd /opt/modules/mongodb/
$ mkdir -p /opt/datas/mongodb/replication/passive/data
$ mkdir -p /opt/datas/mongodb/replication/passive/logs
$ touch /opt/datas/mongodb/replication/passive/logs/passive.log
$ mkdir -p /opt/datas/mongodb/replication/primary/data
$ mkdir -p /opt/datas/mongodb/replication/primary/logs
$ touch /opt/datas/mongodb/replication/primary/logs/primary.log
$ mkdir -p /opt/datas/mongodb/replication/secondary/data
$ mkdir -p /opt/datas/mongodb/replication/secondary/logs
$ touch /opt/datas/mongodb/replication/secondary/logs/secondary.log
$ bin/mongod --dbpath /opt/datas/mongodb/replication/primary/data --port 27021 --replSet iotset --fork --logpath /opt/datas/mongodb/replication/primary/logs/primary.log
$ bin/mongod --dbpath /opt/datas/mongodb/replication/secondary/data --port 27022 --replSet iotset --fork --logpath /opt/datas/mongodb/replication/secondary/logs/secondary.log
$ bin/mongod --dbpath /opt/datas/mongodb/replication/passive/data --port 27023 --replSet iotset --fork --logpath /opt/datas/mongodb/replication/passive/logs/passive.log
初始化集群
$ bin/mongo [hostname]:27021
> rs.initiate()
最后检查服务器状态
> rs.status()
向复制集添加服务器
$ bin/mongo [hostname]:27021
> rs.add("[hostname]:27021")
> rs.add("[hostname]:27022")
> rs.add("[hostname]:27023")
最后检查服务器状态
> rs.status()
接下来修改服务器配置
> conf = rs.conf()
> conf.members[2].hidden = true
> conf.members[2].priority = 0
执行存储配置文档命令查看配置文档
> conf
更新复制集的配置
> rs.reconfig(conf)
查看新的复制集配置是否生效
> rs.conf()
向复制集添加仲裁服务器
$ mkdir -p /opt/datas/mongodb/replication/arbiter/data
$ mkdir -p /opt/datas/mongodb/replication/arbiter/logs
$ touch /opt/datas/mongodb/replication/arbiter/logs/arbiter.log
$ bin/mongod --dbpath /opt/datas/mongodb/replication/arbiter/data --port 27024 --replSet iotset --fork --logpath /opt/datas/mongodb/replication/arbiter/logs/arbiter.log -rest
添加仲裁服务器到复制集
> rs.addArb("[hostname]:27024")
查看添加结果
> rs.status()
因复制集节点需要保持奇数,从投票成员中停止被动服务器投票功能。
> conf = rs.conf()
> conf.members[2].votes = 0
> rs.reconfig(conf)
复制集链
检测实例状态
> rs.status()
强制进行新的选举
> rs.stepDown()
判断某个成员是否为主服务器
> db.isMaster()
修改节点数据同步来源
$ bin/mongo [hostname]:27023
> db.adminCommand({replSetSyncFrom:"[hostname]:27022"})
参考资料
MongoDB大数据处理权威指南(第二版)