The NoteBook of EricKong

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

          MongoDB使用手冊(cè)

          一、安裝包下載地址

               http://www.mongodb.org/downloads

          二、服務(wù)器端使用

          LINUX版本:

          ①用tar -zxvf mongodb-linux-x86_64-1.2.4.tgz解壓開安裝包

          mkdir -p /data/db  創(chuàng)建目錄

          cd /data/db 進(jìn)入db目錄下   然后創(chuàng)建mkdir m_data m_log

          ④進(jìn)入mongodb解壓目錄的bin目錄下,用

          ./mongod -fork --dbpath=./data/db/m_data/ --logpath=./m_log.log  --logappend --port=27017 &  啟動(dòng)服務(wù)器。

          WINDOW版本:

          執(zhí)行如下命令啟動(dòng)服務(wù)器:

          mongod --bind_ip 127.0.0.1 --logpath d:\data\logs --logappend --dbpath d:\data\db  --directoryperdb –install

          【注:將mongodb安裝成服務(wù),裝該服務(wù)綁定到IP127.0.0.1,日志文件為d:\data\logs,以及添加方式記錄。數(shù)據(jù)目錄為d:\data\db。并且每個(gè)數(shù)據(jù)庫(kù)將儲(chǔ)存在一個(gè)單獨(dú)的目錄(--directoryperdb)】

          三、客戶端使用:

          LINUX版本:

          進(jìn)入MongoDBbin目錄下,使用./mongo進(jìn)入命令行行下 然后即可進(jìn)行增刪改查等日常操作。

          WINDOW版本:

          進(jìn)入mongodbbin目錄下,使用mongo.exe --host IP  --port 端口連接到MongoDB服務(wù)器上

          四、MongDB停止

          LINUX版本:

                如果在shell窗口下可以直接按“CTRL+C”,如果是作為后臺(tái)服務(wù)使用時(shí),可以先使用  ps -ef |grep mongod查詢出服務(wù)器的pid,然后使用kill -9 pid    此處的Pid為查詢出的pid

          WINDOW版本:

                 如果在命令窗口下直接按“CTRL+C”,如果是作為系統(tǒng)服務(wù)來(lái)使用的話,需要到服務(wù)管理中停止服務(wù)或者在window管理器中終止進(jìn)程。

          五、web版本的統(tǒng)計(jì)服務(wù)狀態(tài)

              使用http://IP(服務(wù)器IP:端口即可

          六、語(yǔ)法

          1、基本操作

          db.AddUser(username,password) 添加用戶

          db.auth(usrename,password) 設(shè)置數(shù)據(jù)庫(kù)連接驗(yàn)證

          db.cloneDataBase(fromhost) 從目標(biāo)服務(wù)器克隆一個(gè)數(shù)據(jù)庫(kù)

          db.commandHelp(name) returns the help for the command

          db.copyDatabase(fromdb,todb,fromhost) 復(fù)制數(shù)據(jù)庫(kù)fromdb---源數(shù)據(jù)庫(kù)名稱,todb---目標(biāo)數(shù)據(jù)庫(kù)名稱,fromhost---源數(shù)據(jù)庫(kù)服務(wù)器地址

          db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個(gè)數(shù)據(jù)集,相當(dāng)于一個(gè)表

          db.currentOp() 取消當(dāng)前庫(kù)的當(dāng)前操作

          db.dropDataBase() 刪除當(dāng)前數(shù)據(jù)庫(kù)

          db.eval(func,args) run code server-side

          db.getCollection(cname) 取得一個(gè)數(shù)據(jù)集合,同用法:db['cname'] or

          db.getCollenctionNames() 取得所有數(shù)據(jù)集合的名稱列表

          db.getLastError() 返回最后一個(gè)錯(cuò)誤的提示消息

          db.getLastErrorObj() 返回最后一個(gè)錯(cuò)誤的對(duì)象

          db.getMongo() 取得當(dāng)前服務(wù)器的連接對(duì)象get the server

          db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair

          db.getName() 返回當(dāng)操作數(shù)據(jù)庫(kù)的名稱

          db.getPrevError() 返回上一個(gè)錯(cuò)誤對(duì)象

          db.getProfilingLevel()

          db.getReplicationInfo() 獲得重復(fù)的數(shù)據(jù)

          db.getSisterDB(name) get the db at the same server as this onew

          db.killOp() 停止(殺死)在當(dāng)前庫(kù)的當(dāng)前操作

          db.printCollectionStats() 返回當(dāng)前庫(kù)的數(shù)據(jù)集狀態(tài)

          db.printReplicationInfo()

          db.printSlaveReplicationInfo()

          db.printShardingStatus() 返回當(dāng)前數(shù)據(jù)庫(kù)是否為共享數(shù)據(jù)庫(kù)

          db.removeUser(username) 刪除用戶

          db.repairDatabase() 修復(fù)當(dāng)前數(shù)據(jù)庫(kù)

          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)閉當(dāng)前服務(wù)程序

          db.version() 返回當(dāng)前程序的版本信息

          2、數(shù)據(jù)集()操作

          db.test.find({id:10}) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集

          db.test.find({id:10}).count() 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)總數(shù)

          db.test.find({id:10}).limit(2) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從第二條開始的數(shù)據(jù)集

          db.test.find({id:10}).skip(8) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從0到第八條的數(shù)據(jù)集

          db.test.find({id:10}).limit(2).skip(8) 返回test數(shù)據(jù)集ID=1=的數(shù)據(jù)集從第二條到第八條的數(shù)據(jù)

          db.test.find({id:10}).sort() 返回test數(shù)據(jù)集ID=10的排序數(shù)據(jù)集

          db.test.findOne([query]) 返回符合條件的一條數(shù)據(jù)

          db.test.getDB() 返回此數(shù)據(jù)集所屬的數(shù)據(jù)庫(kù)名稱

          db.test.getIndexes() 返回些數(shù)據(jù)集的索引信息

          db.test.group({key:...,initial:...,reduce:...[,cond:...]})

          db.test.mapReduce(mayFunction,reduceFunction,<optional params>)

          db.test.remove(query) 在數(shù)據(jù)集中刪除一條數(shù)據(jù)

          db.test.renameCollection(newName) 重命名些數(shù)據(jù)集名稱

          db.test.save(obj) 往數(shù)據(jù)集中插入一條數(shù)據(jù)

          db.test.stats() 返回此數(shù)據(jù)集的狀態(tài)

          db.test.storageSize() 返回此數(shù)據(jù)集的存儲(chǔ)大小

          db.test.totalIndexSize() 返回此數(shù)據(jù)集的索引文件大小

          db.test.totalSize() 返回些數(shù)據(jù)集的總大小

          db.test.update(query,object[,upsert_bool]) 在此數(shù)據(jù)集中更新一條數(shù)據(jù)

          db.test.validate() 驗(yàn)證此數(shù)據(jù)集

          db.test.getShardVersion() 返回?cái)?shù)據(jù)集共享版本號(hào)

          3、MongoDB語(yǔ)法與現(xiàn)有關(guān)系型數(shù)據(jù)庫(kù)SQL語(yǔ)法比較

          MongoDB語(yǔ)法 MySql語(yǔ)法

          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 test 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 ID<20

          db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) <==> select name,sum(marks) from test group by name

          db.test.find('this.ID<20',{name:1}) <==> select name from test where ID<20

          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 age=20

          db.test.remove({'age':{$lt:20}}) <==> elete test where age<20

          db.test.remove({'age':{$lte:20}}) <==> delete test where age<=20

          db.test.remove({'age':{$gt:20}}) <==> delete test where age>20

          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:{'age':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='foobar'

           

          4、 

           

          七、MongoDB主從復(fù)制介紹

          MongoDB的主從復(fù)制其實(shí)很簡(jiǎn)單,就是在運(yùn)行 主的服務(wù)器 上開啟mongod進(jìn)程 時(shí),加入?yún)?shù)--master即可,在運(yùn)行從的服務(wù) 器上開啟mongod進(jìn)程時(shí),加入--slave --source 指定主即可,這樣,在主數(shù)據(jù) 庫(kù)更新時(shí),數(shù)據(jù)被復(fù)制到從數(shù)據(jù)庫(kù)



          (這里日志 文件 和訪問(wèn) 數(shù)據(jù)時(shí)授權(quán)用戶暫時(shí)不考慮 )

          下面我在單臺(tái)服務(wù)器上開啟2 deamon來(lái)模擬2臺(tái)服務(wù)器進(jìn)行主從復(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)啟動(dòng)了,可以利用 netstat -an -t 查看2801828019端口 是否開放

          登錄主服務(wù)器:

          $ mongodb/bin/mongo --port 28018

          MongoDB shell version: 1.2.4-

          url: test

          connecting to: 127.0.0.1:28018/test

          type "help" for help

          > show dbs

          admin

          local

          test

          > use test

          switched to db test

          > show collections

          這里主上的test數(shù)據(jù)什么表都沒(méi)有,為空,查看從服 務(wù)器同樣也是這樣

          $ mongodb/bin/mongo --port 28019

          MongoDB shell version: 1.2.4-

          url: test

          connecting to: 127.0.0.1:28019/test

          type "help" for help

          > show dbs

          admin

          local

          test

          > use test

          switched to db test

          > show collections

          那么現(xiàn)在我們來(lái)驗(yàn)證主從數(shù)據(jù)是否會(huì)像想象的那樣同步 呢?

          我們?cè)谥魃闲陆ū?/span>user

          > db  

          test

          >db.createCollection("user");

          > show collections           

          system.indexes

          user

          user已經(jīng)存在了,而且test庫(kù)中還多了一個(gè)system.indexes用來(lái)存放索引的表



          到從服務(wù)器上查看test庫(kù):

          > db  

          test

          > show collections           

          system.indexes

          User

          > db.user.find();

          服務(wù)器的test庫(kù)中user表已經(jīng)存在,同時(shí)我還查了一下user表為空

          現(xiàn)在我們?cè)賮?lái)測(cè)試一下,向主服務(wù)器test庫(kù)的user表中插入一條數(shù)據(jù)

          > show collections           

          system.indexes

          user

          > db.user.insert({uid:1,name:"Falcon.C",age:25});

          > db.user.find();                               

          { "_id" : ObjectId("4b8226a997521a578b7aea38"), "uid" : 1, "name" : "Falcon.C", "age" : 25 }

          時(shí)我們查看從服務(wù)器的test庫(kù)user表時(shí)會(huì)多出一條記錄來(lái):

          > db.user.find();

          { "_id" : ObjectId("4b8226a997521a578b7aea38"), "uid" : 1, "name" : "Falcon.C", "age" : 25 }

          MongoDB 還有 Replica Pairs Master - Master

          參考地址:http://www.mongodb.org/display/DOCS/Master+Slave



          MongoDB一般情況下都可以支持主主復(fù)制,但是在大部分情況下官方不推薦使用

          運(yùn)行 master - master的準(zhǔn)備工作是:

          新建存放數(shù)據(jù) 庫(kù)文件 的路徑

          $mkdir mongodata/mm_28050 mongodata/mm_28051

          運(yùn)行mongodb數(shù)據(jù)庫(kù) ,一個(gè)端口 為:28050,一個(gè)為: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 &

          可以通過(guò)ps -ef|grep mongod netstat -an -t來(lái)檢查是否運(yùn)行功能



          測(cè)試master - master模式

          $ mongodb/bin/mongo --port 28050

          MongoDB shell version: 1.2.4-

          url: test

          connecting to: 127.0.0.1:28050/test

          type "help" for help

          > show dbs

          admin

          local

          > db

          test

          > db.user.insert({_id:1,username:"Falcon.C",age:25,sex:"M"});

          > db.user.find();

          { "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }

          > db.user.find();  //28051端口插入數(shù)據(jù)后,再來(lái)查詢,看數(shù)據(jù)是否同步

          { "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }

          { "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }

          $ mongodb/bin/mongo --port 28051

          MongoDB shell version: 1.2.4-

          url: test

          connecting to: 127.0.0.1:28051/test

          type "help" for help

          > db

          test

          > show collections         端口28050已經(jīng)新建了一個(gè)user表并插入了一條數(shù)據(jù),這里多出2

          system.indexes

          user

          > db.user.find();        //查詢表user發(fā)現(xiàn)數(shù)據(jù)已經(jīng)同步

          { "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }

          > db.user.insert({_id:2,username:"NetOne",age:24,sex:"F"});在此插入數(shù)據(jù)看數(shù)據(jù)是否雙向同步

          > db.user.find();  

          { "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }

          { "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }

          過(guò)以上開啟兩終端分別連接到2805028051端口,分別插入測(cè)試數(shù)據(jù)發(fā)現(xiàn),一切正常,正如我們所想的那樣實(shí)現(xiàn)數(shù)據(jù)的雙向同步

           

          八、 

          posted on 2014-11-19 18:28 Eric_jiang 閱讀(171) 評(píng)論(0)  編輯  收藏 所屬分類: MongoDB

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 靖远县| 崇左市| 游戏| 喜德县| 潞城市| 墨玉县| 文昌市| 灵寿县| 兴安县| 论坛| 东光县| 赤壁市| 遂平县| 靖边县| 那曲县| 米泉市| 成都市| 栖霞市| 河津市| 伊金霍洛旗| 合作市| 南康市| 五原县| 彭山县| 仁化县| 图们市| 托克逊县| 六枝特区| 观塘区| 兴化市| 雷州市| 塘沽区| 上栗县| 新宁县| 凤山市| 文化| 沈阳市| 四子王旗| 冷水江市| 甘肃省| 剑川县|