新聞中心
在上文中,我們了解了教你如何利用MySQL學(xué)習(xí)MongoDB之SQL語(yǔ)法,本文中我們繼續(xù)我們的學(xué)習(xí)之旅,學(xué)習(xí)兩者的授權(quán)和權(quán)限。

數(shù)據(jù)庫(kù)的安全性是每一個(gè)DBA重點(diǎn)關(guān)注的部分,在數(shù)據(jù)庫(kù)建立之后,數(shù)據(jù)的安全就顯得尤為重要。
對(duì)于一個(gè)數(shù)據(jù)庫(kù)管理員來(lái)說(shuō),安全性就意味著他必須保證那些具有特殊數(shù)據(jù)訪問(wèn)權(quán)限的用戶能夠登錄到數(shù)據(jù)庫(kù)服務(wù)器,并且能夠訪問(wèn)數(shù)據(jù)以及對(duì)數(shù)據(jù)庫(kù)對(duì)象實(shí)施各種權(quán)限范圍內(nèi)的操作;同時(shí),DBA還要防止所有的非授權(quán)用戶的非法操作。
1、MySQL授權(quán)和權(quán)限
MySQL中有兩種級(jí)別的權(quán)限:管理和用戶。所有權(quán)限都可分別使用 GRANT 和 REVOKE 語(yǔ)句授予和收回??梢允谟栌脩鬰reate、select、update、delete、insert、execute、index 等權(quán)限,也可授予alter、drop和shutdown等系統(tǒng)權(quán)限。根用戶root在默認(rèn)情況下具有所有權(quán)限。
2、MongoDB授權(quán)和權(quán)限
官方文檔開(kāi)啟MongoDB 服務(wù)時(shí)不添加任何參數(shù)時(shí),可以對(duì)數(shù)據(jù)庫(kù)任意操作,而且可以遠(yuǎn)程訪問(wèn)數(shù)據(jù)庫(kù),所以推薦只是在開(kāi)發(fā)是才這樣不設(shè)置任何參數(shù)。如果啟動(dòng)的時(shí)候指定--auth參數(shù),可以從阻止根層面上的訪問(wèn)和連接
(1)、只允許某ip訪問(wèn)
mongod --bind_ip 127.0.0.1
(2)、指定服務(wù)端口
mongod --bind_ip 127.0.0.1 --port27888
(3)、添加用戶認(rèn)證
mongod --bind_ip 127.0.0.1 --port27888 –auth
(4)、添加用戶
在剛安裝完畢的時(shí)候MongoDB都默認(rèn)有一個(gè)admin數(shù)據(jù)庫(kù),而admin.system.users中將會(huì)保存比在其它數(shù)據(jù)庫(kù)中設(shè)置的用戶權(quán)限更大的用戶信息。
當(dāng)admin.system.users中一個(gè)用戶都沒(méi)有時(shí),即使mongod啟動(dòng)時(shí)添加了--auth參數(shù),如果沒(méi)有在admin數(shù)據(jù)庫(kù)中添加用戶,此時(shí)不進(jìn)行任何認(rèn)證還是可以做任何操作,直到在admin.system.users中添加了一個(gè)用戶。
下面分別創(chuàng)建兩個(gè)用戶, 在foo中創(chuàng)建用戶名為user1密碼為pwd1的用戶,如下:
- [root@localhost bin]# ./mongo --port 27888
- MongoDB shell version: 1.8.1
- connecting to: test
- > use foo
- switched to db foo
- > db.addUser("user1","pwd1")
- {
- "user" : "user1",
- "readOnly" : false,
- "pwd" : "35263c100eea1512cf3c3ed83789d5e4"
- }
[[29688]]
在admin中創(chuàng)建用戶名為root密碼為pwd2的用戶,如下:[[29688]]
- > use admin
- switched to db admin
- > db.addUser("root", "pwd2")
- {
- "_id" : ObjectId("4f8a87bce495a88dad4613ad"),
- "user" : "root",
- "readOnly" : false,
- "pwd" : "20919e9a557a9687c8016e314f07df42"
- }
- > db.auth("root", "pwd2")
- 1
- >
如果認(rèn)證成功會(huì)顯示1, 用以下命令可以查看特定的數(shù)據(jù)庫(kù)的用戶信息:[[29688]]
- > use admin
- switched to db admin
- > db.system.users.find();
- { "_id" : ObjectId("4f8a87bce495a88dad4613ad"), "user" : "root", "readOnly" : false, "pwd" : "20919e9a557a9687c8016e314f07df42" }
- > use foo
- switched to db foo
- > db.system.users.find();
- { "_id" : ObjectId("4f92966d77aeb2b2e730c1bb"), "user" : "user1", "readOnly" : false, "pwd" : "35263c100eea1512cf3c3ed83789d5e4" }
- >
下面我們?cè)囼?yàn)一下用戶的權(quán)限設(shè)置是否正確:[[29688]]
- [root@localhost bin]# ./mongo --port 27888
- MongoDB shell version: 1.8.1
- connecting to: 127.0.0.1:27888/test
- > use foo
- switched to db foo
- > db.system.users.find();
- error: {
- "$err" : "unauthorized db:foo lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- > use admin
- switched to db admin
- > db.system.users.find();
- error: {
- "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- >
通知以上實(shí)驗(yàn)結(jié)果,說(shuō)明登錄時(shí)不指定用戶名和口令時(shí)會(huì)報(bào)錯(cuò),也就是說(shuō)安全性的部署生效了。下面我再看一下另一個(gè)場(chǎng)景:
- [root@localhost bin]# ./mongo --port 27888 -uroot -ppwd2
- MongoDB shell version: 1.8.1
- connecting to: 127.0.0.1:27888/test
- Sat Apr 21 19:23:15 uncaught exception: login failed
- exception: login failed
奇怪了,我們明明指定了用戶名而且口令也沒(méi)有錯(cuò)呀,這時(shí)我們看一下系統(tǒng)日志上是否有一些有價(jià)值的信息:
auth: couldn't find user root, test.system.users
哦,原來(lái)是這樣,說(shuō)明連接mongodb時(shí),如果不指定庫(kù)名,那么會(huì)自動(dòng)連接到test庫(kù),但剛才我們新建的用戶,都不是在test庫(kù)上建立的,所以我們需要顯示指定需要連接的庫(kù)名:
- [root@localhost bin]# ./mongo --port 27888 admin -uroot -ppwd2
- MongoDB shell version: 1.8.1
- connecting to: 127.0.0.1:27888/admin
- > show collections;
- system.indexes
- system.users
- > use foo
- switched to db foo
- > show collections
- system.indexes
- system.users
- t1
- >
可以看到root這個(gè)用戶有所有庫(kù)的操作權(quán)限, 那么user1這個(gè)用戶有什么權(quán)限呢?我們一試便知:
- [root@localhost bin]# ./mongo --port 27888 foo -uuser1 -ppwd1
- MongoDB shell version: 1.8.1
- connecting to: 127.0.0.1:27888/foo
- > show collections;
- system.indexes
- system.users
- t1
- > use test
- switched to db test
- > show collections
- Sat Apr 21 19:28:25 uncaught exception: error: {
- "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- >
通過(guò)結(jié)果我們看到, 由于user1是在foo庫(kù)里建立的用戶,所以它不具有操作其它數(shù)據(jù)庫(kù),甚至是test庫(kù)的權(quán)限。
新聞名稱:教你如何利用MySQL學(xué)習(xí)MongoDB之授權(quán)和權(quán)限
網(wǎng)站地址:http://m.fisionsoft.com.cn/article/cooipsg.html


咨詢
建站咨詢
