開啟授權認證之后,只有經過授權的用戶才能夠連接到MongoDB,才允許訪問相關的資源,增加數(shù)據庫的安全性與穩(wěn)定性。
分片集群適合keyFile的形式開啟認證,而副本集適合開啟auth即可。
思路:為5個集群分片,shard1、shard2、shard3、shard4、shard5分別創(chuàng)建超級用戶(用來分別管理Mongo集群的分片),再為集群創(chuàng)建一個管理用戶,控制外部鏈接對集群進程Mongos的訪問。?
1、為shard1創(chuàng)建分片管理超級用戶【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
#添加超級用戶
shard1:PRIMARY>db.createUser(
? ? ? ? {
? ? ? ? ? ? ? ? ? user:"ybl_shard1",
? ? ? ? ? ? ? ? ? pwd:"ybl_shard1",
? ? ? ? ? ? ? ? ? roles:[{role:"root",db:"admin"}]
? ? ? ? }
)
shard1:PRIMARY> db.auth("ybl_shard1","ybl_shard1")
#這一步是為了驗證角色是否創(chuàng)建成功,1-成功,其他-失敗
#查看用戶
shard1:PRIMARY>show users
2、為基于副本集的分片集群創(chuàng)建超級管理用戶
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、關閉集群進程
可先關閉shard服務,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
注意點:
3步驟中的數(shù)字不宜太大,否則啟動時會報錯
【security
key in /opt/mongo/keyfile/keyfile has length 1368, must be between 6 and 1024
chars】
4步驟一定得執(zhí)行,要不然報錯
【permissions on /opt/mongo/keyfile/mongo-keyfile are too open】
5、將文件keyFile存儲在在所有的節(jié)點指定位置
mkdir –p /usr/local/mongodb/key
mongo-keyfile存放在此目錄下
6、使用keyFile參數(shù)指定keyfile啟動分片shard1、shard2、shard3、shard4、shard5
修改所有shard啟動配置文件,增加keyFile =/usr/local/mongodb/key/mongo-keyfile
然后啟動Systemctl start mongodb
7、shard1、shard2、shard3、shard1、shard1、服務器端基于keyfile的用戶口令認證測試
./mongo admin --port 22001 -uybl_shard1 -p ybl_shard1
或
./mongo 127.0.0.145:22001
use admin
db.auth(“ybl_shard1”,”ybl_shard1”)
其他類似
如果認證不成功就會報錯
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、先關閉config server:systemctl stop mongodb 或 kill -2 pid
2、將moongo-keyfile放入目錄下/usr/local/mongodb/key,設置權限chmod 600 mongo-keyfile
3、修改config server啟動配置文件,加入keyFile = /usr/local/mongodb/key/mongo-keyfile,保存
4、啟動config server:
systemctl start mongodb
9、mongos按照如上步驟處理
1、先關閉mongos:systemctl stop mongos或kill -2 pid
2、將moongo-keyfile放入目錄下/usr/local/mongodb/key/,設置權限chmod600 mongo-keyfile
3、修改mongos啟動配置文件,加入keyFile =/usr/local/mongodb/key/mongo-keyfile,保存
4、啟動mongos:
systemctlstart mongos
10、測試分片集群基于keyfile的用戶口令認證
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 ?#說明認證成功
#登陸時指定
./mongoadmin --port 20000 -u yuanbl -p yuanbl
或
./mongo 127.0.0.143:20000
Use admin
db.auth("yuanbl","yuanbl")