一、安装前准备
机器分配: 三台机器 A :192.168.19.101 B: 192.168.19.102 C: 192.168.19.103
A机器:mongos 、config server、shard1主节点、shard2仲裁、shard3从节点
B机器:mongos 、config server、shard1从节点、shard2主节点、shard3仲裁
C机器:mongos 、config server、shard1仲裁、shard2从节点、shard3主节点
端口分配:mongos 20000、config 21000 shard1 27001 shard2 27002、shard3 27003
三台机器全部关闭firewalld服务和selinunx,或者增加相应端口规则
2.目录结构,使basedir 和 datadir 不在一个分区上
basedir: /usr/local/mongodb/
datadir: /data/mongodb/
3.禁用大内存页面
二、下载 解压 创建相关目录
# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.9.tgz
# tar zxvfmongodb-linux-x86_64-rhel70-4.0.9.tgz
# mv mongodb-linux-x86_64-rhel70-4.0.9 /usr/local/mongodb
# 创建相关目录
# mkdir -p /data/mongodb/mongos/log ;mkdir -p /data/mongodb/config/{data,log};mkdir -p /data/mongodb/shard1/{data,log};mkdir -p /data/mongodb/shard2/{data,log};mkdir -p /data/mongodb/shard3/{data,log}
# 设置环境变量
# cat /etc/profile.d/mongodb.sh
#!/bin/bash
MONGODB_HOME=/usr/local/mongodb/
PATH=$PATH:$MONGODB_HOME/bin
export MONGODB_HOME PATH
# . /etc/profile.d/mongodb.sh #使环境变量生效
三、分片集群搭建
1.config server的配置,三台机器都需要操作
# pwd
/usr/local/mongodb/conf
# mkdir /usr/local/mongodb/pids #存放服务的pid 目录
# cat config.conf # config 配置文件
pidfilepath = /usr/local/mongodb/pids/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/configsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
configsvr = true
replSet = configs
maxConns = 20000
#启动
# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/config.conf
#初始化副本集,任意一台机器都可以
# mongo --host 192.168.19.101 --port 21000
> config = {_id:"configs", members:[{_id:101, host:"192.168.19.101:21000"},{_id:102, host:"192.168.19.102:21000"},{_id:103, host:"192.168.19.103:21000"}]}
> rs.initiate(config)
configs:SECONDARY> rs.status() #等一会 当前机器 状态会变为 PRIMARY
configs:PRIMARY>
2.shard的配置,三台机器都需要操作
############### 以下是shard1的配置 ##############
# cat /usr/local/mongodb/conf/shard1.conf
pidfilepath = /usr/local/mongodb/pids/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
replSet = shard1
shardsvr = true
maxConns = 20000
# 启动,三个节点均需要启动
# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/shard1.conf
#连接mongo 配置shard1
# mongo --host 192.168.19.101 --port 27001
> config = {_id:"shard1",members:[{_id:101,host:"192.168.19.101:27001"},{_id:102,host:"192.168.19.102:27001"},{_id:103,host:"192.168.19.103:27001",arbiterOnly:true}]}
> rs.initiate(config)
shard1:SECONDARY> rs.status()
shard1:PRIMARY>
############### 以下是shard2的配置 ##############
# cat /usr/local/mongodb/conf/shard2.conf
pidfilepath = /usr/local/mongodb/pids/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
replSet = shard2
shardsvr = true
maxConns = 20000
# 启动,三个节点均需要启动
# mongod -f /usr/local/mongodb/conf/shard2.conf
#连接mongo 配置shard2
# mongo --host 192.168.19.102 --port 27002
> config = {_id:"shard2", members:[{_id:101, host:"192.168.19.101:27002",arbiterOnly:true}, {_id:102, host:"192.168.19.102:27002"},{_id:103, host:"192.168.19.103:27002"}]}
> rs.initiate(config)
shard2:SECONDARY> rs.status()
shard2:PRIMARY>
############### 以下是shard3的配置 ##############
# cat /usr/local/mongodb/conf/shard3.conf
pidfilepath = /usr/local/mongodb/pids/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
replSet = shard3
shardsvr = true
maxConns = 20000
# 启动,三个节点均需要启动
# mongod -f /usr/local/mongodb/conf/shard3.conf
#连接mongo 配置shard3
# mongo --host 192.168.19.103 --port 27003
> config = {_id:"shard3", members:[{_id:101,host:"192.168.19.101:27003"},{_id:102,host:"192.168.19.102:27003",arbiterOnly:true},{_id:103,host:"192.168.19.103:27003"}]}
> rs.initiate(config)
shard3:SECONDARY> rs.status()
shard3:PRIMARY>
3 mongs配置
# cat /usr/local/mongodb/conf/mongos.conf
pidfilepath = /usr/local/mongodb/pids/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configdb = configs/192.168.19.101:21000,192.168.19.102:21000,192.168.19.103:21000 # 监听的配置服务器,只能有1个或3个,configs为配置服务器的副本集名称
maxConns = 20000
# 启动,三个节点均需要启动
# mongos -f /usr/local/mongodb/conf/mongos.conf
# 连接mongo 配置mongos 把所有的分片和路由器串联
# mongo --host 192.168.19.101 --port 20000
mongos> sh.addShard("shard1/192.168.19.101:27001,192.168.19.102:27001,192.168.19.103:27001")
mongos> sh.addShard("shard2/192.168.19.101:27002,192.168.19.102:27002,192.168.19.103:27002")
mongos> sh.addShard("shard3/192.168.19.101:27003,192.168.19.102:27003,192.168.19.103:27003")
mongos> sh.status() # 查看状态
四、分片测试
mongos> use admin
mongos> db.runCommand( { enablesharding :"test"});
mongos> db.runCommand( { shardcollection : "test.table",key : {id: "hashed"} } )
mongos> use test
mongos> for (var i = 1; i <= 10000; i++) db.table.save({id:i,"test":"testval"})
mongos> db.table.stats() #查看每个分区的数据量
https://www.oschina.net/question/3571801_2286249?sort=default
四、备份与恢复
#备份指定库
# mongodump --host 192.168.19.101 --port 20000 -d test -o /tmp/test
#备份所有库
# mongodump --host 192.168.19.101 --port 20000 -o /tmp/alldb
#备份指定集合
# mongodump --host 192.168.19.101 --port 20000 -d test -c table -o /tmp/test/
#恢复所有库
# mongorestore --host 192.168.19.101 --port 20000 --drop /tmp/alldb # --drop 可选 意思是恢复之前先把之前的数据删除,不建议使用
#恢复指定库
# mongorestore --host 192.168.19.101 --port 20000 -d test /tmp/test
#恢复集合
# mongorestore --host 192.168.19.101 --port 20000 -d test -c table /tmp/test/table.bson
五、其他
1 备份时过滤
# mongodump --host 192.168.19.101 --port 27017 -d touchless -c pousheng_validation_sdap -q '{batch_id: { $in: [ "0001SU201", "0001SU202", "SU20A109210502316"] }, validate_status_flag: "Success"}' -o /cust/cig/data/mongodb/
2