mongo副本集配置部署
副本集:
1主2从2仲裁
mongo安装包:http://www.mongodb.org/downloads
机器:
采用5台机器,host0,host1,host2,host3,host4
角色分布:
1主:host0:8111
2从:host1:8111,host2:8111
2仲裁:host3:10000,host3:10000
replica set部署开始
1.准备
将mongo安装包解压至家目录~,改名为mongo
mkdir -p ~/mongo/data/mongo/
mkdir -p ~/mongo/log/
mkdir -p ~/mongo/conf/
2.配置
vi ~/mongo/conf/mongod.conf
#log logpath = ./log/mongod.log logappend = true #listen port port = 8111 #db path dbpath = ./data/mongo/ #independent dir per db directoryperdb = true #namespace disk size nssize = 500 #max connection numbers maxConns = 2048 #replica set name replSet = shard0 #relication log size oplogSize = 10240 #monitor rest = true
echo "mongoSecretPassword" > ~/mongo/conf/secretKey
3.启动
nohup numactl --interleave=all ./bin/mongod -f ./conf/mongod.conf --fastsync --keyFile=./conf/secretKey >/dev/null 2>&1 &
注:
1.不同的分片中,replSet配置不同
2.numactl --interleave=all,numa:Non-Uniform Memory Access,内存交织分配,必须,否则导致频繁的内存交换,影响mongo性能
3.--keyFile=./conf/secretKey,集群认证的私钥,secretKey权限不要太开放,chmod 400 ~/mongo/conf/secretKey
4.--fastsync,使用已有的基准数据,而非完全重新同步数据
4.检查
检查mongo进程,检查mongo日志~/mongo/log/mongod.log
此时mongod.log会提示:[rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
这是由于副本集无法获取配置导致,完成下一步即可解决
5.设置replica set
随意进入一台mongod机器
cd ~/mongo ./bin/mongo 127.0.0.1:8111 connecting to: 127.0.0.1:8111/test > config = {_id:'shard0',members:[{_id:0,host:'host0IP:8111'},{_id:1,host:'host1IP:8111'},{_id:2,host:'host2IP:8111'}]} > rs.initiate(config)
6.设置secondary可读
登录secondary机器
cd ~/mongo/ ./bin/mongo 127.0.0.1:8111 >rs.slaveOk()
7.检查分片状态
分别到各台机器检查
> rs.conf() > rs.status() > rs.isMaster()
8.用户名密码设定
到primary机器(rs.status可以查找到),进行如下操作
> use admin > db.addUser("username","password")
9.arbiter:仲裁者的构建
9.1准备
arbiter与mongod同进程,不同是arbiter无数据集,在选举时进行投票
将mongo安装包解压至家目录~,改名为arbiter
cd ~/arbiter/bin/ && cp mongod arbiter-10000 //arbiter耗费资源较少,集群分片的arbiter可全量搭建在两台机器,以arbiter-port命名
mkdir -p ~/arbiter/conf/
mkdir -p ~/arbiter/log/
mkdir -p ~/arbiter/data/arbiter-10000/
9.2配置
vi ~/arbiter/conf/arbiter-10000.conf //统一命名规范,便于问题定位
#log logpath = ./log/arbiter-10000.log logappend = true #listen port = 10000 port = 10000 #db path dbpath = ./data/arbiter-10000/ #max connection numbers maxConns = 2048 #replica set name replSet = shard0 #relication log size oplogSize = 1024 #monitor rest = true # disable http nohttpinterface = true
9.3启动
类似副本集中mongod的启动命令
arbiter启动:
nohup numactl --interleave=all ./bin/arbiter-10000 -f ./conf/arbiter-10000.conf --keyFile=./conf/secretKey >/dev/null 2>&1 &
8.4将arbiter加入分片
primary节点执行:
./bin/mongo 127.0.0.1:8111/admin -u username -p password
shard0:PRIMARY> rs.addArb("host3IP:10000") shard0:PRIMARY> rs.addArb("host4IP:10000")
8.5查询arbiter状态
分片节点执行rs.status()查询arbiter状态
至此,1主2从2arbiter的分片构造完毕。