docker安裝mongo及開啟使用者認證
阿新 • • 發佈:2018-11-09
1、下載映象
docker pull mongo:3.6.6
2、執行
2.1、資料庫授權
預設情況下,mongo資料庫沒有新增認證約束,為了增強資料庫的安全性,我們需要對資料庫新增授權認證
1、在執行mongo容器命令中新增--auth
引數
命令說明:
- –name:自定義別名
- -p 27017:27017 :將容器的27017 埠對映到主機的27017 埠
- -v /etc/localtime:/etc/localtime:ro:掛載系統時區
- -v /home/mongo/db:/data/db :將主機中 /home/mongo/db 掛載到容器的 /data/db,作為mongo資料儲存目錄
- –antu:開啟密碼授權訪問
docker run --name mongo-master -p 27017:27017 -v /etc/localtime:/etc/localtime:ro -v /home/mongo/db:/data/db -d mongo:3.6.6 --auth
2.2、建立使用者管理員
2.2.1、首先進入mongo容器
docker exec -it mongo-master bash
2.2.2、建立使用者管理員(此語法適用於mongo v3.x)
# 進入mongo資料庫
mongo
# 首先切換到admin資料庫下
use admin;
# 建立一個使用者admin, 密碼是Hello123
# 此使用者即為管理員
# user: 使用者名稱
# pwd: 密碼明文
# role: 使用者角色 db: 該使用者將建立到哪個資料庫中
db.createUser({
user: 'admin',
pwd: 'Hello123',
roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]
});
# 測試下是否正確
db.auth("admin", "Hello123");
1 # 返回1表示正確
# 退出
exit;
role角色引數參考:
- Read:允許使用者讀取指定資料庫
- readWrite:允許使用者讀寫指定資料庫
- dbAdmin:允許使用者在指定資料庫中執行管理函式,如索引建立、刪除,檢視統計或訪問system.profile
- userAdmin:允許使用者向system.users集合寫入,可以找指定資料庫裡建立、刪除和管理使用者
- clusterAdmin:只在admin資料庫中可用,賦予使用者所有分片和複製集相關函式的管理許可權
- readAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀許可權
- readWriteAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀寫許可權
- userAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的userAdmin許可權
- dbAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的dbAdmin許可權
- root:只在admin資料庫中可用。超級賬號,超級許可權
2.3、建立訪問指定資料庫的使用者
管理員已經建立成功後,我們需要重新連線mongo資料庫,用管理員進行登入,併為目標資料庫建立目標使用者
2.3.1、退出容器,重新進入容器
docker exec -it mongo-master mongo admin
2.3.2、授權登陸admin
db.auth("admin", "Hello123");
2.3.3、建立訪問指定資料庫的使用者
假設我們為 test 庫建立一個使用者,使用者名稱為 test,密碼為 test
use test;
db.createUser({ user: 'test', pwd:'test', roles: [ {role:"readWrite",db:"student"}]});
db.auth("test","test");
建立成功並exit
退出,test 使用者可以對(也只能對) student 庫進行操作
注:
- admin的作用是管理使用者,MongoDB下的每個資料庫,使用者都被它管理,除此外,它基本沒什麼更多許可權做其他事情
- MongoDB沒有通常意義的超級使用者的概念,test庫的授權使用者只能被admin建立,而admin只能登陸admin資料庫
操作記錄:
[[email protected] ~]# docker pull mongo:3.6.6
3.6.6: Pulling from library/mongo
2caa28db99eb: Pull complete
f47091165808: Pull complete
1ee453af0da6: Pull complete
33c5b5832a66: Pull complete
27b2ac6ef5db: Pull complete
05dbddd53552: Pull complete
d417ae2661cf: Pull complete
5a42b4806d9d: Pull complete
3d03045cfed1: Pull complete
95c2d5a2400c: Pull complete
Digest: sha256:bc5d490da249a792c1aec0808640c74d236e30aea02f41b018d1b4c4685c429f
Status: Downloaded newer image for mongo:3.6.6
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ryaning/tomcat 7.0.90-jre7-alpine-catalina a7824e8cc0d0 3 days ago 147MB
ryaning/tomcat 8.0.53-jre8-alpine-catalina 43f03e135be6 3 days ago 107MB
zookeeper 3.4 cfed220ec48b 5 days ago 148MB
mongo 3.6.6 5324878b8244 5 days ago 368MB
mysql 5.7.22 66bc0f66b7af 2 weeks ago 372MB
ilanyu/golang-reverseproxy latest a2714f84f679 3 months ago 7.33MB
[[email protected] ~]# docker run --name mongo-master -p 27017:27017 -v /etc/localtime:/etc/localtime:ro -v /home/mongo/db:/data/db -d mongo:3.6.6 --auth
a598c22d79e166efc7765a36ee16bf647df6663ef7e55d80571e663571eab896
[[email protected] ~]# docker exec -it mongo-master bash
[email protected]:/# mongo;
MongoDB shell version v3.6.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
> use admin;
switched to db admin
> db.createUser({
... user: 'admin',
... pwd: 'Hello123',
... roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]
... });
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> db.auth("admin", "Hello123");
1
> exit;
[email protected]:/# exit
exit
[[email protected] ~]# docker exec -it mongo-master mongo admin
MongoDB shell version v3.6.6
connecting to: mongodb://127.0.0.1:27017/admin
MongoDB server version: 3.6.6
> db.auth("admin", "Hello123");
1
> use test;
switched to db test
> db.createUser({ user: 'test', pwd:'test', roles: [ {role:"readWrite",db:"student"}]});
Successfully added user: {
"user" : "test",
"roles" : [
{
"role" : "readWrite",
"db" : "student"
}
]
}
> db.auth("test","test");
1
> exit
bye
[[email protected] ~]#