数据库|linux系统mongodb 分片集群搭建

数据库|linux系统mongodb 分片集群搭建
文章图片

(1)如图搭建一个分片集群 要求每个分片节点中的复制集含有一个仲裁节点
(2)使用权限控制 建立访问你访问的数据库lg_resume 这个账号名字是lagou_gx 密码是abc321 这个账号对数据库有读写权限
(3)使用SpringBoot 进行访问分片集群 对lg_resume 库中的lg_resume_datas 进行增加数据
验证视频及代码
链接:https://pan.baidu.com/s/1XRCKDBL7JuUvtZvOtbYiCQ
提取码:lmch
集群部署步骤
下载解压mongodb安装包
链接:https://pan.baidu.com/s/1sXqpR4y08h3n3n6dxokxaA
提取码:bjfs
配置节点一

vi config-17011.conf dbpath=config/config1 #日志文件位置 logpath=config/logs/config1.log #以追加方式写入日志 logappend=true #是否以守护进程方式运行 fork = true bind_ip=0.0.0.0 port = 17011 #表示是一个配置服务器 configsvr=true #配置服务器副本集名称 replSet=configsvr

创建好配置文件中的文件夹
配置节点二
dbpath=config/config2 #日志文件位置 logpath=config/logs/config2.log #以追加方式写入日志 logappend=true #是否以守护进程方式运行 fork = true bind_ip=0.0.0.0 port = 17013 #表示是一个配置服务器 configsvr=true #配置服务器副本集名称 replSet=configsvr

配置节点三
dbpath=config/config3 #日志文件位置 logpath=config/logs/config3.log #以追加方式写入日志 logappend=true #是否以守护进程方式运行 fork = true bind_ip=0.0.0.0 port = 17015 #表示是一个配置服务器 configsvr=true #配置服务器副本集名称 replSet=configsvr

启动配置节点
./bin/mongod -f config/config-17011.conf ./bin/mongod -f config/config-17013.conf ./bin/mongod -f config/config-17015.conf

进入任意节点的mongo shell 并添加配置节点集群,注意use admin
./bin/mongo--port17011 useadmin var cfg ={"_id":"configsvr", "members":[ {"_id":1,"host":"192.168.70.40:17011"}, {"_id":2,"host":"192.168.70.40:17013"}, {"_id":3,"host":"192.168.70.40:17015"}] }; rs.initiate(cfg)

配置shard集群
shard1集群搭建37011,37013,37015,37017
dbpath=shard/shard1/shard1-37011 bind_ip=0.0.0.0 port=37011 fork=true logpath=shard/shard1/shard1-37011.log replSet=shard1 shardsvr=truedbpath=shard/shard1/shard1-37013 bind_ip=0.0.0.0 port=37013 fork=true logpath=shard/shard1/shard1-37013.log replSet=shard1 shardsvr=truedbpath=shard/shard1/shard1-37015 bind_ip=0.0.0.0 port=37015 fork=true logpath=shard/shard1/shard1-37015.log replSet=shard1 shardsvr=truedbpath=shard/shard1/shard1-370171 bind_ip=0.0.0.0 port=37017 fork=true logpath=shard/shard1/shard1-37017.log replSet=shard1 shardsvr=true

创建配置中使用的目录
启动每个mongod
./bin/mongod -f shard/shard1/shard1-37011.conf ./bin/mongod -f shard/shard1/shard1-37013.conf ./bin/mongod -f shard/shard1/shard1-37015.conf ./bin/mongod -f shard/shard1/shard1-37017.conf

然后进入其中一个进行集群配置
var cfg ={"_id":"shard1", "protocolVersion" : 1, "members":[{"_id":1,"host":"192.168.70.40:37011",priority:3},{"_id":2,"host":"192.168.70.40:37013",priority:2}, {"_id":3,"host":"192.168.70.40:37015",priority:1},{"_id":4,"host":"192.168.70.40:37017",arbiterOnly:true}]}; rs.initiate(cfg) rs.status()

shard2集群搭建47011,47013,47015,47017
dbpath=shard/shard2/shard2-47011 bind_ip=0.0.0.0 port=47011 fork=true logpath=shard/shard2/shard2-47011.log replSet=shard2 shardsvr=truedbpath=shard/shard2/shard2-47013 bind_ip=0.0.0.0 port=47013 fork=true logpath=shard/shard2/shard2-47013.log replSet=shard2 shardsvr=truedbpath=shard/shard2/shard2-47015 bind_ip=0.0.0.0 port=47015 fork=true logpath=shard/shard2/shard2-47015.log replSet=shard2 shardsvr=truedbpath=shard/shard2/shard2-47017 bind_ip=0.0.0.0 port=47017 fork=true logpath=shard/shard2/shard2-47017.log replSet=shard2 shardsvr=true

创建配置中使用的目录
启动每个mongod
./bin/mongod -f shard/shard2/shard2-47011.conf ./bin/mongod -f shard/shard2/shard2-47013.conf ./bin/mongod -f shard/shard2/shard2-47015.conf ./bin/mongod -f shard/shard2/shard2-47017.conf

【数据库|linux系统mongodb 分片集群搭建】然后进入其中一个进行集群配置
var cfg ={"_id":"shard2", "protocolVersion" : 1, "members":[ {"_id":1,"host":"192.168.70.40:47011",priority:3}, {"_id":2,"host":"192.168.70.40:47013",priority:2}, {"_id":3,"host":"192.168.70.40:47015",priority:1}, {"_id":4,"host":"192.168.70.40:47017",arbiterOnly:true} ] }; rs.initiate(cfg) rs.status()

shard3集群搭建57011,57013,57015,57017(服务器192.168.70.41)
dbpath=shard/shard3/shard3-57011 bind_ip=0.0.0.0 port=57011 fork=true logpath=shard/shard3/shard3-57011.log replSet=shard3 shardsvr=truedbpath=shard/shard3/shard3-57013 bind_ip=0.0.0.0 port=57013 fork=true logpath=shard/shard3/shard3-57013.log replSet=shard3 shardsvr=truedbpath=shard/shard3/shard3-57015 bind_ip=0.0.0.0 port=57015 fork=true logpath=shard/shard3/shard3-57015.log replSet=shard3 shardsvr=truedbpath=shard/shard3/shard3-57017 bind_ip=0.0.0.0 port=57017 fork=true logpath=shard/shard3/shard3-57017.log replSet=shard3 shardsvr=true

创建配置中使用的目录
启动每个mongod
./bin/mongod -f shard/shard3/shard3-57011.conf ./bin/mongod -f shard/shard3/shard3-57013.conf ./bin/mongod -f shard/shard3/shard3-57015.conf ./bin/mongod -f shard/shard3/shard3-57017.conf

然后进入其中一个进行集群配置
var cfg ={"_id":"shard3", "protocolVersion" : 1, "members":[ {"_id":1,"host":"192.168.70.41:57011",priority:3}, {"_id":2,"host":"192.168.70.41:57013",priority:2}, {"_id":3,"host":"192.168.70.41:57015",priority:1}, {"_id":4,"host":"192.168.70.41:57017",arbiterOnly:true} ] }; rs.initiate(cfg) rs.status()

shard4集群搭建58011,58013,58015,58017
dbpath=shard/shard4/shard4-58011 bind_ip=0.0.0.0 port=58011 fork=true logpath=shard/shard4/shard4-58011.log replSet=shard4 shardsvr=truedbpath=shard/shard4/shard4-58013 bind_ip=0.0.0.0 port=58013 fork=true logpath=shard/shard4/shard4-58013.log replSet=shard4 shardsvr=truedbpath=shard/shard4/shard4-58015 bind_ip=0.0.0.0 port=58015 fork=true logpath=shard/shard4/shard4-58015.log replSet=shard4 shardsvr=truedbpath=shard/shard4/shard4-58017 bind_ip=0.0.0.0 port=58017 fork=true logpath=shard/shard4/shard4-58017.log replSet=shard4 shardsvr=true

创建配置中使用的目录
启动每个mongod
./bin/mongod -f shard/shard4/shard4-58011.conf ./bin/mongod -f shard/shard4/shard4-58013.conf ./bin/mongod -f shard/shard4/shard4-58015.conf ./bin/mongod -f shard/shard4/shard4-58017.conf

然后进入其中一个进行集群配置
var cfg ={"_id":"shard4", "protocolVersion" : 1, "members":[ {"_id":1,"host":"192.168.70.41:58011",priority:3}, {"_id":2,"host":"192.168.70.41:58013",priority:2}, {"_id":3,"host":"192.168.70.41:58015",priority:1}, {"_id":4,"host":"192.168.70.41:58017",arbiterOnly:true} ] }; rs.initiate(cfg) rs.status()

配置和启动路由节点
route-27017.conf
port=27017 bind_ip=0.0.0.0 fork=true logpath=route/logs/route.log configdb=configsvr/192.168.70.40:17011,192.168.70.40:17013,192.168.70.40:17015

启动路由节点使用 mongos (注意不是mongod)
./bin/mongos -froute/route-27017.conf

mongos(路由)中添加分片节
进入路由mongos
./bin/mongo --port 27017 sh.status()

sh.addShard("shard1/192.168.70.40:37011,192.168.70.40:37013,192.168.70.40:37015,192.168.70.40:37017"); sh.addShard("shard2/192.168.70.40:47011,192.168.70.40:47013,192.168.70.40:47015,192.168.70.40:47017"); sh.addShard("shard3/192.168.70.41:57011,192.168.70.41:57013,192.168.70.41:57015,192.168.70.41:57017"); sh.addShard("shard4/192.168.70.41:58011,192.168.70.41:58013,192.168.70.41:58015,192.168.70.41:58017"); sh.status()

开启数据库和集合分片(指定片键)
继续使用mongos完成分片开启和分片大小设置
为数据库开启分片功能 sh.enableSharding("lagou_resume") 为指定集合开启分片功能 sh.shardCollection("lagou_resume.lagou_resume_datas",{"片键字段名如 name":索引说 明}) sh.shardCollection("lagou_resume.lagou_resume_datas",{"name":"hashed"})

集群验证
向集合中插入数据测试,通过路由循环向集合中添加数
uselagou_resume; for(var i=1; i<= 1000; i++){db.lagou_resume_datas.insert({"name":"test"+i,salary:(Math.random()*20000).toFixed(2)}); }

./bin/mongod -f shard/shard3/shard3-57011.conf ./bin/mongod -f shard/shard3/shard3-57013.conf ./bin/mongod -f shard/shard3/shard3-57015.conf ./bin/mongod -f shard/shard3/shard3-57017.conf ./bin/mongod -f shard/shard4/shard4-58011.conf ./bin/mongod -f shard/shard4/shard4-58013.conf ./bin/mongod -f shard/shard4/shard4-58015.conf ./bin/mongod -f shard/shard4/shard4-58017.conf

(2)使用权限控制 建立访问你访问的数据库lg_resume 这个账号名字是lagou_gx 密码是abc321 这个账号对数据库有读写权限
use admin
db.createUser( { user:“root”, pwd:“123456”, roles:[{role:“root”,db:“admin”}] })
use lg_resume
db.createUser( { user:“lagou_gx”, pwd:“abc321”, roles:[{role:“readWrite”,db:“lg_resume”}] })
关闭所有的配置节点 分片节点 和 路由节
killall mongod (需要安装psmisc yum install psmisc)
.生成密钥文件 并修改权限
openssl rand -base64 756 > data/mongodb/testKeyFile.file
修改权限
chmod 600 data/mongodb/testKeyFile.file
配置节点集群和分片节点集群开启安全认证和指定密钥文件
auth=true
keyFile=data/mongodb/testKeyFile.file
在路由配置文件中 设置密钥文件
keyFile=data/mongodb/testKeyFile.file
启动所有配置节点
./bin/mongod -f config/config-17011.conf ./bin/mongod -f config/config-17013.conf ./bin/mongod -f config/config-17015.conf ./bin/mongod -f shard/shard1/shard1-37011.conf ./bin/mongod -f shard/shard1/shard1-37013.conf ./bin/mongod -f shard/shard1/shard1-37015.conf ./bin/mongod -f shard/shard1/shard1-37017.conf ./bin/mongod -f shard/shard2/shard2-47011.conf ./bin/mongod -f shard/shard2/shard2-47013.conf ./bin/mongod -f shard/shard2/shard2-47015.conf ./bin/mongod -f shard/shard2/shard2-47017.conf ./bin/mongos -froute/route-27017.conf

使用SpringBoot 进行访问分片集群 对lg_resume 库中的lg_resume_datas 进行增加数据
链接:https://pan.baidu.com/s/1XRCKDBL7JuUvtZvOtbYiCQ
提取码:lmch

    推荐阅读