数据库运维
记录DBA学习成长历程

MongoDB复制集RS(ReplicationSet)

默认一主两从,无损,数据一致性,主从无延时

基本原理

基本构成是1主2从的结构,自带互相监控投票机制(Raft(MongoDB) Paxos(mysql MGR 用的是变种))
如果发生主库宕机,复制集内部会进行投票选举,选择一个新的主库替代原有主库对外提供服务。同时复制集会自动通知
客户端程序,主库已经发生切换了。应用就会连接到新的主库。

配置过程

规划

三个以上的mongodb节点(或多实例)

环境准备

多个端口
28017、28018、28019、28020
多套目录
su - mongod 
mkdir -p /mongodb/28017/conf /mongodb/28017/data /mongodb/28017/log
mkdir -p /mongodb/28018/conf /mongodb/28018/data /mongodb/28018/log
mkdir -p /mongodb/28019/conf /mongodb/28019/data /mongodb/28019/log
mkdir -p /mongodb/28020/conf /mongodb/28020/data /mongodb/28020/log
多套配置文件
/mongodb/28017/conf/mongod.conf
/mongodb/28018/conf/mongod.conf
/mongodb/28019/conf/mongod.conf
/mongodb/28020/conf/mongod.conf
配置文件内容
cat > /mongodb/28017/conf/mongod.conf <<EOF
systemLog:
destination: file
path: /mongodb/28017/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/28017/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
net:
bindIp: 10.0.0.200,127.0.0.1
port: 28017
replication:
oplogSizeMB: 2048
replSetName: my_repl
EOF
\cp /mongodb/28017/conf/mongod.conf /mongodb/28018/conf/
\cp /mongodb/28017/conf/mongod.conf /mongodb/28019/conf/
\cp /mongodb/28017/conf/mongod.conf /mongodb/28020/conf/
sed 's#28017#28018#g' /mongodb/28018/conf/mongod.conf -i
sed 's#28017#28019#g' /mongodb/28019/conf/mongod.conf -i
sed 's#28017#28020#g' /mongodb/28020/conf/mongod.conf -i
启动多个实例备用
mongod -f /mongodb/28017/conf/mongod.conf
mongod -f /mongodb/28018/conf/mongod.conf
mongod -f /mongodb/28019/conf/mongod.conf
mongod -f /mongodb/28020/conf/mongod.conf
netstat -lnp|grep 280

配置普通复制集

1主2从,从库普通从库
mongo --port 28017 admin
config = {_id: 'my_repl', members: [
                {_id: 0, host: '10.0.0.200:28017'},
                {_id: 1, host: '10.0.0.200:28018'},
                {_id: 2, host: '10.0.0.200:28019'}]
        } 
rs.initiate(config) 
查询复制集状态
rs.status();
1主1从1个arbiter
mongo -port 28017 admin
config = {_id: 'my_repl', members: [
                {_id: 0, host: '10.0.0.51:28017'},
                {_id: 1, host: '10.0.0.51:28018'},
                {_id: 2, host: '10.0.0.51:28019',"arbiterOnly":true}]
       } 
rs.initiate(config)
赞(1)
MySQL学习笔记 » MongoDB复制集RS(ReplicationSet)