• MongoDB分片集群架构
  • config配置节点集群搭建
  • shard分片集群搭建
  • route路由节点搭建
  • 集群安全认证配置

  • MongoDB分片集群架构

CentOS7-MongoDB分片集群搭建_第1张图片

  • config配置节点集群搭建
  •  wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.0.tgz #MongoDB安装包
[root@centos-linux--1- ~]# cd shard_cluster/
[root@centos-linux--1- shard_cluster]# mkdir shard
[root@centos-linux--1- shard_cluster]# cd shard
[[root@centos-linux--1- shard]# mkdir shard1 shard2 shard3
[root@centos-linux--1- shard]# cd shard1/
[root@centos-linux--1- shard1]# mkdir shard1-37017 shard1-37018 shard1-37019
[root@centos-linux--1- shard1]# vi shard-37017.cnf
dbpath=shard/shard1/shard1-37017
bind_ip=0.0.0.0
port=37017
fork=true
logpath=shard/shard1/shard1-37017.log
shardsvr=true
replSet=shard1
[root@centos-linux--1- shard1]# cp shard-37017.cnf shard-37018.cnf
[root@centos-linux--1- shard1]# vi shard-37018.cnf
dbpath=shard/shard1/shard1-37018
bind_ip=0.0.0.0
port=37018
fork=true
logpath=shard/shard1/shard1-37018.log
shardsvr=true
replSet=shard1
[root@centos-linux--1- shard1]# cp shard-37018.cnf shard-37019.cnf
[root@centos-linux--1- shard1]# vi shard-37019.cnf
dbpath=shard/shard1/shard1-37019
bind_ip=0.0.0.0
port=37019
fork=true
logpath=shard/shard1/shard1-37019.log
shardsvr=true
replSet=shard1
[root@centos-linux--1- shard1]# cd ../..
[root@centos-linux--1- shard_cluster]# ./bin/mongod -f shard/shard1/shard-37017.cnf
[root@centos-linux--1- shard_cluster]# ./bin/mongod -f shard/shard1/shard-37018.cnf
[root@centos-linux--1- shard_cluster]# ./bin/mong./od -f shard/shard1/shard-37019.cnf
[root@centos-linux--1- shard_cluster]# ./bin/mongo --port 37017
> var cfg ={"_id":"shard1",
... "protocolVersion" : 1,
... "members":[
... {"_id":1,"host":"10.211.55.4:37017"},
... {"_id":2,"host":"10.211.55.4:37018"},
... {"_id":3,"host":"10.211.55.4:37019"}
... ]
... };
> rs.initiate(cfg)
> rs.status()
// 添加仲裁节点
> var cfg ={"_id":"shard4",
... "protocolVersion" : 1,
... "members":[
... {"_id":1,"host":"10.211.55.4:38017"},
... {"_id":2,"host":"10.211.55.4:38018"},
... {"_id":3,"host":"10.211.55.4:38019"},
... {"_id":4,"host":"10.211.55.4:38020","arbiterOnly":true}
... ]
... };
> rs.reconfig(cfg)
> rs.status()

  • shard分片集群搭建
[root@centos-linux--1- ~]# tar -xvf mongodb-linux-x86_64-rhel70-4.4.1.tgz
[root@centos-linux--1- ~]# mv mongodb-linux-x86_64-rhel70-4.4.1 shard_cluster
[root@centos-linux--1- ~]# cd shard_cluster/
[root@centos-linux--1- shard_cluster]# mkdir config
[root@centos-linux--1- shard_cluster]# cd config/
[root@centos-linux--1- config]# vi config-17017.cnf
dbpath=config/config1
#日志文件位置
logpath=config/logs/config1.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17017
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
[root@centos-linux--1- config]# mkdir config1 logs
[root@centos-linux--1- config]# cd ..
[root@centos-linux--1- shard_cluster]# ./bin/mongod -f config/config-17017.cnf
[root@centos-linux--1- shard_cluster]# cd config/
[root@centos-linux--1- config]# cp config-17017.cnf config-17018.cnf
[root@centos-linux--1- config]# vi config-17018.cnf
dbpath=config/config2
#日志文件位置
logpath=config/logs/config2.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17018
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
[root@centos-linux--1- config]# mkdir config2 config3
[root@centos-linux--1- config]# cp config-17018.cnf config-17019.cnf
[root@centos-linux--1- config]# vi config-17019.cnf
dbpath=config/config3
#日志文件位置
logpath=config/logs/config3.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17019
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
[root@centos-linux--1- config]# cd ..
[root@centos-linux--1- shard_cluster]# ./bin/mongod -f config/config-17018.cnf
[root@centos-linux--1- shard_cluster]# ./bin/mongod -f config/config-17019.cnf
[root@centos-linux--1- shard_cluster]# ./bin/mongo --port 17017
> use admin
> var cfg ={"_id":"configsvr",
... "members":[
... {"_id":1,"host":"10.211.55.4:17017"},
... {"_id":2,"host":"10.211.55.4:17018"},
... {"_id":3,"host":"10.211.55.4:17019"}]
... };
> rs.initiate(cfg)
>rs.status()

  • route路由节点搭建
[root@centos-linux--1- ~]# cd shard_cluster
[root@centos-linux--1- shard_cluster]# mkdir route/logs
[root@centos-linux--1- shard_cluster]# cd route
[root@centos-linux--1- route]# vi route-27017.cnf
bind_ip=0.0.0.0
fork=true
logpath=route/logs/route.log
configdb=configsvr/10.211.55.4:17017,10.211.55.4:17018,10.211.55.4:17019
[root@centos-linux--1- route]# cd ..
[root@centos-linux--1- shard_cluster]# ./bin/mongos -f route/route-27017.cnf
[root@centos-linux--1- shard_cluster]# ./bin/mongo
mongos> sh.status()
mongos> sh.addShard("shard1/10.211.55.4:37017,10.211.55.4:37018,10.211.55.4:37019");
mongos> sh.status()
// 为数据库开启分片功能
mongos> sh.enableSharding("xxx_db")
// 为指定集合开启分片功能
mongos> sh.shardCollection("xxx_db.xxx_datas",{"name":"hashed"})
// 验证
mongos> use xxx_db;
mongos> for(var i=1;i<= 1000;i++){ db.xxx_datas.insert({"name":"test"+i, salary:(Math.random()*20000).toFixed(2)}); }

  • 集群安全认证配置
mongos> use admin
mongos> db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
mongos> use xxx_db
mongos> db.createUser({ user:"usename", pwd:"password", roles:[{role:"readWrite",db:"xxx_db"}]})

[root@centos-linux--1- ~]# yum install psmisc
[root@centos-linux--1- ~]# ps -ef|grep mongo
[root@centos-linux--1- ~]# killall mongo
[root@centos-linux--1- ~]# killall mongod
[root@centos-linux--1- ~]# killall mongos
[root@centos-linux--1- ~]# cd shard_cluster/
[root@centos-linux--1- shard_cluster]# mkdir data/mongodb -p
[root@centos-linux--1- shard_cluster]# openssl rand -base64 756 > data/mongodb/testKeyFile.file
[root@centos-linux--1- shard_cluster]# cd data/mongodb/
[root@centos-linux--1- mongodb]# chmod 600 testKeyFile.file
[root@centos-linux--1- mongodb]# cd ../..
// 修改每一个配置文件开启安全认证
[root@centos-linux--1- shard_cluster]# vi config/config-17017.cnf
dbpath=config/config1
#日志文件位置
logpath=config/logs/config1.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17017
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
#安全认证
auth=true
keyFile=data/mongodb/testKeyFile.file
// 脚本批量启动分片集群,注意顺序
[root@centos-linux--1- shard_cluster]# vi startup.sh
./bin/mongod -f config/config-17018.cnf
./bin/mongod -f config/config-17019.cnf
./bin/mongod -f shard/shard1/shard-37017.cnf
./bin/mongod -f shard/shard1/shard-37018.cnf
./bin/mongod -f shard/shard1/shard-37019.cnf
./bin/mongod -f shard/shard2/shard-47017.cnf
./bin/mongod -f shard/shard2/shard-47018.cnf
./bin/mongod -f shard/shard2/shard-47019.cnf
./bin/mongod -f shard/shard3/shard-57017.cnf
./bin/mongod -f shard/shard3/shard-57018.cnf
./bin/mongod -f shard/shard3/shard-57019.cnf
./bin/mongos -f route/route-27017.cnf
[root@centos-linux--1- shard_cluster]# ./startup.sh
[root@centos-linux--1- shard_cluster]# ./bin/mongo
mongos> use admin
mongos> db.auth("root","123456")
mongos> show dbs
mongos> exit
[root@centos-linux--1- shard_cluster]# ./bin/mongo
mongos> use xxx_db
mongos> db.auth("usename","password")
mongos> show dbs


友情提示:记得点赞、收藏、加关注,鼓励一下哦~