1,、基本操作 db.AddUser(username,password)
添加用戶
db.auth(usrename,password) 設(shè)置數(shù)據(jù)庫連接驗(yàn)證
db.cloneDataBase(fromhost) 從目標(biāo)服務(wù)器克隆一個(gè)數(shù)據(jù)庫
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost)
復(fù)制數(shù)據(jù)庫fromdb---源數(shù)據(jù)庫名稱,,todb---目標(biāo)數(shù)據(jù)庫名稱,fromhost---源數(shù)據(jù)庫服務(wù)器地址
db.createCollection(name,{size:3333,capped:333,max:88888})
創(chuàng)建一個(gè)數(shù)據(jù)集,,相當(dāng)于一個(gè)表
db.currentOp() 取消當(dāng)前庫的當(dāng)前操作
db.dropDataBase() 刪除當(dāng)前數(shù)據(jù)庫
db.eval_r(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ò)誤的對象
db.getMongo() 取得當(dāng)前服務(wù)器的連接對象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ù)庫的名稱
db.getPrevError() 返回上一個(gè)錯(cuò)誤對象
db.getProfilingLevel()
db.getReplicationInfo() 獲得重復(fù)的數(shù)據(jù)
db.getSisterDB(name) get the db at the same server as this
onew
db.killOp() 停止(殺死)在當(dāng)前庫的當(dāng)前操作
db.printCollectionStats() 返回當(dāng)前庫的數(shù)據(jù)集狀態(tài)
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當(dāng)前數(shù)據(jù)庫是否為共享數(shù)據(jù)庫
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復(fù)當(dāng)前數(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)閉當(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ù)庫名稱
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ù)集的存儲大小
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ù)集共享版本號
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 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í)很簡單,就是在運(yùn)行 主的服務(wù)器 上開啟mongod進(jìn)程 時(shí),,加入?yún)?shù)--master即可,,在運(yùn)行從的服務(wù)
器上開啟mongod進(jìn)程時(shí),加入--slave 和 --source 指定主即可,,這樣,,在主數(shù)據(jù) 庫更新時(shí),數(shù)據(jù)被復(fù)制到從數(shù)據(jù)庫
中
(這里日志 文件 和訪問 數(shù)據(jù)時(shí)授權(quán)用戶暫時(shí)不考慮 )
下面我在單臺服務(wù)器上開啟2 deamon來模擬2臺服務(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)啟動了,,可以利用 netstat -an -t 查看28018,、28019端口
是否開放
登錄主服務(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ù)什么表都沒有,為空,,查看從服 務(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)在我們來驗(yàn)證主從數(shù)據(jù)是否會像想象的那樣同步 呢,?
我們在主上新建表user
> db
test
>db.createCollection("user");
> show
collections
system.indexes
user
>
表 user已經(jīng)存在了,而且test庫中還多了一個(gè)system.indexes用來存放索引的表
到從服務(wù)器上查看test庫:
> db
test
> show
collections
system.indexes
User
> db.user.find();
>
從 服務(wù)器的test庫中user表已經(jīng)存在,,同時(shí)我還查了一下user表為空
現(xiàn)在我們再來測試一下,,向主服務(wù)器test庫的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庫user表時(shí)會多出一條記錄來:
> db.user.find();
{ "_id" : ObjectId("4b8226a997521a578b7aea38"), "uid" : 1, "name" :
"Falcon.C", "age" : 25 }
>
MongoDB 還有 Replica Pairs 和 Master - Master
參考地址:http://www./display/DOCS/Master+Slave
MongoDB一般情況下都可以支持主主復(fù)制,,但是在大部分情況下官方不推薦使用
運(yùn)行 的master - master的準(zhǔn)備工作是:
新建存放數(shù)據(jù) 庫文件 的路徑
$mkdir mongodata/mm_28050 mongodata/mm_28051
運(yùn)行mongodb數(shù)據(jù)庫 ,一個(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 &
可以通過ps -ef|grep mongod 或 netstat -an -t來檢查是否運(yùn)行功能
測試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ù)后,,再來查詢,看數(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" }
>
通
過以上開啟兩終端分別連接到28050,、28051端口,分別插入測試數(shù)據(jù)發(fā)現(xiàn),,一切正常,,正如我們所想的那樣實(shí)現(xiàn)數(shù)據(jù)的雙向同步
|