(1)对于初学者,要善于使用help命令,然后看命令注释,helpdb.help()helpondbmethodsdb.mycoll.help()helponcollectionmethodsrs.help()helponreplicasetmethodshelpconnectconnectingtoadbhelphe
(1)对于初学者,要善于使用help命令,然后看命令注释,
> help
db.help() help
on db methods
db.mycoll.help() help
on collection methods
rs.help() help
on replica set methods
help
connect connecting
to a db help
help
admin administrative
help
help
misc misc
things to know
show
dbs show
database names
show
collections show
collections in current database
show
users show
users in current database
show
profile show
most recent system.profile entries with time >= 1ms
use
set
current database
db.foo.find() list
objects in collection foo
db.foo.find( { a : 1 }
) list objects in foo where a == 1
it result
of the last line evaluated; use to further iterate
exit quit
the mongo shell
(2)一个服务器上可以包含多个数据库(dbs),一个db包含多个集合(collections),此结构与mysql非常类似,当然在关系型数据库mysql中,是不存在集合的概念的,而mongodb中db
,collection都是隐式创建的
> show dbs //列出当前服务器的所有数据库
admin
local
richinfo
test
> use test1 //切换数据库
switched to db test1
> db //显示当前数据库
test1
> show collections //列出当前数据库中所有的集合
>
db.ee_info.save({name:"gabriel",position:"DBA",age:29,sex:"F"});
> show collections
ee_info
system.indexes
> show dbs
admin
local
richinfo
test
test1
以上隐式创建了一个test1 的db; ee_info的集合。
(3)
mongodb的数据库复制功能db.copyDatabase() 可以在local和remote服务器上进行数据库的复制,db.cloneDatabase()也可以实现remote数据库的复制。
> show dbs
admin
local
richinfo
test
test1
> db.copyDatabase("test1","test2");
{ "ok" : 1 }
> show dbs
admin
local
richinfo
test
test1
test2
> use test2
switched to db test2
> db.ee_info.find();
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" :
"gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
> db.dropDatabase();
{ "dropped" : "test2", "ok" : 1 }
> show dbs
admin
local
richinfo
test
test1
(4) 使用getSisterDB()获取其他数据库的引用,一般情况下我们使用use语句可以实现数据库的切换,但是如果我们在current
database A 操作database B中的集合,getSisterDB()就能很好的引用
> use test
switched to db test
> db
test
> test1.ee_info.find();
Thu Jul 7 16:21:41 ReferenceError: test1 is not defined
(shell):0
> test1=db.getSisterDB("test1");
test1
> test1.ee_info.find();
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" :
"gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
> db
Test
(4)作为DBA可以使用fsync强制性的将内存中的数据写于数据文件。Asymc表示异步写
> db.runCommand({fsync:1});
{ "errmsg" : "access denied; use admin db", "ok" : 0 }
> use admin
switched to db admin
> db.runCommand({fsync:1});
{ "numFiles" : 8, "ok" : 1 }
> db.runCommand({fsync:1,async:true});
{ "numFiles" : 8, "ok" : 1 }
(5)作为DBA有时为了备份,整理数据库,需要将系统锁定,mongodb的锁定模式也不进行读阻塞。以下用3个session模拟锁机制
Session1 锁定操作:
> db
admin
> db.copyDatabase("test1","test2");
{ "ok" : 1 }
> show dbs
admin
local
richinfo
test
test1
test2
> db.runCommand({fsync:1,lock:1});
{
"info" : "now locked
against writes, use db.$cmd.sys.unlock.findOne() to unlock",
"ok" : 1
}
>
Session2:
[mongodb@tes102 ~]$
cd /soft/mongodb-linux-x86_64-1.6.5/bin/
[mongodb@tes102 bin]$ ./mongo 192.168.18.102:10001
MongoDB shell version: 1.6.5
connecting to: 192.168.18.102:10001/test
> use test1
switched to db test1
> db.ee_info.find(); //读没有阻塞
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" :
"gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
>
db.ee_info.save({name:"wangxiangtao",age:23}); //写已经产生等待
Session3
[root@tes102 ~]# su - oracle
su: user oracle does not exist
[root@tes102 ~]# su - mongodb
[mongodb@tes102 ~]$ cd /soft/mongodb-linux-x86_64-1.6.5
[mongodb@tes102 mongodb-linux-x86_64-1.6.5]$ cd bin/
[mongodb@tes102 bin]$ ./mongo 192.168.18.102:10001
MongoDB shell version: 1.6.5
connecting to: 192.168.18.102:10001/test
> use test2
switched to db test2
> db.ee_info.find();
^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A // 直接等待
-----------------------在session1 直接解锁,session2,session3均恢复正常
> db.$cmd.sys.unlock.findOne();
{ "ok" : 1, "info" : "unlock requested" }
>
在session3中查找数据:
> test2=db.getSisterDB("test2");
test2
> db.ee_info.find();
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" :
"gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
{ "_id" : ObjectId("4e1571770a2bdd1c776e5239"), "name" :
"wangxiangtao", "age" : 23 }
> test1=db.getSisterDB("test1");
test1
> db.ee_info.find();
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" :
"gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
{ "_id" : ObjectId("4e1571770a2bdd1c776e5239"), "name" :
"wangxiangtao", "age" : 23 }
>
(5)验证集合的正确性:
> show collections
system.indexes
tt
> db.tt.validata();
Thu Jul 7 17:22:02 TypeError: db.tt.validata is not a
function (shell):0
> db.tt.validate();
{
"ns" : "blog.tt",
"result" :
"\nvalidate\n firstExtent:0:2500
ns:blog.tt\n lastExtent:0:2500 ns:blog.tt\n #
extents:1\n datasize?:52 nrecords?:1
lastExtentSize:3328\n padding:1\n first
extent:\n loc:0:2500 xnext:null
xprev:null\n nsdiag:blog.tt\n size:3328
firstRecord:0:25b0 lastRecord:0:25b0\n 1 objects found,
nobj:1\n 68 bytes data w/headers\n 52 bytes data
wout/headers\n deletedList:
0000000100000000000\n deleted: n: 1 size:
3084\n nIndexes:1\n blog.tt.$_id_
keys:1\n",
"ok" : 1,
"valid" : true,
"lastExtentSize" :
3328
}
>
总结:mongodb与oracle mysql比起来,差距还是蛮大的,特别从lock的测试中可以看出,一个会话等待,另外一个会话的读也阻塞了。
路漫漫,生活继续,工作继续………………………………….