mongoDB 集群与安全
主节点
从节点
哨兵
192.168.152.100 192.168.152.101 192.168.152.102
bash
#master
mkdir -p /opt/mongodb/replica_sets/log && mkdir -p replica_sets/data/db
vim /opt/mongodb/replica_sets/mongod.conf
>>
systemLog:
destination: file
path: "/opt/mongodb/replica_sets/log/mongodb.log"
logAppend: true
storage:
dbPath: "/opt/mongodb/replica_sets/data/db"
journal:
enabled: true
processManagement:
#后台启动
fork: true
pidFilePath: "/opt/mongodb/replica_sets/log/mongod.pid"
net:
# 绑定ip
#记得更改 bindIp: localhost,192.168.152.100
port: 27017
replication:
replSetName: myrs
<<
配置环境变量
>><<
scp =>node1 node2
启动节点
rs.initiate()
rs.conf()
添加从节点
rs.add(host)
添加仲裁节点
rs.addArd(host)
#node1
rs.slaveOk() or rs.secondaryOk()
#取消奴隶节点rs.slaveOk(false)
#node2 仲裁节点 不需要操作
SpringBoot 连接集群形式
uri: mongodb://192.168.152.100:27017,192.168.152.101:27017,192.168.152.102:27017/articledb?connect=replicaSet&slaveOk=true&replicaSet=myrs分片集群
yaml
3+3+ 3 + 2
#############################
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27018/log
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27018/data/db
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27118/log
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27118/data/db
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27218/log
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27218/data/db
conf
/opt/mongodb/shared_cluster/myshardrs01_27018/mongod.conf
>>
systemLog:
destination: file
path: "/opt/mongodb/shared_cluster/myshardrs01_27018/log/mongodb.log"
logAppend: true
storage:
dbPath: "/opt/mongodb/shared_cluster/myshardrs01_27018/data/db"
journal:
enabled: true
processManagement:
#后台启动
fork: true
pidFilePath: "/opt/mongodb/shared_cluster/myshardrs01_27018/log/mongod.pid"
net:
# 绑定ip
#记得更改 bindIp: localhost,192.168.152.100
port: 27018
replication:
##记得更改
### replSetName: myshardrs01
sharding:
clusterRole: shardsvr
<<
#启动
mongod -f /opt/mongodb/shared_cluster/myshardrs01_27018/mongod.conf
#############################第二套。。。 scp
>>
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27018/log
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27018/data/db
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27118/log
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27118/data/db
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27218/log
mkdir -p /opt/mongodb/shared_cluster/myshardrs01_27218/data/db
<<
###################################配置节点集
mkdir -p /opt/mongodb/shared_cluster/myconfigrs_27018/log
mkdir -p /opt/mongodb/shared_cluster/myconfigrs_27018/data/db
mkdir -p /opt/mongodb/shared_cluster/myconfigrs_27118/log
mkdir -p /opt/mongodb/shared_cluster/myconfigrs_27118/data/db
mkdir -p /opt/mongodb/shared_cluster/myconfigrs_27218/log
mkdir -p /opt/mongodb/shared_cluster/myconfigrs_27218/data/db
vim /opt/mongodb/shared_cluster/myconfigrs_27018/mongod.conf
>>
systemLog:
destination: file
path: "/opt/mongodb/shared_cluster/myconfigrs_27018/log/mongodb.log"
logAppend: true
storage:
dbPath: "/opt/mongodb/shared_cluster/myconfigrs_27018/data/db"
journal:
enabled: true
processManagement:
#后台启动
fork: true
pidFilePath: "/opt/mongodb/shared_cluster/myconfigrs_27018/log/mongod.pid"
net:
# 绑定ip
#记得更改 bindIp: localhost,192.168.152.102
port: 27018
replication:
replSetName: myconfgrs
sharding:
clusterRole: configsvr
<<
###################### 节点初始化
#初始化
master
mongo --port=27018
rs.initiate()
rs.add("192.168.152.100:27118")
rs.addArb("192.168.152.100:27218")
node1
mongo --port=27018
rs.initiate()
rs.add("192.168.152.101:27118")
rs.addArb("192.168.152.101:27218")
node2
mongo --port=27018
rs.initiate()
rs.add("192.168.152.102:27118")
rs.add("192.168.152.102:27218")
################################# 路由
mkdir -p /opt/mongodb/shared_cluster/mymongos_27017/log
vim /opt/mongodb/shared_cluster/mymongos_27017/mongos.conf
>>
systemLog:
destination: file
path: "/opt/mongodb/shared_cluster/mymongos_27017/log/mongodb.log"
logAppend: true
processManagement:
#后台启动
fork: true
pidFilePath: "/opt/mongodb/shared_cluster/mymongos_27017/log/mongod.pid"
net:
# 绑定ip
#记得更改 bindIp: localhost,192.168.152.100
port: 27017
sharding:
configDB: myconfgrs/192.168.152.102:27018,192.168.152.102:27118,192.168.152.102:27218
<<
### 启动mongos
添加分片
sh.addShard("ip:port")
sh.addShard("myshardrs01/192.168.152.100:27018,192.168.152.100:27118,192.168.152.100:27218")
sh.addShard("myshardrs02/192.168.152.101:27018,192.168.152.101:27118,192.168.152.101:27218")
sh.status()
#集合分片
sh.enableSharding("articledb")
哈希策略
sh.shardCollection("articledb.comment",{"nickname":"hashed"})
范围策略
sh.shardCollection("articledb.author",{"age":1})
###############添加节点
mkdir -p /opt/mongodb/shared_cluster/mymongos_27117/log
/opt/mongodb/shared_cluster/mymongos_27117/log
cp /opt/mongodb/shared_cluster/mymongos_27017/mongos.conf /opt/mongodb/shared_cluster/mymongos_27117/
vim /opt/mongodb/shared_cluster/mymongos_27117/mongos.conf
################springboot连接
uri :mongodb://192.168.152.100:27017,192.168.152.100:27117/articledb
####################### 安全认证
use admin
db.createUser({user:"myroot",pwd:"123456",roles:["root"]})
db.createUser({user:"myadmin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
db.system.users.find()
db.dropUser("myadmin")
db.changeUserPassword("myroot","123456")
db.auth("myroot","123456")
db.createUser({user:"zly",pwd:"123456",roles:[{role:"readWrite",db:"articledb"}]})
db.auth("zly","123456")
#############开启认证
mongod -f /opt/mongodb/mongod.conf --auth
>> 文件
security:
authorization: enabled
<<
数据库用户角色:read、readWrite;
数据库管理角色:dbAdmin、dbOwner、userAdmin;
集群管理角色:clusterAdmin、clusterManager、4. clusterMonitor、hostManage;
备份恢复角色:backup、restore;
所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
超级用户角色:root
内部角色:__system
db.createUser({
user:"zly",
pwd:"123456",
customData:{
name:'zly',
email:'zly@qq.com',
age:18,
},
roles:[
{role:"readWrite",db:"db001"},
{role:"readWrite",db:"db002"},
'read'// 对其他数据库有只读权限,对db001、db002是读写权限
]
})
@的url编码为%40,:的编码为%3a奇葩错误
yaml
spring:
data:
mongodb:
authentication-database: admin
database: articledb
uri: mongodb://zly:[]@zbzlytop.mongodb.rds.aliyuncs.com:3717,dds-bp1c645f197ee3942563-pub.mongodb.rds.aliyuncs.com:3717/?replicaSet=mgset-70066046