教你如何利用MySQL學(xué)習(xí)MongoDB之授權(quán)和權(quán)限
在上文中,我們了解了教你如何利用MySQL學(xué)習(xí)MongoDB之SQL語法,本文中我們繼續(xù)我們的學(xué)習(xí)之旅,學(xué)習(xí)兩者的授權(quán)和權(quán)限。
數(shù)據(jù)庫的安全性是每一個(gè)DBA重點(diǎn)關(guān)注的部分,在數(shù)據(jù)庫建立之后,數(shù)據(jù)的安全就顯得尤為重要。
對于一個(gè)數(shù)據(jù)庫管理員來說,安全性就意味著他必須保證那些具有特殊數(shù)據(jù)訪問權(quán)限的用戶能夠登錄到數(shù)據(jù)庫服務(wù)器,并且能夠訪問數(shù)據(jù)以及對數(shù)據(jù)庫對象實(shí)施各種權(quán)限范圍內(nèi)的操作;同時(shí),DBA還要防止所有的非授權(quán)用戶的非法操作。
1、MySQL授權(quán)和權(quán)限
MySQL中有兩種級(jí)別的權(quán)限:管理和用戶。所有權(quán)限都可分別使用 GRANT 和 REVOKE 語句授予和收回??梢允谟栌脩鬰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)限
官方文檔開啟MongoDB 服務(wù)時(shí)不添加任何參數(shù)時(shí),可以對數(shù)據(jù)庫任意操作,而且可以遠(yuǎn)程訪問數(shù)據(jù)庫,所以推薦只是在開發(fā)是才這樣不設(shè)置任何參數(shù)。如果啟動(dòng)的時(shí)候指定--auth參數(shù),可以從阻止根層面上的訪問和連接
(1)、只允許某ip訪問
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ù)庫,而admin.system.users中將會(huì)保存比在其它數(shù)據(jù)庫中設(shè)置的用戶權(quán)限更大的用戶信息。
當(dāng)admin.system.users中一個(gè)用戶都沒有時(shí),即使mongod啟動(dòng)時(shí)添加了--auth參數(shù),如果沒有在admin數(shù)據(jù)庫中添加用戶,此時(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"
- }
在admin中創(chuàng)建用戶名為root密碼為pwd2的用戶,如下:
- > 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ù)庫的用戶信息:
- > 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" }
- >
下面我們試驗(yàn)一下用戶的權(quán)限設(shè)置是否正確:
- [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é)果,說明登錄時(shí)不指定用戶名和口令時(shí)會(huì)報(bào)錯(cuò),也就是說安全性的部署生效了。下面我再看一下另一個(gè)場景:
- [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
奇怪了,我們明明指定了用戶名而且口令也沒有錯(cuò)呀,這時(shí)我們看一下系統(tǒng)日志上是否有一些有價(jià)值的信息:
auth: couldn't find user root, test.system.users
哦,原來是這樣,說明連接mongodb時(shí),如果不指定庫名,那么會(huì)自動(dòng)連接到test庫,但剛才我們新建的用戶,都不是在test庫上建立的,所以我們需要顯示指定需要連接的庫名:
- [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è)用戶有所有庫的操作權(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
- }
- >
通過結(jié)果我們看到, 由于user1是在foo庫里建立的用戶,所以它不具有操作其它數(shù)據(jù)庫,甚至是test庫的權(quán)限。
【編輯推薦】