MongoDB使用手冊_第1頁
MongoDB使用手冊_第2頁
MongoDB使用手冊_第3頁
MongoDB使用手冊_第4頁
MongoDB使用手冊_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、MongoDB使用手冊一、安裝包下載地址 2、 服務(wù)器端使用LINUX版本:mkdir -p /data/db 創(chuàng)建目錄cd /data/db 進入db目錄下 然后創(chuàng)建mkdir m_data m_log進入mongodb解壓目錄的bin目錄下,用./mongod -fork -dbpath=./data/db/m_data/ -logpath=./m_log.log -logappend -port=27017 & 啟動服務(wù)器。WINDOW版本:執(zhí)行如下命令啟動服務(wù)器:mongod -bind_ip -logpath d:datalogs -logappend -dbpa

2、th d:datadb -directoryperdb install 【注:將mongodb安裝成服務(wù),裝該服務(wù)綁定到,日志文件為d:datalogs,以及添加方式記錄。數(shù)據(jù)目錄為d:datadb。并且每個數(shù)據(jù)庫將儲存在一個單獨的目錄(-directoryperdb)】 3、 客戶端使用:LINUX版本:進入MongoDB的bin目錄下,使用./mongo進入命令行行下 然后即可進行增刪改查等日常操作。WINDOW版本:進入mongodb的bin目錄下,使用mongo.exe -host IP -port 端口連接到MongoDB服務(wù)器上4、 MongDB停止LINUX版本: 如果在shel

3、l窗口下可以直接按“CTRL+C”,如果是作為后臺服務(wù)使用時,可以先使用 ps -ef |grep mongod查詢出服務(wù)器的pid,然后使用kill -9 pid 此處的Pid為查詢出的pid。WINDOW版本: 如果在命令窗口下直接按“CTRL+C”,如果是作為系統(tǒng)服務(wù)來使用的話,需要到服務(wù)管理中停止服務(wù)或者在window管理器中終止進程。5、 web版本的統(tǒng)計服務(wù)狀態(tài) 使用http:/IP(服務(wù)器IP):端口即可6、 語法1、 基本操作db.AddUser(username,password) 添加用戶 db.auth(usrename,password) 設(shè)置數(shù)據(jù)庫連接驗證 db.cl

4、oneDataBase(fromhost) 從目標服務(wù)器克隆一個數(shù)據(jù)庫 mandHelp(name) returns the help for the command db.copyDatabase(fromdb,todb,fromhost) 復(fù)制數(shù)據(jù)庫fromdb-源數(shù)據(jù)庫名稱,todb-目標數(shù)據(jù)庫名稱,fromhost-源數(shù)據(jù)庫服務(wù)器地址 db.createCollection(name,size:3333,capped:333,max:88888) 創(chuàng)建一個數(shù)據(jù)集,相當于一個表 db.currentOp() 取消當前庫的當前操作 db.dropDataBase() 刪除當前數(shù)據(jù)庫 db.

5、eval(func,args) run code server-side db.getCollection(cname) 取得一個數(shù)據(jù)集合,同用法:dbcname or db.getCollenctionNames() 取得所有數(shù)據(jù)集合的名稱列表 db.getLastError() 返回最后一個錯誤的提示消息 db.getLastErrorObj() 返回最后一個錯誤的對象 db.getMongo() 取得當前服務(wù)器的連接對象get the server db.getMondo().setSlaveOk() allow this connection to read from then non

6、master membr of a replica pair db.getName() 返回當操作數(shù)據(jù)庫的名稱 db.getPrevError() 返回上一個錯誤對象 db.getProfilingLevel() db.getReplicationInfo() 獲得重復(fù)的數(shù)據(jù) db.getSisterDB(name) get the db at the same server as this onew db.killOp() 停止(殺死)在當前庫的當前操作 db.printCollectionStats() 返回當前庫的數(shù)據(jù)集狀態(tài) db.printReplicationInfo()db.pri

7、ntSlaveReplicationInfo() db.printShardingStatus() 返回當前數(shù)據(jù)庫是否為共享數(shù)據(jù)庫 db.removeUser(username) 刪除用戶 db.repairDatabase() 修復(fù)當前數(shù)據(jù)庫 db.resetError() db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into cmdObj:1 db.setProfilingLevel(level) 0=off,1=slow,2=all db.shutdownServer() 關(guān)閉

8、當前服務(wù)程序 db.version() 返回當前程序的版本信息 2、 數(shù)據(jù)集(表)操作返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)總數(shù) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從第二條開始的數(shù)據(jù)集 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從0到第八條的數(shù)據(jù)集 返回test數(shù)據(jù)集ID=1=的數(shù)據(jù)集從第二條到第八條的數(shù)據(jù) 返回test數(shù)據(jù)集ID=10的排序數(shù)據(jù)集 返回符合條件的一條數(shù)據(jù) 返回此數(shù)據(jù)集所屬的數(shù)據(jù)庫名稱 返回些數(shù)據(jù)集的索引信息 在數(shù)據(jù)集中刪除一條數(shù)據(jù) 重命名些數(shù)據(jù)集名稱 往數(shù)據(jù)集中插入一條數(shù)據(jù) 返回此數(shù)據(jù)集的狀態(tài) 返回此數(shù)據(jù)集的存儲大小 返回此數(shù)據(jù)集的索引文件大小返

9、回些數(shù)據(jù)集的總大小 在此數(shù)據(jù)集中更新一條數(shù)據(jù) 驗證此數(shù)據(jù)集 返回數(shù)據(jù)集共享版本號 3、 MongoDB語法與現(xiàn)有關(guān)系型數(shù)據(jù)庫SQL語法比較MongoDB語法 MySql語法 db.test.find(name:foobar) select * from test where name=foobar db.test.find() select * from test db.test.find(ID:10).count() select count(*) from test where ID=10 db.test.find().skip(10).limit(20) select * from te

10、st limit 10,20 db.test.find(ID:$in:25,35,45) select * from test where ID in (25,35,45) db.test.find().sort(ID:-1) select * from test order by ID desc db.test.distinct(name,ID:$lt:20) select distinct(name) from test where ID20 db.test.group(key:name:true,cond:name:foo,reduce:function(obj,prev)prev.ms

11、um+=obj.marks;,initial:msum:0) select name,sum(marks) from test group by name db.test.find(this.ID20,name:1) select name from test where ID20 db.test.insert(name:foobar,age:25)insert into test (name,age) values(foobar,25) db.test.remove() delete * from test db.test.remove(age:20) delete test where a

12、ge=20 db.test.remove(age:$lt:20) elete test where age20 db.test.remove(age:$lte:20) delete test where age=20 db.test.remove(age:$gt:20) delete test where age20 db.test.remove(age:$gte:20) delete test where age=20 db.test.remove(age:$ne:20) delete test where age!=20 db.test.update(name:foobar,$set:ag

13、e:36) update test set age=36 where name=foobar db.test.update(name:foobar,$inc:age:3) update test set age=age+3 where name=foobar4、7、 MongoDB主從復(fù)制介紹MongoDB的主從復(fù)制其實很簡單,就是在運行 主的服務(wù)器 上開啟mongod進程 時,加入?yún)?shù)-master即可,在運行從的服務(wù) 器上開啟mongod進程時,加入-slave 和 -source 指定主即可,這樣,在主數(shù)據(jù) 庫更新時,數(shù)據(jù)被復(fù)制到從數(shù)據(jù)庫 中(這里日志 文件 和訪問 數(shù)據(jù)時授權(quán)用戶暫時不

14、考慮 )下面我在單臺服務(wù)器上開啟2 deamon來模擬2臺服務(wù)器進行主從復(fù)制:$ mkdir m_master m_slave$mongodb/bin/mongod -port 28018 -dbpath /m_master -master &$mongodb/bin/mongod -port 28019 -dbpath /m_slave -slave -source localhost:28018 &這樣主從服務(wù)器都已經(jīng)啟動了,可以利用 netstat -an -t 查看28018、28019端口 是否開放登錄主服務(wù)器:$ mongodb/bin/mongo -port 28018url:

15、testtype help for help show dbsadminlocaltest use testswitched to db test show collections這里主上的test數(shù)據(jù)什么表都沒有,為空,查看從服 務(wù)器同樣也是這樣$ mongodb/bin/mongo -port 28019url: testtype help for help show dbsadminlocaltest use testswitched to db test show collections那么現(xiàn)在我們來驗證主從數(shù)據(jù)是否會像想象的那樣同步 呢?我們在主上新建表user db testdb.

16、createCollection(user); show collections system.indexesuser表 user已經(jīng)存在了,而且test庫中還多了一個system.indexes用來存放索引的表到從服務(wù)器上查看test庫: db test show collections system.indexesUser從 服務(wù)器的test庫中user表已經(jīng)存在,同時我還查了一下user表為空現(xiàn)在我們再來測試一下,向主服務(wù)器test庫的user表中插入一條數(shù)據(jù) show collections system.indexesuser _id : ObjectId(4b8226a997521

17、a578b7aea38), uid : 1, name : Falcon.C, age : 25 這 時我們查看從服務(wù)器的test庫user表時會多出一條記錄來: _id : ObjectId(4b8226a997521a578b7aea38), uid : 1, name : Falcon.C, age : 25 MongoDB 還有 Replica Pairs 和 Master - MasterMongoDB一般情況下都可以支持主主復(fù)制,但是在大部分情況下官方不推薦使用運行 的master - master的準備工作是:新建存放數(shù)據(jù) 庫文件 的路徑$mkdir mongodata/mm_2

18、8050 mongodata/mm_28051運行mongodb數(shù)據(jù)庫 ,一個端口 為:28050,一個為:28051$ mongodb/bin/mongod -port 28050 -dbpath /mongodata/mm_28050 -master -slave -source localhost:28051 /dev/null &$ mongodb/bin/mongod -port 28051 -dbpath mongodata/mm_28051 -master -slave -source localhost:28050 /dev/null &可以通過ps -ef|grep mongod 或 netstat -an -

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論