安裝步驟:
1、將下載的mongodb壓縮包解壓到對應文件夾,我的是:D:\softInstall\mongodb,請注意,我的bin目錄是:D:\softInstall\mongodb\bin,注意自己的bin目錄是否和我的相同
2、配置(請用管理員權(quán)限打開cmd,并切換到mongodb安裝的文件夾,如:cd /d D:\softInstall\mongodb\bin):
1)創(chuàng)建一個目錄用于保存MongoDB的日志文件:md D:\softInstall\mongodb\log,然后增加配置:echo logpath=D:\softInstall\mongodb\log\mongo.log > D:\softInstall\mongodb\mongod.cfg
2)創(chuàng)建數(shù)據(jù)庫和日志文件的目錄:md D:\softInstall\mongodb\data\db,然后增加配置:mongod --config D:\softInstall\mongodb\mongod.cfg --dbpath D:\softInstall\mongodb\data\db\ --install
3、安裝和運行mongodb:
1)安裝mongodb服務:mongod.exe --config D:\softInstall\mongodb\mongod.cfg --install
2)運行mongodb:net start mongodb(停止命令為:net stop mongodb)
3)連接到mongodb:mongo
4)創(chuàng)建用戶:切換到admin:use admin,創(chuàng)建用戶:db.addUser("admin","admin"); 查看用戶:db.system.users.find();(注意:mongodb的語句后要加 ; 號)
5)查看數(shù)據(jù)庫:show dbs
至此,mongodb安裝完成,可以在可視化工具如MongoBooster中通過localhost連接到本地數(shù)據(jù)庫
PS:
1、安裝過程中可能出現(xiàn)的錯誤:
1)net start mongodb出錯:服務無法啟動,發(fā)生系統(tǒng)錯誤1067。請打開配置文件D:\softInstall\mongodb\mongod.cfg,看下是否加上配置:dbpath=D:\softInstall\mongodb\data\db\,沒有,請手動加上。
2)通過工具連接,MongoError:auth fails錯誤:請在對應的集合加上用戶,安裝步驟3已經(jīng)給出了切換數(shù)據(jù)庫和創(chuàng)建用戶的命令
2、MongoDB查詢語句:
1)db.collection.drop(); // 刪除集合
2)db.messageDetail.find({}); // 查詢集合
3)db.messageDetail.find({"status":4}); // 根據(jù)字段查詢
4)db.messageDetail.find({"status":{ $ne : 4 }}); //?"$lt", "$lte", "$gt", "$gte", "$ne"對應于"<", "<=", ">", ">=","!="
5)db.messageDetail.find({"createTime" : {$lte:new Date(2017,10,26)}}); // 時間查詢
6)db.messageDetail.find({"$where":function () {?if(this.count > 0) return true;}}); // 函數(shù)查詢
7)db.messageLogs.find({}).sort({"createTime":-1}); // 查詢結(jié)果排序
8)db.messageDetail.insert({_id:"terast",type:"type1",system:"test"}); // 插入
daemonize 設置yes或者no區(qū)別
daemonize:yes:redis采用的是單進程多線程的模式。當redis.conf中選項daemonize設置成yes時,代表開啟守護進程模式。在該模式下,redis會在后臺運行,并將進程pid號寫入至redis.conf選項pidfile設置的文件中,此時redis將一直運行,除非手動kill該進程。
daemonize:no: 當daemonize選項設置成no時,當前界面將進入redis的命令行界面,exit強制退出或者關(guān)閉連接工具(putty,xshell等)都會導致redis進程退出。
啟動 : mongod -dbpath 你的數(shù)據(jù)庫目錄