今天開始做MongoDB的學習總結,感覺MongoDB的也不是想象中那麼難,不過如果熟悉javascript的話,學習MongoDB就更容易上手。
首先是對MongoDB使用者和許可權的設定,如果不設定使用者的話,直接使用mongo命令就可以進入用戶端shell介面進行操作了,但是如果沒有設定使用者的話,總感覺少了點什麼,於是經過半天的尋找和實踐,差不多把使用者和許可權弄明白了。總結如下:
如果按照以下這個指令安裝的話:mongod --install --dbpath "C:\Program Files\mongodb\data\db" --logpath "C:\Program
Files\mongodb\data\log\MongoDB.log"
如下:c:\Program Files\mongodb\bin>mongod --install --dbpath "C:\Program Files\mongodb\data\db" --logpath "C:\Program Files\mongodb\data\log\MongoDB.log"Fri Apr 05 13:47:43.164Fri Apr 05 13:47:43.168 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.Fri Apr 05 13:47:43.169Fri Apr 05 13:47:43.169 Trying to install Windows service 'MongoDB'Fri Apr 05 13:47:43.170 There is already a service named 'MongoDB', abortingc:\Program Files\mongodb\bin>net start MongoDBMongo DB 服服務務已已經經啟啟動動成成功功。。c:\Program Files\mongodb\bin>mongoMongoDB shell version: 2.4.1connecting to: testServer has startup warnings:Fri Apr 05 13:48:02.516 [initandlisten]Fri Apr 05 13:48:02.516 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.Fri Apr 05 13:48:02.516 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).Fri Apr 05 13:48:02.516 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.Fri Apr 05 13:48:02.516 [initandlisten] ** See http://dochub.mongodb.org/core/32bitFri Apr 05 13:48:02.516 [initandlisten]> show dbsadmin (empty)local 0.03125GB>
可以看到有兩個預設的資料庫admin和local
執行個體1:建立一個使用者名稱為root,密碼為admin的使用者,如下:> use adminswitched to db admin> db.addUser("root","admin"){ "user" : "root", "readOnly" : false, "pwd" : "bde0d84f6749c235a6b4e36d945eb666", "_id" : ObjectId("515e662430d89f61f6991c91")}> show collectionsFri Apr 05 13:50:36.685 JavaScript execution failed: error: { "$err" : "not authorized for query on admin.system.namespaces", "code" : 16550} at src/mongo/shell/query.js:L128>
說明:使用以上指令show collections的時候,發現報錯了。是因為沒有許可權。做如下操作:> db.auth("root","admin");1
說明:返回1表示驗證成功了,返回0表示驗證失敗。此時,輸入以下指令:show collections則可以看到admin下的集合了。> show collectionssystem.indexessystem.users> db.system.users.find(){ "_id" : ObjectId("515e662430d89f61f6991c91"), "user" : "root", "readOnly" : false, "pwd" : "bde0d84f6749c235a6b4e36d945eb666" }>
執行個體2:在使用者名稱為root,密碼為admin的使用者下建立一個student資料庫,並在student資料庫中建立一個stu的集合并插入一個文檔,如下:> use studentswitched to db student> db.stu.insert({"name":"maoyuanjun","age":25,"sex":"male"})> db.stu.find(){ "_id" : ObjectId("515e676630d89f61f6991c92"), "name" : "maoyuanjun", "age" : 25, "sex" : "male" }
退出伺服器,重新登陸如下:c:\Program Files\mongodb\bin>mongoMongoDB shell version: 2.4.1connecting to: test> show dbsFri Apr 05 13:58:05.420 JavaScript execution failed: listDatabases failed:{ "ok" : 0, "errmsg" : "unauthorized" } at src/mongo/shell/mongo.js:L46
出錯原因:是沒有許可權。則先判斷是否有許可權,如下:> use adminswitched to db admin> db.auth("root","admin")1> show dbsadmin 0.0625GBlocal 0.03125GBstudent 0.0625GB>這時,我們可以看到使用者root,密碼admin的使用者下多了一個student資料庫。
執行個體3:在建立一個使用者c:\Program Files\mongodb\bin>mongoMongoDB shell version: 2.4.1connecting to: test> use adminswitched to db admin
直接添加使用者會報錯:> db.addUser("test","123456")Fri Apr 05 14:01:31.404 JavaScript execution failed: error: { "$err" : "not authorized for query on admin.system.users", "code" : 16550 } at src/mongo/shell/query.js:L128
做許可權的判斷:> db.auth("root","admin");1> use adminswitched to db admin> db.addUser("test",123456){ "user" : "test", "readOnly" : false, "pwd" : "c8ef9e7ab00406e84cfa807ec082f59e", "_id" : ObjectId("515e68e2be252e81c5dee198")}> show collectionssystem.indexessystem.users> db.system.users.find(){ "_id" : ObjectId("515e662430d89f61f6991c91"), "user" : "root", "readOnly" : false, "pwd" : "bde0d84f6749c235a6b4e36d945eb666" }{ "_id" : ObjectId("515e68e2be252e81c5dee198"), "user" : "test", "readOnly" : false, "pwd" : "c8ef9e7ab00406e84cfa807ec082f59e" }>可以看到已經有兩個賬戶了。以上建立好使用者之後,就可以使用java代碼來判斷串連是否成功了:
package com.MongoDBDemo;import java.net.UnknownHostException;import com.mongodb.DB;import com.mongodb.Mongo;import com.mongodb.MongoException;/* * MongoDB的串連和關閉 */public class MongoDBUtil {public static void main(String[] args) {Mongo m = null;DB db = null;try {//Mongo(param1, param2):param1為IP地址 param2為連接埠m = new Mongo("127.0.0.1", 27017);//根據mongodb資料庫的名稱擷取mongodb對象db = m.getDB("admin");//校正使用者密碼是否正確 if (!db.authenticate("root", "admin".toCharArray())){ System.out.println("串連MongoDB資料庫,校正失敗!"); }else{ System.out.println("串連MongoDB資料庫,校正成功!"); }} catch (UnknownHostException e) {e.printStackTrace();} catch (MongoException e) {e.printStackTrace();}finally{//mongodb串連關閉try{m.close();} catch(Exception ee) {ee.printStackTrace();}m = null;}}}