自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你如何利用MySQL學(xué)習(xí)MongoDB之授權(quán)和權(quán)限

數(shù)據(jù)庫 其他數(shù)據(jù)庫 MySQL MongoDB
在上文中,我們了解了教你如何利用MySQL學(xué)習(xí)MongoDB之SQL語法,本文中我們繼續(xù)我們的學(xué)習(xí)之旅,學(xué)習(xí)兩者的授權(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的用戶,如下:

  1. [root@localhost bin]# ./mongo --port 27888   
  2. MongoDB shell version: 1.8.1   
  3. connecting to: test   
  4. > use foo   
  5. switched to db foo   
  6. > db.addUser("user1","pwd1")   
  7. {   
  8. "user" : "user1",   
  9. "readOnly" : false,   
  10. "pwd" : "35263c100eea1512cf3c3ed83789d5e4"   
  11. }  

 

[[29688]]

在admin中創(chuàng)建用戶名為root密碼為pwd2的用戶,如下:[[29688]]

 

  1. > use admin   
  2. switched to db admin   
  3. > db.addUser("root""pwd2")   
  4. {   
  5. "_id" : ObjectId("4f8a87bce495a88dad4613ad"),   
  6. "user" : "root",   
  7. "readOnly" : false,   
  8. "pwd" : "20919e9a557a9687c8016e314f07df42"   
  9. }   
  10. > db.auth("root""pwd2")   
  11. 1   
  12. >  

 

如果認(rèn)證成功會(huì)顯示1, 用以下命令可以查看特定的數(shù)據(jù)庫的用戶信息:[[29688]]

  1. > use admin   
  2. switched to db admin   
  3. > db.system.users.find();   
  4. "_id" : ObjectId("4f8a87bce495a88dad4613ad"), "user" : "root""readOnly" : false"pwd" : "20919e9a557a9687c8016e314f07df42" }   
  5. > use foo   
  6. switched to db foo   
  7. > db.system.users.find();   
  8. "_id" : ObjectId("4f92966d77aeb2b2e730c1bb"), "user" : "user1""readOnly" : false"pwd" : "35263c100eea1512cf3c3ed83789d5e4" }   
  9. >  

 

下面我們試驗(yàn)一下用戶的權(quán)限設(shè)置是否正確:[[29688]]

  1. [root@localhost bin]# ./mongo --port 27888   
  2. MongoDB shell version: 1.8.1   
  3. connecting to: 127.0.0.1:27888/test   
  4. > use foo   
  5. switched to db foo   
  6. > db.system.users.find();   
  7. error: {   
  8. "$err" : "unauthorized db:foo lock type:-1 client:127.0.0.1",   
  9. "code" : 10057   
  10. }   
  11. > use admin   
  12. switched to db admin   
  13. > db.system.users.find();   
  14. error: {   
  15. "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",   
  16. "code" : 10057   
  17. }   
  18. >  

 

通知以上實(shí)驗(yàn)結(jié)果,說明登錄時(shí)不指定用戶名和口令時(shí)會(huì)報(bào)錯(cuò),也就是說安全性的部署生效了。下面我再看一下另一個(gè)場景:

  1. [root@localhost bin]# ./mongo --port 27888 -uroot -ppwd2   
  2. MongoDB shell version: 1.8.1   
  3. connecting to: 127.0.0.1:27888/test   
  4. Sat Apr 21 19:23:15 uncaught exception: login failed   
  5. exception: login failed  

 

奇怪了,我們明明指定了用戶名而且口令也沒有錯(cuò)呀,這時(shí)我們看一下系統(tǒng)日志上是否有一些有價(jià)值的信息:

auth: couldn't find user root, test.system.users

 

哦,原來是這樣,說明連接mongodb時(shí),如果不指定庫名,那么會(huì)自動(dòng)連接到test庫,但剛才我們新建的用戶,都不是在test庫上建立的,所以我們需要顯示指定需要連接的庫名:

  1. [root@localhost bin]# ./mongo --port 27888 admin -uroot -ppwd2   
  2. MongoDB shell version: 1.8.1   
  3. connecting to: 127.0.0.1:27888/admin   
  4. > show collections;   
  5. system.indexes   
  6. system.users   
  7. > use foo   
  8. switched to db foo   
  9. > show collections   
  10. system.indexes   
  11. system.users   
  12. t1   
  13. >  

 

可以看到root這個(gè)用戶有所有庫的操作權(quán)限, 那么user1這個(gè)用戶有什么權(quán)限呢?我們一試便知:

  1. [root@localhost bin]# ./mongo --port 27888 foo -uuser1 -ppwd1   
  2. MongoDB shell version: 1.8.1   
  3. connecting to: 127.0.0.1:27888/foo   
  4. > show collections;   
  5. system.indexes   
  6. system.users   
  7. t1   
  8. > use test   
  9. switched to db test   
  10. > show collections   
  11. Sat Apr 21 19:28:25 uncaught exception: error: {   
  12. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",   
  13. "code" : 10057   
  14. }   
  15. >  

 

通過結(jié)果我們看到, 由于user1是在foo庫里建立的用戶,所以它不具有操作其它數(shù)據(jù)庫,甚至是test庫的權(quán)限。

 

【編輯推薦】

  1. 教你如何利用MySQL學(xué)習(xí)MongoDB之SQL語法
  2. 教你如何利用MySQL學(xué)習(xí)MongoDB之?dāng)?shù)據(jù)存儲(chǔ)結(jié)構(gòu)
  3. 如何解決PHP+MySQL出現(xiàn)亂碼的現(xiàn)象
  4. 教你如何利用MySQL學(xué)習(xí)MongoDB之安裝篇
  5. MySQL配置時(shí)提示無法連接到MySQL本地服務(wù)器
責(zé)任編輯:艾婧 來源: it168
相關(guān)推薦

2011-05-24 10:11:30

MySQLMongoDB

2011-05-24 09:51:07

MySQLMongoDB

2011-05-23 09:23:19

MySQLMongoDB

2011-05-24 09:10:24

MySQLMongoDB

2011-05-23 13:30:00

MySQLMongoDB

2023-12-01 10:21:00

機(jī)器學(xué)習(xí)算法

2011-09-14 15:30:00

MongoDB

2023-11-02 13:34:00

云計(jì)算聯(lián)合學(xué)習(xí)

2011-03-31 10:52:13

2023-03-20 07:48:03

2020-11-27 07:38:43

MongoDB

2023-08-17 14:22:17

深度學(xué)習(xí)機(jī)器學(xué)習(xí)

2016-11-11 11:11:25

2013-02-20 10:01:29

Wireshark監(jiān)測無線網(wǎng)絡(luò)

2011-08-01 09:26:51

Xcode Xcode 4 Instrument

2019-10-15 14:53:23

MongoDBMySQL數(shù)據(jù)庫

2024-11-20 16:12:31

Python圖像處理計(jì)算機(jī)視覺

2020-12-25 15:24:24

人工智能

2010-05-12 16:58:59

IIS Web

2011-03-28 13:29:22

MongoDB索引用法效率分析
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)