一、Replica Set 集群分为两种架构:
奇数个节点构成Replica Set,所有节点拥有数据集。最小架构: 1个Primary节点,2个Secondary节点
偶数个节点 + 一个仲裁节点 构成的Replica Set,节点拥有数据集,仲裁节点仅参与仲裁选举出Primary节点。 最小架构:1个Primary节点,1个Secondary节点,1个Arbiter节点
接下来就以3台服务器为例,部署具有仲裁的框架。
IP地址 操作系统版本 MongoDB版本 端口 功能10.10.18.10 CentOS7.5 4.0 27017 Primary
10.10.18.11 Centos7.5 4.0 27017 Secondary
10.10.18.12 Centos7.5 4.0 27017 Arbiter
二、MongoDB安装部署
安装环境: CentOS 7.5
安装基础依赖包
yum install cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs libcurl libpcap lm_sensors-libs net-snmp net-snmp-agent-libs openldap openssl rpm-libs tcp_wrappers-libs
下载安装包
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.10.tgz
解析安装包
tar -xzvf mongodb-linux-x86_64-4.0.10.tgz -C /data/
ln -s /data/mongodb-linux-x86_64-4.0.10 /data/mongodb
添加环境变量
echo "export PATH=\$PATH:/data/mongodb/bin" >> ~/.bashrc
source ~/.bashrc
mongo --version
Primary的配置文件:
systemLog:
destination: file
path: "/data/mongodb/log/mongod.log"
logAppend: true
storage:
dbPath: "/data/mongodb/data"
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
processManagement:
fork: true
pidFilePath: "/data/mongodb/pid/m.pid"
net:
bindIp: 10.10.18.10
port: 27017
replication:
replSetName: "rs0"
Secondary的配置文件
systemLog:
destination: file
path: "/data/mongodb/log/mongod.log"
logAppend: true
storage:
dbPath: "/data/mongodb/data"
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
processManagement:
fork: true
pidFilePath: "/data/mongodb/pid/m.pid"
net:
bindIp: 10.10.18.11
port: 27017
replication:
replSetName: "rs0"
Arbiter的配置文件
systemLog:
destination: file
path: "/data/mongodb/log/mongod.log"
logAppend: true
storage:
dbPath: "/data/mongodb/data"
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
processManagement:
fork: true
pidFilePath: "/data/mongodb/pid/m.pid"
net:
bindIp: 10.10.18.12
port: 27017
replication:
replSetName: "rs0"
启动三台服务器上的mongdb
mongod -f /data/mongodb/mongod.conf
三、配置Replica Set
登录三台服务器中任意一台,登录mongo
mongo --host 10.10.18.10
>cfg={ _id:"rs0",members:[{_id:0,host:'10.10.18.10:27017',priority:1},{_id:1,host:'10.10.18.11:27017',priority:1},{_id:2,host:'10.10.18.12:27017',arbiterOnly:true}] };
>rs.initiate(cfg)
查看Replica Set配置
> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "10.10.18.10:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "10.10.18.11:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "10.10.18.12:27017",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {