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

MongoDB基于Java、PHP的一般操作和用戶安全設(shè)置

數(shù)據(jù)庫(kù) 其他數(shù)據(jù)庫(kù) MongoDB
本文原名《NoSQL數(shù)據(jù)庫(kù):MongoDB安裝、啟動(dòng)和基于JAVA、PHP的一般操作和用戶安全設(shè)置》,實(shí)際上介紹安裝的文章已經(jīng)很多了,故截取文中“基于Java、PHP的一般操作和用戶安全設(shè)置”的部分,和大家分享。

用JAVA語(yǔ)言操作MongoDB

在官方網(wǎng)站中下載mongo.jar,并添加到項(xiàng)目中。

創(chuàng)建類MongoDBTest.java

可以使用如下兩種方式得到數(shù)據(jù)庫(kù)連接對(duì)象:

  1. Mongo m1 = new Mongo();//默認(rèn)本機(jī)連接 
  2. Mongo m2 = new Mongo("localhost"27017);//連接地址,端口號(hào) 

在創(chuàng)建連接對(duì)象之后,得到數(shù)據(jù)庫(kù):

  1. DB db = m.getDB("admin");//數(shù)據(jù)庫(kù)名稱:admin  如果數(shù)據(jù)庫(kù)不存在 則自動(dòng)創(chuàng)建  

在得到數(shù)據(jù)庫(kù)對(duì)象之后,得到表:

  1. DBCollection dbc = db.getCollection("things");//數(shù)據(jù)庫(kù)admin下的表things   如沒(méi)有此表 則自動(dòng)創(chuàng)建  

mongoDB基于JAVA語(yǔ)言的CRUD  ---

1.添加數(shù)據(jù):

  1. DBObject o = new BasicDBObject();//創(chuàng)建一個(gè)對(duì)象 
  2. o.put("name""iteye");//添加一個(gè)鍵值對(duì) 
  3. o.put("myname""xiao9");//再添加一個(gè)鍵值對(duì) 
  4.  
  5. dbc.insert(o);//插入數(shù)據(jù) 

 2.查詢數(shù)據(jù)

  1. DBCursor c = dbc.find();//查詢所有列表 
  2. List<DBObject> list = c.toArray(); 
  3. for (int i = 0; i <list.size(); i++) { 
  4.     DBObject dbo = list.get(i); 
  5.     System.out.println(dbo.toString()); 
  1. DBObject o = new BasicDBObject(); 
  2. o.put("name""iteye"); 
  3.  
  4. DBCursor c = dbc.find(o);//根據(jù)條件查詢列表   (name=iteye) 
  1. DBObject o = dbc.findOne();//查詢第一個(gè)數(shù)據(jù) 
  1. DBObject o = new BasicDBObject(); 
  2. o.put("name""iteye"); 
  3.  
  4. DBObject o = dbc.findOne(o);//根據(jù)條件查詢單個(gè)數(shù)據(jù) 

 3.修改數(shù)據(jù)

  1. DBObject queryObject = new BasicDBObject(); 
  2. queryObject.put("name""iteye"); 
  3.  
  4. DBObject obj = new BasicDBObject(); 
  5. queryObject.put("name""iteye123"); 
  6.              
  7. dbc.update(queryObject, obj);//查詢條件,要修改的值 

 4.刪除數(shù)據(jù)

  1. DBObject obj = new BasicDBObject(); 
  2. queryObject.put("name""iteye123"); 
  3.              
  4. dbc.remove(obj);//根據(jù)條件刪除數(shù)據(jù) 

用PHP語(yǔ)言操作MongoDB

  1. <?php 
  2.  
  3. //得到MongoDB連接 
  4. $m = new Mongo(); 
  5.  
  6. //選擇數(shù)據(jù)庫(kù)comedy 
  7. $db = $m->comedy; 
  8.  
  9. //選擇一個(gè)表  如沒(méi)有此表則自動(dòng)創(chuàng)建 
  10. $collection = $db->cartoons; 
  11.  
  12. //創(chuàng)建一個(gè)對(duì)象 
  13. $obj = array"title" => "Calvin and Hobbes""author" => "Bill Watterson" ); 
  14.  
  15. //插入對(duì)象到數(shù)據(jù)庫(kù) 
  16. $collection->insert($obj); 
  17.  
  18. //創(chuàng)建一個(gè)對(duì)象 
  19. $obj = array"title" => "XKCD""online" => true ); 
  20.  
  21. //插入對(duì)象到數(shù)據(jù)庫(kù) 
  22. $collection->insert($obj); 
  23.  
  24. //查詢所有該表中的對(duì)象 
  25. $cursor = $collection->find(); 
  26.  
  27. //進(jìn)行遍歷和輸出 
  28. foreach ($cursor as $obj) { 
  29.     echo $obj["title"] . "\n"
  30.  
  31.  
  32. //PHP也支持這種得到單個(gè)對(duì)象的API 
  33. $obj = $collection->findOne(); 
  34. var_dump( $obj ); 
  35.  
  36. //也可以進(jìn)行循環(huán)插入 
  37. for($i=0; $i<100; $i++) { 
  38.     $collection->insert( array"i" => $i ) ); 
  39.  
  40. //輸出表中所有數(shù)據(jù)的數(shù)量 
  41. echo $collection->count(); 
  42.  
  43. //PHP的條件查詢 
  44. $query = array"i" => 71 ); 
  45. $cursor = $collection->find( $query ); 
  46.  
  47. while$cursor->hasNext() ) { 
  48.     var_dump( $cursor->getNext() ); 
  49.  
  50. //索引的建立 
  51. $coll->ensureIndex( array"i" => 1 ) );  // create index on "i" 
  52. $coll->ensureIndex( array"i" => -1, "j" => 1 ) );  // index on "i" descending, "j" ascending 
  53.  
  54. ?>  

#p#

對(duì)于MongoDB的安全設(shè)置,用戶密碼策略

MongoDB默認(rèn)是不要求用戶名和密碼登陸的,這樣并不安全,接下來(lái)就要設(shè)置登陸賬號(hào)密碼了。

(1)控制臺(tái)設(shè)置用戶密碼和控制臺(tái)通過(guò)用戶密碼訪問(wèn)MongoDB

1. 啟動(dòng)MongoDB服務(wù)器

  1. cd d: 
  2. cd mongodb\bin 
  3. mongod --dbpath data 

2. 打開(kāi)一個(gè)新的CMD運(yùn)行

  1. cd d: 
  2. cd mongodb\bin 
  3. //打開(kāi)mongodb數(shù)據(jù)庫(kù)操作 
  4. mongo.exe 
  5. //使用admin庫(kù) 
  6. use admin; 
  7. //添加登陸賬號(hào):user1   密碼pwd1 
  8. db.addUser('user1','pwd1'); 
  9. //查看是否設(shè)置成功 
  10. //db.system.users.find(); 

3. 關(guān)閉MongoDB服務(wù)器,并使用驗(yàn)證模式 ( auth )重新啟動(dòng)

  1. cd d: 
  2. cd mongodb\bin 
  3. mongod --dbpath data --auth 

接下來(lái)在通過(guò)CMD運(yùn)行Mongodb的時(shí)候 就需要

  1. cd d: 
  2. cd mongodb\bin 
  3. mongo.exe 
  4. use admin; 
  5. //進(jìn)行登陸驗(yàn)證,如果不通過(guò),是沒(méi)有操作權(quán)限的了。 
  6. db.auth('user1','pwd1'); 

(2)JAVA方式通過(guò)用戶密碼訪問(wèn)MongoDB

  1. Mongo m = new Mongo(); 
  2.          
  3. DB db = m.getDB("admin"); 
  4.          
  5. char[] pwd_char = "pwd1".toCharArray(); 
  6.          
  7. boolean auth = db.authenticate("user1",pwd_char);//登陸驗(yàn)證,成功之后才能進(jìn)行有效操作 
  8.  
  9. if(!auth){ 
  10.     throw new RuntimeException(); 

(3)PHP方式通過(guò)用戶密碼訪問(wèn)MongoDB

  1. //PHP是直接在獲取連接對(duì)象時(shí)就進(jìn)行配置了 
  2. //mongodb://賬號(hào):密碼@連接地址 
  3.  
  4. $m = new Mongo("mongodb://user1:pwd1@localhost"); 

原文鏈接:http://xiao9.iteye.com/blog/1119003

【編輯推薦】

  1. 教你如何利用MySQL學(xué)習(xí)MongoDB
  2. 說(shuō)說(shuō)MongoDB的基礎(chǔ)
  3. 如何用Java操作MongoDB
  4. 如何用Java操作MongoDB
責(zé)任編輯:艾婧 來(lái)源: 小萌愛(ài)coding
相關(guān)推薦

2010-03-31 09:51:38

CentOS系統(tǒng)

2011-07-12 16:27:13

PHP

2020-06-09 08:09:07

機(jī)器學(xué)習(xí)統(tǒng)計(jì)學(xué)習(xí)無(wú)監(jiān)督學(xué)習(xí)

2009-12-16 10:52:47

PSP無(wú)線路由設(shè)置

2020-04-26 17:04:18

Python代碼數(shù)據(jù)

2009-11-13 09:49:49

Linux耗電Linux調(diào)控器

2018-01-08 15:07:15

java項(xiàng)目后臺(tái)

2011-09-08 11:35:18

2010-10-08 14:23:08

MySQL中INSER

2017-08-31 14:09:26

數(shù)據(jù)庫(kù)MySQLSQL優(yōu)化

2024-03-01 09:03:49

LinkedLisJavaList

2024-02-22 08:59:41

JavaArrayListLinkedList

2021-08-19 09:16:29

MySQL數(shù)據(jù)庫(kù)優(yōu)化器

2021-08-16 08:42:31

MySQL查詢數(shù)據(jù)庫(kù)

2009-11-23 09:45:46

CentOSLinux

2010-09-14 10:55:14

DIV CSS網(wǎng)頁(yè)制作

2019-06-05 15:23:09

Redis緩存存儲(chǔ)

2012-06-27 09:29:49

程序員

2022-04-02 15:08:54

API調(diào)試

2011-04-06 16:41:25

LCPPPPIPCP
點(diǎn)贊
收藏

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