mongoDB (四) mongoDB认证
mongoDB认证
单节点认证
- 配置文件: authorization: enable
[ ~]# vim /data/mongodb/27017/mongodb.conf systemLog: destination: file logAppend: true path: /data/mongodb/27017/mongodb.log storage: dbPath: /data/mongodb/27017/ journal: enabled: true processManagement: fork: true net: port: 27017 bindIp: 0.0.0.0 security: authorization: enabled [ ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/mongodb.conf #启动服务
- 登录报错
[ ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:27017 > use test switched to db test > db.mydata.insert({id:1}) WriteCommandError({ "ok" : 0, "errmsg" : "not authorized on test to execute command { insert: \"mydata\", ordered: true, lsid: { id: UUID(\"84740c59-f4ff-4fe5-879d-d10679b0f355\") }, $db: \"test\" }", "code" : 13, "codeName" : "Unauthorized" }) >
- 解决办法
> use admin > db.createUser({ ... user: "admin", ... pwd: "qwer1234QAZ", ... roles: [ { role: "root",db: "admin" } ] ... }) > use admin > db.auth(‘admin‘,‘qwer1234QAZ‘) > use test > db.mydata.insert({id:"1"}) #插入数据测试
- 登录认证
[ ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:27017 -uadmin -pqwer1234QAZ --authenticationDatabase admin
副本集认证
- 副本集的数据同步使用密钥
- 副本集搭建完成之后再创建用户
证书准备
[ ~]# openssl rand -base64 756 > /data/mongodb/cluster.key [ ~]# chmod 700 /data/mongodb/cluster.key
环境说明
三台副本集机器,设置好各自的端口:27017,27018,27019
我这边先用单节点三副本配置: 其余的配置文件改成对应端口和目录即可
[ ~]# mkdir /data/mongodb/{27017,27018,27019} -pv [ ~]# vim /data/mongodb/27017/mongodb.conf systemLog: destination: file logAppend: true path: /data/mongodb/27017/mongodb.log storage: dbPath: /data/mongodb/27017/ journal: enabled: true processManagement: fork: true net: port: 27017 bindIp: 0.0.0.0 replication: replSetName: cluster security: keyFile: /data/mongodb/cluster.key authorization: enabled
- 启动服务
[ ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/mongodb.conf [ ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27018/mongodb.conf [ ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27019/mongodb.conf
- 初始化
[ ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:27017 > use admin > config = { _id:"cluster", members:[ {_id:0,host:"127.0.0.1:27017"}, {_id:1,host:"127.0.0.1:27018"}, {_id:2,host:"127.0.0.1:27019"}] } > rs.initiate(config) # 初始化 cluster:SECONDARY> rs.status()
- 副本集认证开启
cluster:PRIMARY> use admin cluster:PRIMARY> db.createUser({ ... user: "admin", ... pwd: "qwer1234QAZ", ... roles: [ {role: "root",db:"admin"} ] ... }) > use admin > db.auth(‘admin‘,‘qwer1234QAZ‘) > use test > db.mydata.insert({id:"1"})
- 认证登录
[ ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:27017 -uadmin -pqwer1234QAZ --authenticationDatabase admin
分片配置认证
- router不需要配置认证,但是得配置keyFile
- configsvr和shardsvr需要配置认证和keyFile
相关推荐
大秦铁骑 2020-08-19
thatway 2020-08-19
lovecodeblog 2020-08-19
codetyper 2020-08-16
MongoDB数据库 2020-08-16
csuzxm000 2020-08-02
flyDeDog 2020-06-14
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08