狠狠撸

狠狠撸Share a Scribd company logo
MongoDB: Introduction
内容
? 介绍
? 安装
? 启动、停止
? 插入、删除、更新、查找
? 聚合、Map-Reduce、索引
? 高级主题:复制、分片
? 管理
内容
? 介绍
? 安装
? 启动、停止
? 插入、删除、更新、查找
? 聚合、Map-Reduce、索引
? 高级主题:复制、分片
? 管理
Created By
open source
document database
leading NoSQL database
Written in C++
具有众多狈叠的特性
? Document-Oriented Storage
? Full Index Support
? Replication & High Availability
? Auto-Sharding
? Rich, document-based queries.
? Atomic modifiers for contention-
free performance.
? Map/Reduce
? ……
支持几乎所有主流编程语言
http://www.mongodb.org/display/DOCS/Drivers
内容
? 介绍
? 安装
? 启动、停止
? 插入、删除、更新、查找
? 聚合、Map-Reduce、索引
? 高级主题:复制、分片
? 管理
? http://www.mongodb.org/downloads
? Linux Binary 安装:
–wget http://fastdl.mongodb.org/linux/mongodb-
linux-x86_64-2.4.1.tgz
–tar zxvf mongodb-linux-x86_64-2.4.1.tgz
–mv mongodb-linux-x86_64-2.4.1 mongodb
–cd mongodb:
–cd bin:
? 核心进程
?mongod: MongoDB系统的主要进程,它用于管理
数据格式,处理数据操作的请求,执行后台管理
操作。
?mongos: mongodb分片中使用,路由使用。
?mongod: JavaScript shell interface, 提供
了一个强大的系统管理等命令,并且可以查询和
操作数据库。
内容
? 介绍
? 安装
? 启动、停止
? 插入、删除、更新、查找
? 聚合、Map-Reduce、索引
? 高级主题:复制、分片
? 管理
? mongod
–mongodb的主进程,启动时支持众多参数,
? --config <filename>
? --port <port>
? --fork
? ……
? 示例:
mongod --port 27017 --fork –logpath
/srv/mongodb/mongodb.log --dbpath /srv/mongodb/
? 详细参数:
http://docs.mongodb.org/manual/reference/mongod/
? Stop mongodb:
–./mongo --host xxx --port yyy 登陆;
–然后运行:
? use admin;
? db.shutdownServer();
–也可以找到 mongod 进程,kill -2 PID
Database == Database
? > show dbs
admin (empty)
local 25.94140625GB
test 29.939453125GB
? > use test
switched to db test
? > show collections
system.indexes
test0
test1
test2
Collection == Table
? >use test
? >db.test_0.insert({name:"qfdai2"})
? > db.test_0.find()
{ "_id" :
ObjectId("5178e9372ae21fe1fb129a32"),
"name" : "qfdai2" }
? > db.test_0.count()
1
Document == Row
{"_id" :
ObjectId("4be97eaebcd1b30e86000003"),
"title" : "Ordered List",
"creator_id" :
ObjectId("4be97eadbcd1b30e86000001"),
"memberships" : [
ObjectId("4be97eadbcd1b30e86000001"),
ObjectId("4be97eaebcd1b30e86000002")
]
}
内容
? 介绍
? 安装
? 启动、停止
? 插入、删除、更新、查找
? 聚合、Map-Reduce、索引
? 高级主题:复制、分片
? 管理
SQL:INSERT into
tbl_name[(column_name, …)]
values(value, …)
? 插入
db = Connection(host="192.168.86.14", port=27017).test
mycollection = db.test_0
mycollection.insert({"color":"red", "size":100})
mycollection.insert({"color":"green", "size":120})
mycollection.insert({"color":"blue", "size":110})
mycollection.insert({"color":"purple", "size":140})
mycollection.insert({"color":"orange", "size":150})
mycollection.insert({"color":"yellow", "size":90})
? 批量插入更有效(批量插入数-消耗时间)
SQL: DELETE FROM tbl_name [WHERE
where_definition] [ORDER BY
...] [LIMIT row_count]
DROP TABLE tbl_name;
? 删除
mycollection.remove({"color":“red“})
? 全表删除
collection.remove()
db.drop_collection("mycollection")
对一百万个数据的集合,操作时间对比:
collection.remove(): 18733ms
db.drop.collection("mycollection“): 1029ms
SQL: UPDATE tbl_name SET
col_name1=expr1 [,
col_name2=expr2 ...] [WHERE
where_definition] [ORDER BY
...] [LIMIT row_count]
? 更新
db.collection.update( <query>, <update>, <options> )
<query>:对应 where 语句;
<update>:对应 SET语句;
默认的更新只更新一个,如果更新多个,需要添加 options。
MongoDB single update:
– mycollection.insert({"color":"yellow", "size":90})
– mycollection.insert({"color":"yellow", "size":30})
– mycollection.update({"color":"yellow"}, {"$set": {"size":120}})
结果
? 更新
MongoDB multi update:
– mycollection.insert({"color":"yellow", "size":90})
– mycollection.insert({"color":"yellow", "size":30})
– mycollection.update({"color":"yellow"}, {"$set": {"size":120}},
multi=True)
MongoDB upsert: 如果没找到满足条件的,则插入
– mycollection.update({"color":"zzz"}, {"$set": {"size":120}},
upsert=True)
结果
结果
? 更新—更多的修改器
? 操作字段
– $inc, $rename,$set,$setOnInsert,$unset
? 操作数组
– $addToSet,$pop,$pullAll,$pull,$pushAll,
– $push,$each,$slice,$sort
?更新—操作字段
? $inc:对某个字段增加值
– mycollection.insert({"color":"yellow", "size":90})
– mycollection.insert({"color":"blue", "size":30})
– mycollection.update({"color":"blue"}, {"$inc":{"size":40}})
? $unset:删除某个字段
– mycollection.insert({"color":"blue", "size":30})
– mycollection.update({"color":"blue"}, {"$unset":{"color":1}})
结果
结果
?更新—操作字段
?$rename:修改字段的名称
–mycollection.insert({"color":"blue", "size":30})
–mycollection.insert({"color":"yellow", "size":30})
–mycollection.update({"color":"blue"},
{"$rename":{"color":"mycolor"}})
–mycollection.update({"color":"yellow"},
{"$rename":{"color":"size"}})
结果
?更新—操作数组
? $addToSet:将某个值放到数组中,仅当数组中不存在才放入。
– mycollection.insert({"color":"blue", "myset":['a','c','b']})
– mycollection.update({"color":"blue"}, {"$addToSet":{"myset":'a'}})
– mycollection.update({"color":"blue"}, {"$addToSet":{"myset":'b'}})
– mycollection.update({"color":"blue"}, {"$addToSet":{"myset":'e'}})
? 加入多个
– mycollection.insert({"color":"blue", "myset":['a','c','b']})
– mycollection.update({"color":"blue"}, {"$addToSet":{"myset": {"$each":
['e','b','f']}}})
{
"_id" : ObjectId("5179e19b8a267c22d8f30b9d"),
"color" : "blue",
"myset" : ["a", "c", "b", "e"]
}
{
"_id" : ObjectId("5179e27b8a267c2df8e68615"),
"color" : "blue",
"myset" : ["a", "c", "b", "e", "f"]
}
结果
结果
?更新—操作数组
? $pop:删除数组中最后或第一个元素
– mycollection.insert({"color":"blue", "myset":['a','c','b']})
– mycollection.update({"color":"blue"}, {"$pop":{"myset": 1}})
#remove last
– mycollection.update({"color":"blue"}, {"$pop":{"myset": -1}})
#remove first
? $pullAll,$pull:从数组中删除值
– mycollection.insert({"color":"blue",
"myset":['a','c','b','b','e']})
– mycollection.update({"color":"blue"}, {"$pull":{"myset": 'b'}})
– mycollection.update({"color":"blue"}, {"$pullAll":{"myset":
['a','c']}}) {
"_id" :
ObjectId("5179e4f08a267c22d878e0a5"),
"color" : "blue",
"myset" : ["e"]
}
结果
?更新—操作数组
? $psuh, $pushAll:向数组添加元素
– mycollection.insert({"color":"blue"})
– mycollection.update({"color":"blue"}, {"$push":{"myset": 'b'}})
– mycollection.update({"color":"blue"}, {"$pushAll":{"myset":
['a','c']}})
– mycollection.update({“color”:“blue”},
{“$push”:{“myset”: {“$each”: [‘g’,‘b’,‘f’]}}}) #使
用 each
{
"_id" :
ObjectId("5179e83b8a267c378c7660c6"),
"color" : "blue",
"myset" : ["b", "a", "c", "g", "b", "f"]
}
结果
?更新—操作数组
? $slice:与$each 一起使用,为0或负数,截取 后几位
– mycollection.insert({"color":"blue", "size":[11,22]})
– mycollection.update({"color":"blue"}, {"$push":{"size":
{"$each":[1,3,4,5,6], "$slice":-2}}})
? $sort:对数组中元素排序,数组中元素必须是document
– mycollection.insert({“color”:“blue”,
“size”:[{“socre”:10,“name”:“aaa”},
{“score”:20,“name”:“bbb”}]})
– mycollection.update({"color":"blue"}, {"$push":{"size": {"$each":
[{"score":50,"name":"ccc"}, {"score":30,"name":"ddd“}], “$slice":-
3"$sort":{"score":1}}}})
{
"_id" : ObjectId("5179f5e88a267c4308024d93"),
"color" : "blue“, "size" : [5, 6]
}
{"_id" : ObjectId("5179f8178a267c4380bd8375"),"color" : "blue",
"size" : [{"score" : 20,"name" : "bbb"}, {"score" : 30,"name" : "ddd"},
{"score" : 50,"name" : "ccc"}]}
结果
结果
SQL: SELECT column,[column,…]
FROM mytable [WHERE
where_definition] [ORDER BY
...] [LIMIT row_count];
? 查找
db.collection.find( <query>, <projection> )
<query>:对应于 WHERE statement
<projection>:对应于需要从结果里面检索出来的字段
db.collection.find(): 返回集合中所有的文档。
插入数据:
–mycollection.insert({"_id":1, "name":"aaa", "age":20})
–mycollection.insert({"_id":2, "name":"bbb", "age":65})
–mycollection.insert({"_id":3, "name":"ccc", "age":54})
–mycollection.insert({"_id":4, "name":"ddd", "age":39})
–mycollection.insert({"_id":5, "name":"eee", "age":2})
–mycollection.insert({"_id":6, "name":"fff", "age":70})
?查找
相等判断:
mycollection.find({"name":"ccc"})
使用operator:
mycollection.find({"name": {"$in": ["ddd"]}})
使用范围查询:
mycollection.find({"age": {"$gt": 50, "$lt": 70}})
{u'age': 54, u'_id': 3, u'name': u'ccc'}
{u'age': 39, u'_id': 4, u'name': u'ddd'}
{u'age': 65, u'_id': 2, u'name': u'bbb'}
{u'age': 54, u'_id': 3, u'name': u'ccc'}
结果
结果
结果
?查找—数组
db.collection.find({field:value}):返回field数组中匹配value
的文档:
mycollection.insert({"_id":1, "name":["a","b","c"], "age":20})
mycollection.insert({"_id":1, "name":["e","f"], "age":30})
mycollection.find({"name": "e"})
数组中元素为文档:一般使用$elemMatch
– mycollection.insert({"_id":1, "name":[{"a": "a11", "b": "b11"}, {"a":
"a12", "b": "b12"}], "age":20})
– mycollection.insert({"_id":2, "name":[{"a": "a21", "b": "b21"}],
"age":30})
mycollection.find({“name.a”: “a11”, “name.b”: “b12”}):
mycollection.find({“name” : {“$elemMatch”: {“a”: “a11”, “b”:
“b12”}}}):无结果
{u'age': 20, u'_id': 1, u'name': [u'a', u'b', u'c']}
{u'age': 20, u'_id': 1, u'name': [{u'a': u'a11', u'b': u'b11'}, {u'a': u'a12', u'b': u'b12'}]}
结果
结果
?查找—子文档、$or
? db.collection.find( { “name”: {”first ”: ‘Hello’,
”last ” : ‘World’ } } ) 会做完全匹配;
? { “name”: {”first ”: ‘Hello’, ”last ” :
‘World’, “other”:’Hei’ } } 不会匹配;
要想匹配,需要用 dot 表示法:
? db.collection.find( { “name.first ”: ‘Hello’,
”name.last ” : ‘World’ } )
对于 $or 表达式:
会返回满足 “name.first”:”AAA” 和 “age”:30 的。
mycollection.find(
{ "$or": [
{ 'name.first' : "AAA" },
{ "age": 30 }
]
}
)
结果
?查找—limit、sort
? SQL: select * from table limit 10;
? MongoDB: db.collection.find().limit( num)
? SQL: select * from table order by column asc;
? MongoDB: db.collection.find().sort(“field”:1)
–mycollection.insert({"_id":1, "score":32})
–mycollection.insert({"_id":2, "score":62})
–mycollection.insert({"_id":3, "score":42})
–mycollection.insert({"_id":4, "score":90})
–mycollection.insert({“_id”:5, “score”:10})
–mycollection.find({}).limit(3).sort([("score",
pymongo.DESCENDING)])
{u'_id': 4, u'score': 90}
{u'_id': 2, u'score': 62}
{u'_id': 3, u'score': 42}
结果
?查找—使用limit提升性能
–使用limit提高效率
? 如果某次查询只想取一部分数据,那请使用limit吧。
#插入 10W行记录,每个1KB
for i in range(100000):
mycollection.insert({"_id":i, "name":"AAAAABBBBB"*100})
#不使用limit,使用了: 98714 ms
for j in range(10000):
for i,c in enumerate(mycollection.find({})):
if i >= 10:
break
#使用limit,使用了: 15558 ms
for j in range(10000):
for i,c in enumerate(mycollection.find({}).limit(10)):
pass
内容
? 介绍
? 安装
? 启动、停止
? 插入、删除、更新、查找
? 聚合、Map-Reduce、索引
? 高级主题:复制、分片
? 管理
SELECT column,count(column) FROM
mytable [WHERE
where_definition] [GROUP BY
...] [LIMIT row_count];
聚合
? 下面采用mongo shell(JS)示例聚合:
> db.colors.insert({name:'red', primary:true})
> db.colors.insert({name:'green', primary:true})
> db.colors.insert({name:'blue', primary:true})
> db.colors.insert({name:'purple', primary:false})
> db.colors.insert({name:'orange', primary:false})
> db.colors.insert({name:'yellow', primary:false})
> db.people.insert({name:'John', age:28})
> db.people.insert({name:'Steve', age:29})
> db.people.insert({name:'Steph', age:27})
? count
> db.colors.count()
6
> db.colors.count({primary:true})
3
? distinct
> db.colors.distinct('name')
[ "red", "green", "blue", "purple", "orange",
"yellow" ]
> db.people.distinct('name', {age:28})
[ "John" ]
> db.items.insert({title:'Home', template:'home'})
> db.items.insert({title:'What We Do', template:'page'})
> db.items.insert({title:'Our Writing', template:'page'})
> db.items.insert({title:'Who We Are', template:'page'})
> db.items.insert({title:'Hire Us', template:'page'})
> var key = {template: true};
> var initial = {count:0};
> var reduce = function(obj, prev) { prev.count += 1; };
> db.items.group({key:key, initial:initial, reduce:reduce})
[
{"template" : "home", "count" : 1},
{"template" : "page", "count" : 4}
]
Map-Reduce
? 与Hadoop的Map-Reduce思想一致,可以分为四步:
–从输入集合读入数据
–执行 map 函数
–执行 reduce 函数 input collection,
–将输出写入到输出集合
? Map-Reduce: 可以执行聚合函数的所有功能,应该说是聚
合函数的超集。
? Map-Reduce:需要参看各个驱动程序的实现,一般是需要
传递JS函数。
Map-Reduce
? 同样通过mongo shell(JS)演示:
> db.items.insert({tags: ['dog', 'cat']})
> db.items.insert({tags: ['dog']})
> db.items.insert({tags: ['dog', 'mouse']})
> db.items.insert({tags: ['dog', 'mouse',
'hippo']})
> db.items.insert({tags: ['dog', 'mouse',
'hippo']})
> db.items.insert({tags: ['dog', 'hippo']})
> var map = function() {
this.tags.forEach(function(t) {
emit(t, {count: 1});
});
}
> var reduce = function(key, values) {
var count = 0;
for(var i=0, len=values.length; i<len; i++) {
count += values[i].count;
}
return {count: count};
}
> var map = function() {
this.tags.forEach(function(t) {
emit(t, {count: 1});
});
}
> var reduce = function(key, values) {
var count = 0;
for(var i=0, len=values.length; i<len; i++) {
count += values[i].count;
}
return {count: count};
}
> var result = db.items.mapReduce(map, reduce, {
out: “map_reduce_example” });
> db.map_reduce_example.find()
{ "_id" : "cat", "value" : { "count" : 1 } }
{ "_id" : "dog", "value" : { "count" : 6 } }
{ "_id" : "hippo", "value" : { "count" : 3 } }
{ "_id" : "mouse", "value" : { "count" : 3 } }
索引
? 概念
–MongoDB中的索引与其它DB原理相似,使用索
引可以快速查询文档:
索引是面向整个集合的;
所有的MongoDB索引使用B-tree结构;
所有的查询仅会使用一个索引;
索引类型:
_id Index:唯一索引,默认会创建。
子文档索引:对子文档进行索引。
复合索引:多个字段组成的索引。
多key索引:对数组进行索引清醒。
hash索引,唯一索引。
……
? 索引操作
–建立索引(Mongo Shell)
– db.collection.ensureIndex(Index_pattern)
? db.people.ensureIndex({pn:1})#按pn升序索引
? db.collection.ensureIndex( { a: 1, b: 1, c: 1 } )建立
复合索引
? db.collection.ensureIndex( {a: 1},{unique: true })建立
唯一索引
db.collection.ensureIndex({a: 1},{dropDups: true})删除重
复值
db.users.ensureIndex({ twitter_name: 1},{sparse: true} )
建立稀疏索引,对不存在索引字段的项不索引
db.collection.ensureIndex({a:“hashed”})建立hash索引
? 索引操作
–删除索引
? 可以从一个集合中删除索引
? 示例:db.accounts.dropIndex( { "tax-id": 1 } )
–使用explain():
? 在任何查询游标后使用 explain 可以查看查询所使用的索引
状况。
–使用hint():
? 可以强制要求查询游标使用某种索引
db.people.find( { name: "John Doe", zipcode: { $gt: 63000 } } } ).hint( { zipcode: 1 } )
db.people.find( { name: “John Doe”, zipcode: { $gt: 63000 } } } ).explain()
内容
? 介绍
? 安装
? 启动、停止
? 插入、删除、更新、查找
? 聚合、Map-Reduce、索引
? 高级主题:复制、分片
? 管理
生产环境架构
? Config server:为mongod实例,存储了集群的一些
metadata,mongos会缓存这些数据以决定路由。
? Shards:至少两个分片,用于存储数据,分片会存储集群
的所有数据。
? Replica Sets:生产环境最好是每个分片都是副本集,用
于容灾。
? Mongos:应用层到分片的中间层,只负责转发数据,不维
护任何状态,当然配置信息在mongos中有缓存。
生产环境架构
? Mongos进程挂掉:由于mongos不持久化任何信息,其实挂
掉无所谓,重启就可以,另外一般启动多个mongos。
? replica set 的一个mongod挂掉:由于有副本集,如果是
primary mongod挂掉,会重新选举,如果是secondary
mongod挂掉,没任何影响。
? Replica set的所有mongod挂掉:会丢失部分数据,不影
响读写。
? Config server挂掉:所有的挂掉整个服务会不可用,但
如果只有部分挂掉,不影响服务。
高可用保证
? 副本集-概念
–副本集指的是一些mongod的实例进程,互为备份关系
,副本集合中有一个primary,其余为secondary。
–客户端数据会直接写入primary,secondary会从
primary同步。
–Primary挂掉,其余节点会进行选举。
–读-写分离:客户端连接可以指定secondary优先,这
样在大量读写时有效减轻primary的压力。
副本集-分片-配置服务器 搭建
? 副本集-搭建
–以搭建三个实例为例:
– mkdir -p /srv/mongodb/rs0-0 /srv/mongodb/rs0-1
/srv/mongodb/rs0-2
– mongod --port 27017 --dbpath /srv/mongodb/rs0-0 --replSet rs0
– mongod --port 27018 --dbpath /srv/mongodb/rs0-1 --replSet rs0
– mongod --port 27019 --dbpath /srv/mongodb/rs0-2 --replSet rs0
– mongo --port 27017 连接到一个mongod实例
– rsconf = { _id: "rs0", members: [ { _id: 0, host:
"<hostname>:27017" } ] }
– 初始化:rs.initiate( rsconf )
– 增加副本:rs.add("<hostname>:27018") rs.add("<hostname>:27019")
– 删除副本:rs.remove("<hostname>:27017")
– 修改副本:cfg = rs.conf()
cfg.members[0].host = "mongo2.example.net:27019"
rs.reconfig(cfg)
?
副本集-分片-配置服务器 搭建
? 配置服务器-搭建
–建立数据存放目录:
? mkdir /data/configdb
–启动配置服务器:
? mongod --configsvr --dbpath <path> --port
<port>
–配置分片:需要使用mongos连接配置服务器进行配置
,也可以参考各个语言驱动来配置。
副本集-分片-配置服务器 搭建
? 分片-搭建
–需要启动mongos连接配置服务器来进行配置。
–启动:mongos --configdb <config server
hostnames>
–启动mongo,连接mongos:
? mongo --host <hostname of machine running mongos> --
port <port mongos listens on>
–增加分片:
? sh.addShard( “rs1/ip:port” )
? rs1为副本集名称,ip:port 为副本集的任何一个mongod。
副本集-分片-配置服务器 搭建
? 使用分片
–添加分片后,如果需要对某个库或集合使用分片,需
要执行如下操作:
? sh.enableSharding("<database>")
? db.runCommand( { enableSharding: <database> } )
? sh.shardCollection("<database>.<collection>", shard-
key-pattern)
? shard-key-pattern 与建立索引的方式一致。
副本集-分片-配置服务器 搭建
sh.shardCollection("records.people", { "zipcode": 1, "name": 1 } )
sh.shardCollection("people.addresses", { "state": 1, "_id": 1 } )
sh.shardCollection("assets.chairs", { "type": 1, "_id": 1 } )
示例
? 分片键的选择
–容易切分:便于mongos进行分发,让片上数据均匀。
–具有随机性:避免某个mongod成为瓶颈。
–尽量能随机到一台机器:与具有随机性貌似有点矛盾
,但也是一个优势。
–使用复合键:有时候需要使用,根据需求。
?Hashed Sharding
–使用某个键的hash值进行分片。
副本集-分片-配置服务器 搭建
?对集合使用分片
–对集合使用分片后发现会对其进行索引。
–从性能上也可以对比,使用分片和不使用,具有很大
差异,对构造的10W条记录:
? 使用分片,查询1W次:9,007ms
? 不使用分片,查询1K次:64,309ms
分片与索引?
admin.command('shardcollection', 'test.test_big', key={'name': 1})
admin.command('shardcollection', 'test.test_x', key={'name': pymongo.HASHED})
内容
? 介绍
? 安装
? 启动、停止
? 插入、删除、更新、查找
? 聚合、Map-Reduce、索引
? 高级主题:复制、分片
? 管理
备份-恢复
? 备份有两种方式:
–Mongodump:备份的文件会较小,备份速度较慢。
–filesystem snapshots:会生成较大备份文件,快速
,需要操作系统支持。
? 需要系统级别支持,要安装工具才能做到。
? 详见:
http://docs.mongodb.org/manual/tutorial/backu
p-databases-with-filesystem-snapshots/
备份-恢复
?使用Mongodump备份
? mongodump --dbpath /data/db/ --out /data/backup/
–导出的是bson结构的,通过--collection collection --db test
可以指定集合和数据库。
–使用--oplog:在副本集中使用oplog确保备份的数据是一致的,
系统会同时备份日志文件。
?使用mongorestore恢复
–mongorestore --port <port number> <path to the backup>
–可以使用mongorestore --filter ‘{“field”: 1}’过滤一些
数据
–使用--oplogReplay:该选项确保恢复的数据也是一致的。
监控
? MongoDB提供了两种监控方式:
–发布了一系列实时收集数据库的工具。
–一些数据库的命令,查询系统状态(shell方式)。
?工具类:
–Mongotop:当前的读写状况。
–Mongostat:当前所有操作的所有统计数目。
–REST Interface:HTTP请求http://localhost:28017
方式
监控—统计命令
? serverStatus(db.command(“serverStatus”)):统计
数据库的状态,硬盘、内存、连接等状况。
? replSetGetStatus:返回副本集的状态,注意要使用
admin账号。
? dbStats, collStats:分别返回数据库,集合的详细状
态,包括硬盘空间等。
? 详细请参考:
http://docs.mongodb.org/manual/administration/monitoring/
导入-导出数据
? 与备份-恢复不同,导入-导出只的是导数据库中的部分数
据,不涉及整个db。
? mongoexport: 导出到json文件或控制台输出。
–mongoexport --collection collection --out collection.json
–可以加入 –query指定查询条件
–Mongod若没运行,可以使用: --dbpath 指定数据库路径
?mongoimport:
–mongoimport --collection collection --file
collection.json
–Mongod若没运行,可以使用: --dbpath 指定数据库路径
? copydb, clone, cloneCollection:数据库,集合的移动
操作。
ulimit 设置
? Linux内核会限制线程、连接、打开文件数。这些限制对
于mongodb的使用会有影响。
? mongod 和 mongos 使用线程和文件描述符管理连接和内
部状态。
? 推荐配置:
? 生产环境使用参考:
http://docs.mongodb.org/manual/reference/ulimit/
查看副本集:
connect mongod: >rs.status()
查看分片:
connect mongos: >use config >db.shards.find({}) #查看分片host
>sh.status() #查看分片状态
查看查询使用的状态:
connect mongod/mongos: >db.collection.find(condition).explain()
查看索引:
connect mongod/mongos: >db.system.indexes.find({})
? Email:
http://groups.google.com/group/mongodb-user
? IRC:
irc://irc.freenode.net/#mongodb
? WEB:
http://mongodb.org/
http://mongotips.com/
? BOOK:
http://www.10gen.com/books
http://cookbook.mongodb.org/
? Conference:
http://www.10gen.com/events
http://windycitydb.org/
? Training:
http://ideafoundry.info/mongodb
Thank you!
qfdai2@iflytek.com
科大讯飞研究院-互联网技术部

More Related Content

Mongo db introduction