開啟授權(quán)認(rèn)證之后盈包,只有經(jīng)過授權(quán)的用戶才能夠連接到MongoDB沸呐,才允許訪問相關(guān)的資源,增加數(shù)據(jù)庫的安全性與穩(wěn)定性呢燥。
分片集群適合keyFile的形式開啟認(rèn)證崭添,而副本集適合開啟auth即可。
思路:為5個(gè)集群分片叛氨,shard1呼渣、shard2、shard3力试、shard4徙邻、shard5分別創(chuàng)建超級(jí)用戶(用來分別管理Mongo集群的分片),再為集群創(chuàng)建一個(gè)管理用戶畸裳,控制外部鏈接對(duì)集群進(jìn)程Mongos的訪問缰犁。?
1、為shard1創(chuàng)建分片管理超級(jí)用戶【shard2,shard3,shard4,shard5類似】
cd/usr/local/mongodb/bin
./mongo 127.0.0.145:22001
#查看副本集狀態(tài)
shard1:PRIMARY>rs.status()
shard1:PRIMARY>use admin
#添加超級(jí)用戶
shard1:PRIMARY>db.createUser(
? ? ? ? {
? ? ? ? ? ? ? ? ? user:"ybl_shard1",
? ? ? ? ? ? ? ? ? pwd:"ybl_shard1",
? ? ? ? ? ? ? ? ? roles:[{role:"root",db:"admin"}]
? ? ? ? }
)
shard1:PRIMARY> db.auth("ybl_shard1","ybl_shard1")
#這一步是為了驗(yàn)證角色是否創(chuàng)建成功怖糊,1-成功帅容,其他-失敗
#查看用戶
shard1:PRIMARY>show users
2、為基于副本集的分片集群創(chuàng)建超級(jí)管理用戶
cd/usr/local/mongodb/bin
./mongo 127.0.0.143:20000
mongos> use admin
switched to db admin
mongos>db.createUser(
{
user:"yuanbl",
pwd:"yuanbl",
roles:[{role:"root",db:"admin"}]
}
);
db.auth("yuanbl","yuanbl")
mongos> db.auth("yuanbl","yuanbl")
3伍伤、關(guān)閉集群進(jìn)程
可先關(guān)閉shard服務(wù)并徘,systemctl stop mongodb
4、創(chuàng)建keyFile文件
1扰魂、mkdir -p /opt/mongo/keyfile/
2麦乞、cd/opt/mongo/keyfile
3、openssl rand -base64 753>mongo-keyfile
4劝评、chmod 600 keyfile
注意點(diǎn):
3步驟中的數(shù)字不宜太大姐直,否則啟動(dòng)時(shí)會(huì)報(bào)錯(cuò)
【security
key in /opt/mongo/keyfile/keyfile has length 1368, must be between 6 and 1024
chars】
4步驟一定得執(zhí)行,要不然報(bào)錯(cuò)
【permissions on /opt/mongo/keyfile/mongo-keyfile are too open】
5蒋畜、將文件keyFile存儲(chǔ)在在所有的節(jié)點(diǎn)指定位置
mkdir –p /usr/local/mongodb/key
mongo-keyfile存放在此目錄下
6声畏、使用keyFile參數(shù)指定keyfile啟動(dòng)分片shard1、shard2姻成、shard3插龄、shard4、shard5
修改所有shard啟動(dòng)配置文件科展,增加keyFile =/usr/local/mongodb/key/mongo-keyfile
然后啟動(dòng)Systemctl start mongodb
7均牢、shard1、shard2才睹、shard3膨处、shard1见秤、shard1砂竖、服務(wù)器端基于keyfile的用戶口令認(rèn)證測(cè)試
./mongo admin --port 22001 -uybl_shard1 -p ybl_shard1
或
./mongo 127.0.0.145:22001
use admin
db.auth(“ybl_shard1”,”ybl_shard1”)
其他類似
如果認(rèn)證不成功就會(huì)報(bào)錯(cuò)
2017-03-12T17:40:16.236+0800 EQUERY[thread1] Error: listDatabasesfailed:{
? ? ? ? ?"ok" : 0,
? ? ? ? ?"errmsg" : "notauthorized on admin to execute command { listDatabases: 1.0 }",
? ? ? ? ?"code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:761:19
shellHelper@src/mongo/shell/utils.js:651:15
@(shellhelp2):1:1
到此分片開啟成功
8真椿、config server按照如上步驟處理
1、先關(guān)閉config server:systemctl stop mongodb 或 kill -2 pid
2乎澄、將moongo-keyfile放入目錄下/usr/local/mongodb/key,設(shè)置權(quán)限chmod 600 mongo-keyfile
3突硝、修改config server啟動(dòng)配置文件,加入keyFile = /usr/local/mongodb/key/mongo-keyfile置济,保存
4解恰、啟動(dòng)config server:
systemctl start mongodb
9、mongos按照如上步驟處理
1浙于、先關(guān)閉mongos:systemctl stop mongos或kill -2 pid
2护盈、將moongo-keyfile放入目錄下/usr/local/mongodb/key/,設(shè)置權(quán)限chmod600 mongo-keyfile
3、修改mongos啟動(dòng)配置文件羞酗,加入keyFile =/usr/local/mongodb/key/mongo-keyfile腐宋,保存
4、啟動(dòng)mongos:
systemctlstart mongos
10檀轨、測(cè)試分片集群基于keyfile的用戶口令認(rèn)證
cd/usr/local/mongodb/bin/
./mongo127.0.0.143:20000
showdbs
2017-03-12T18:04:28.845+0800E QUERY[thread1] Error: listDatabasesfailed:{
? ? ? ? ?"ok" : 0,
? ? ? ? ?"errmsg" : "notauthorized on admin to execute command { listDatabases: 1.0 }",
? ? ? ? ?"code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:761:19
shellHelper@src/mongo/shell/utils.js:651:15
@(shellhelp2):1:1
useadmin
db.auth("yuanbl","yuanbl")
1 ?#說明認(rèn)證成功
#登陸時(shí)指定
./mongoadmin --port 20000 -u yuanbl -p yuanbl
或
./mongo 127.0.0.143:20000
Use admin
db.auth("yuanbl","yuanbl")