一、Replica Set 集群分为两种架构: 奇数个节点构成Replica Set,所有节点拥有数据集。最小架构: 1个Primary节点,2个Secondary节点 偶数个节点 + 一个仲裁节点 构成的Replica Set,节点拥有数据
一、Replica Set 集群分为两种架构:
奇数个节点构成Replica Set,所有节点拥有数据集。最小架构: 1个Primary节点,2个Secondary节点
偶数个节点 + 一个仲裁节点 构成的Replica Set,节点拥有数据集,仲裁节点仅参与仲裁选举出Primary节点。 最小架构:1个Primary节点,1个Secondary节点,1个Arbiter节点
接下来就以3台服务器为例,部署具有仲裁的框架。
二、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" >> ~/.bashrcsource ~/.bashrcmongo --version
Primary的配置文件:
systemLog: destination: file path: "/data/mongodb/log/mongod.log" logAppend: truestorage: dbPath: "/data/mongodb/data" journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 2processManagement: fork: true pidFilePath: "/data/mongodb/pid/m.pid"net: bindIp: 10.10.18.10 port: 27017replication: replSetName: "rs0"
Secondary的配置文件
systemLog: destination: file path: "/data/mongodb/log/mongod.log" logAppend: truestorage: dbPath: "/data/mongodb/data" journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 2processManagement: fork: true pidFilePath: "/data/mongodb/pid/m.pid"net: bindIp: 10.10.18.11 port: 27017replication: replSetName: "rs0"
Arbiter的配置文件
systemLog: destination: file path: "/data/mongodb/log/mongod.log" logAppend: truestorage: dbPath: "/data/mongodb/data" journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 2processManagement: fork: true pidFilePath: "/data/mongodb/pid/m.pid"net: bindIp: 10.10.18.12 port: 27017replication: 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" : {
}, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : {
}, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5cff76e5e57e23a5bc7054e2") }}
四、验证Replica Set
在Primary中插入数据
rs0:PRIMARY> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBrs0:PRIMARY> db.users.insertOne(... {... name:"sue",... age: 26,... status:"pending"... }){ "acknowledged" : true, "insertedId" : ObjectId("5cff79e8993e70290a081d04")}
rs0:PRIMARY> db.users.find(){ "_id" : ObjectId("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }
在Secondary中,默认是不允许读
rs0:SECONDARY> db.users.find()Error: error: { "operationTime" : Timestamp(1560247181, 1), "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk", "$clusterTime" : { "clusterTime" : Timestamp(1560247181, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }}
五、故障模拟
停止Primary上的mongo进程,在Secondary上登录mongo查看
mongo --host 10.10.18.11
rs0:PRIMARY> db.users.find(){ "_id" : ObjectId("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }
发现原Secondary变成Primary,并且可以进行查询。
开启原Primary服务器上的mongo进程,该服务器变成了Secondary。