mongodb还提供了HTTP查看运行 状态及restfull的接口
默认的访问端口 是28017
rest的访问接口
参考地址:http://www.mongodb.org/display/DOCS/Http+Interface
MongoDB的sharding功能
MongoDB的auto-sharding功能是指mongodb通过mongos自动建立一个水平扩展的数据 库集群 系统 ,将数据库 分表存储在sharding的各个节点上。
一个mongodb集群包括一些shards(包括一些mongod进程 ),mongos路由进程,一个或多个config服务器
Shards
每一个shard包括一个或多个服务 和存储数据的mongod进程(mongod是 MongoDB数据的核心进程)
典型的每个shard开启多个服务来提高服务的可用性。这些服务/mongod进程在shard中组成一个复制集
Chunks
Chunk是一个来自特殊集合中的一个数据范围,(collection,minKey,maxKey)描叙一个chunk,它介于minKey和 maxKey范围之间。
例如chunks 的maxsize大小是100M,如果一个文件 达到或超过这个范围时,会被切分到2个新的 chunks中。当一个shard的数据过量时,chunks将会被迁移到其他的shards上。同样,chunks也可以迁移到其他的shards上
Config Server s
Config服务器存储着集群的metadata信息,包括每个服务器,每个shard的基本信息和chunk信息
Config服务器主要存储的是chunk信息。每一个config服务器都复制了完整的chunk信息。
配置:(模拟2个shard服务和一个config服务)
Shard1:27020
Shard2:27021
Config:27022
Mongos启动时默认使用的27017端口
新建存放数据的目录
[falcon@www.fwphp .cn ~/mongodata]$ mkdir 27020 27021 27022
[falcon@www.fwphp.cn ~/mongodata]$ ls
27020 27021 27022
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020 > /home/falcon/mongodata/27020.log &
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021 > /home/falcon/mongodata/27021.log &
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022 > /home/falcon/mongodata/27022.log &
启动mongos时,默认开启了27017端口
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongos --configdb localhost:27022 > /home/falcon/mongodata/config.log &
检查是否启动
[falcon@www.fwphp.cn ~/mongodb/bin]$ ps -ef|grep mongo
falcon 2612 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020
falcon 2619 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021
falcon 2625 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022
falcon 2658 1 0 20:15 ? 00:00:00 ./mongos --configdb localhost:27022
falcon 2804 2772 0 20:31 pts/0 00:00:00 bin/mongo
falcon 2847 2812 0 20:55 pts/2 00:00:00 grep mongo
[falcon@www.fwphp.cn ~/mongodb/bin]$
[falcon@www.fwphp.cn ~/mongodb/bin]$ netstat -an -t
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:10022 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27021 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27022 0.0.0.0:* LISTEN
......
tcp 0 0 0.0.0.0:28020 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:28021 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:28022 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
........
复制代 码
看到以上信息证明mongodb启动完整,对于开启的28020、28021、28022是对于的http接口
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongo 默认连接到mongos上
MongoDB shell version: 1.2.4-
url: test
connecting to: test
type "help" for help
> show dbs
admin
config
Local
加 入shard节点
> use admin
switched to db admin
> db.runCommand( { addshard : "localhost:27020", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27020"}
> db.runCommand( { addshard : "localhost:27021", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27021"}
> db.runCommand({listshards:1}); 查看shard节点列表
{
"shards" : [
{
"_id" : ObjectId("4b9cd380c33000afad27718e"),
"host" : "localhost:27020"
},
{
"_id" : ObjectId("4b9cd381c33000afad27718f"),
"host" : "localhost:27021"
}
],
"ok" : 1
}
新 建自动切片的库user001:
> config = connect("localhost:27022")
> config = config.getSisterDB("config")
> user001=db.getSisterDB("user001");
user001
> db.runCommand({enablesharding:"user001"})
{ "ok" : 1 }
> db.printShardingStatus();
--- Sharding Status ---
sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
shards:
{ "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
{ "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
databases:
{ "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
{ "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
my chunks
我们来在user001中新建表,插入数据
> use user001
switched to db user001
> db.createCollection("user_001")
{ "ok" : 1 }
> show collections
system.indexes
user_001
> db.user_001.insert({uid:1,username:"Falcon.C",sex:"男",age:25});
> db.user_001.find();
{ "_id" : ObjectId("4b9ce1a6c84d7f20576c4df1"), "uid" : 1, "username" : "Falcon.C", "sex" : "男", "age" : 25 }
我们来看看user001库被分配到了哪个shard 上
[falcon@www.fwphp.cn ~/mongodata]$ ls -R
.:
27020 27021 27022 mongos.log
./27020:
27020.log mongod.lock test.0 test.1 test.ns _tmp
./27020/_tmp:
./27021:
27021.log mongod.lock _tmp user.0 user001.0 user001.1 user001.ns user.1 user.ns
./27021/_tmp:
./27022:
27022.log config.0 config.ns mongod.lock mongos.log _tmp
./27022/_tmp:
[falcon@www.fwphp.cn ~/mongodata]$
从以上的文件可以看出,user001被分配到了 27021的shard上了,但是通过mongos路由,我们并感觉不到是数据存放在哪个shard的chunk上
Sharding的管理 命令
> db.$cmd.findOne({isdbgrid:1});
{ "isdbgrid" : 1, "hostname" : "http://www.fwphp.cn/", "ok" : 1 }
> db.$cmd.findOne({ismaster:1});
{ "ismaster" : 1, "msg" : "isdbgrid", "ok" : 1 }
> printShardingStatus(db.getSisterDB("config"))
--- Sharding Status ---
sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
shards:
{ "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
{ "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
databases:
{ "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
my chunks
{ "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
my chunks
> use admin
switched to db admin
> db.runCommand({netstat:1})
{ "configserver" : "localhost:27022", "isdbgrid" : 1, "ok" : 1 }
>
参 考信息:http://www.mongodb.org/display/DOCS/Sharding
MongoDB数据库的MapReduce简单操作
MongoDB也简单的实现了MapReduce的功能来提供分布式的数据 查询服务 ,MapReduce的分布是功能主要用在 Shard上
db.runCommand(
{ mapreduce : <collection>,
map : <mapfunction>,
reduce : <reducefunction>
[, query : <query filter object>]
[, sort : <sort the query. useful for optimization>]
[, limit : <number of objects to return from collection>]
[, out : <output-collection name>]
[, keeptemp: <true|false>]
[, finalize : <finalizefunction>]
[, scope : <object where fields go into javascript global scope >]
[, verbose : true]
}
);
下 面是对MapReduce的简单测试
此例子来源于:http://www.mongodb.org/display/DOCS/MapReduce
> db.things.insert({_id:1,tags:['dog','cat']});
> db.things.insert({_id:2,tags:['cat']});
> db.things.insert({_id:3,tags:['mouse','cat','dog']});
> db.things.insert({_id:4,tags:[]});
> m = function(){
... this.tags.forEach(
... function(z){
... emit(z,{count:1});
... }
... );
};
function () {
this.tags.forEach(function (z) {emit(z, {count:1});});
}
> r=function(key,values){
... var total = 0;
... for(var i=0;i<values.length;i++)
... total += values[i].count;
... return {count:total};
... };
function (key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i].count;
}
return {count:total};
}
> res=db.things.mapReduce(m,r);
{
"result" : "tmp.mr.mapreduce_1268577545_1",
"timeMillis" : 25,
"counts" : {
"input" : 4,
"emit" : 6,
"output" : 3
},
"ok" : 1,
"ok" : 1,
}
> res
{
"result" : "tmp.mr.mapreduce_1268577545_1",
"timeMillis" : 25,
"counts" : {
"input" : 4,
"emit" : 6,
"output" : 3
},
"ok" : 1,
"ok" : 1,
}
> db[res.result].find()
{ "_id" : "cat", "value" : { "count" : 3 } }
{ "_id" : "dog", "value" : { "count" : 2 } }
{ "_id" : "mouse", "value" : { "count" : 1 } }
> db[res.result].drop()
true
> db[res.result].find()
>
以 下有几个MapReduce的参考例子:
http://www.mongodb.org/display/DOCS/MapReduce
http://github.com/mongodb/mongo/ ... sts/mr_bigobject.js
http://github.com/mongodb/mongo/blob/master/jstests/mr5.js
http://github.com/mongodb/mongo/blob/master/jstests/mr4.js
http://github.com/mongodb/mongo/blob/master/jstests/mr3.js
http://github.com/mongodb/mongo/blob/master/jstests/mr2.js
http://github.com/mongodb/mongo/blob/master/jstests/mr1.js
默认的访问端口 是28017
rest的访问接口
参考地址:http://www.mongodb.org/display/DOCS/Http+Interface
MongoDB的sharding功能
MongoDB的auto-sharding功能是指mongodb通过mongos自动建立一个水平扩展的数据 库集群 系统 ,将数据库 分表存储在sharding的各个节点上。
一个mongodb集群包括一些shards(包括一些mongod进程 ),mongos路由进程,一个或多个config服务器
Shards
每一个shard包括一个或多个服务 和存储数据的mongod进程(mongod是 MongoDB数据的核心进程)
典型的每个shard开启多个服务来提高服务的可用性。这些服务/mongod进程在shard中组成一个复制集
Chunks
Chunk是一个来自特殊集合中的一个数据范围,(collection,minKey,maxKey)描叙一个chunk,它介于minKey和 maxKey范围之间。
例如chunks 的maxsize大小是100M,如果一个文件 达到或超过这个范围时,会被切分到2个新的 chunks中。当一个shard的数据过量时,chunks将会被迁移到其他的shards上。同样,chunks也可以迁移到其他的shards上
Config Server s
Config服务器存储着集群的metadata信息,包括每个服务器,每个shard的基本信息和chunk信息
Config服务器主要存储的是chunk信息。每一个config服务器都复制了完整的chunk信息。
配置:(模拟2个shard服务和一个config服务)
Shard1:27020
Shard2:27021
Config:27022
Mongos启动时默认使用的27017端口
新建存放数据的目录
[falcon@www.fwphp .cn ~/mongodata]$ mkdir 27020 27021 27022
[falcon@www.fwphp.cn ~/mongodata]$ ls
27020 27021 27022
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020 > /home/falcon/mongodata/27020.log &
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021 > /home/falcon/mongodata/27021.log &
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022 > /home/falcon/mongodata/27022.log &
启动mongos时,默认开启了27017端口
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongos --configdb localhost:27022 > /home/falcon/mongodata/config.log &
检查是否启动
[falcon@www.fwphp.cn ~/mongodb/bin]$ ps -ef|grep mongo
falcon 2612 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020
falcon 2619 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021
falcon 2625 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022
falcon 2658 1 0 20:15 ? 00:00:00 ./mongos --configdb localhost:27022
falcon 2804 2772 0 20:31 pts/0 00:00:00 bin/mongo
falcon 2847 2812 0 20:55 pts/2 00:00:00 grep mongo
[falcon@www.fwphp.cn ~/mongodb/bin]$
[falcon@www.fwphp.cn ~/mongodb/bin]$ netstat -an -t
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:10022 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27021 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27022 0.0.0.0:* LISTEN
......
tcp 0 0 0.0.0.0:28020 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:28021 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:28022 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
........
复制代 码
看到以上信息证明mongodb启动完整,对于开启的28020、28021、28022是对于的http接口
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongo 默认连接到mongos上
MongoDB shell version: 1.2.4-
url: test
connecting to: test
type "help" for help
> show dbs
admin
config
Local
加 入shard节点
> use admin
switched to db admin
> db.runCommand( { addshard : "localhost:27020", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27020"}
> db.runCommand( { addshard : "localhost:27021", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27021"}
> db.runCommand({listshards:1}); 查看shard节点列表
{
"shards" : [
{
"_id" : ObjectId("4b9cd380c33000afad27718e"),
"host" : "localhost:27020"
},
{
"_id" : ObjectId("4b9cd381c33000afad27718f"),
"host" : "localhost:27021"
}
],
"ok" : 1
}
新 建自动切片的库user001:
> config = connect("localhost:27022")
> config = config.getSisterDB("config")
> user001=db.getSisterDB("user001");
user001
> db.runCommand({enablesharding:"user001"})
{ "ok" : 1 }
> db.printShardingStatus();
--- Sharding Status ---
sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
shards:
{ "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
{ "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
databases:
{ "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
{ "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
my chunks
我们来在user001中新建表,插入数据
> use user001
switched to db user001
> db.createCollection("user_001")
{ "ok" : 1 }
> show collections
system.indexes
user_001
> db.user_001.insert({uid:1,username:"Falcon.C",sex:"男",age:25});
> db.user_001.find();
{ "_id" : ObjectId("4b9ce1a6c84d7f20576c4df1"), "uid" : 1, "username" : "Falcon.C", "sex" : "男", "age" : 25 }
我们来看看user001库被分配到了哪个shard 上
[falcon@www.fwphp.cn ~/mongodata]$ ls -R
.:
27020 27021 27022 mongos.log
./27020:
27020.log mongod.lock test.0 test.1 test.ns _tmp
./27020/_tmp:
./27021:
27021.log mongod.lock _tmp user.0 user001.0 user001.1 user001.ns user.1 user.ns
./27021/_tmp:
./27022:
27022.log config.0 config.ns mongod.lock mongos.log _tmp
./27022/_tmp:
[falcon@www.fwphp.cn ~/mongodata]$
从以上的文件可以看出,user001被分配到了 27021的shard上了,但是通过mongos路由,我们并感觉不到是数据存放在哪个shard的chunk上
Sharding的管理 命令
> db.$cmd.findOne({isdbgrid:1});
{ "isdbgrid" : 1, "hostname" : "http://www.fwphp.cn/", "ok" : 1 }
> db.$cmd.findOne({ismaster:1});
{ "ismaster" : 1, "msg" : "isdbgrid", "ok" : 1 }
> printShardingStatus(db.getSisterDB("config"))
--- Sharding Status ---
sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
shards:
{ "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
{ "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
databases:
{ "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
my chunks
{ "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
my chunks
> use admin
switched to db admin
> db.runCommand({netstat:1})
{ "configserver" : "localhost:27022", "isdbgrid" : 1, "ok" : 1 }
>
参 考信息:http://www.mongodb.org/display/DOCS/Sharding
MongoDB数据库的MapReduce简单操作
MongoDB也简单的实现了MapReduce的功能来提供分布式的数据 查询服务 ,MapReduce的分布是功能主要用在 Shard上
db.runCommand(
{ mapreduce : <collection>,
map : <mapfunction>,
reduce : <reducefunction>
[, query : <query filter object>]
[, sort : <sort the query. useful for optimization>]
[, limit : <number of objects to return from collection>]
[, out : <output-collection name>]
[, keeptemp: <true|false>]
[, finalize : <finalizefunction>]
[, scope : <object where fields go into javascript global scope >]
[, verbose : true]
}
);
下 面是对MapReduce的简单测试
此例子来源于:http://www.mongodb.org/display/DOCS/MapReduce
> db.things.insert({_id:1,tags:['dog','cat']});
> db.things.insert({_id:2,tags:['cat']});
> db.things.insert({_id:3,tags:['mouse','cat','dog']});
> db.things.insert({_id:4,tags:[]});
> m = function(){
... this.tags.forEach(
... function(z){
... emit(z,{count:1});
... }
... );
};
function () {
this.tags.forEach(function (z) {emit(z, {count:1});});
}
> r=function(key,values){
... var total = 0;
... for(var i=0;i<values.length;i++)
... total += values[i].count;
... return {count:total};
... };
function (key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i].count;
}
return {count:total};
}
> res=db.things.mapReduce(m,r);
{
"result" : "tmp.mr.mapreduce_1268577545_1",
"timeMillis" : 25,
"counts" : {
"input" : 4,
"emit" : 6,
"output" : 3
},
"ok" : 1,
"ok" : 1,
}
> res
{
"result" : "tmp.mr.mapreduce_1268577545_1",
"timeMillis" : 25,
"counts" : {
"input" : 4,
"emit" : 6,
"output" : 3
},
"ok" : 1,
"ok" : 1,
}
> db[res.result].find()
{ "_id" : "cat", "value" : { "count" : 3 } }
{ "_id" : "dog", "value" : { "count" : 2 } }
{ "_id" : "mouse", "value" : { "count" : 1 } }
> db[res.result].drop()
true
> db[res.result].find()
>
以 下有几个MapReduce的参考例子:
http://www.mongodb.org/display/DOCS/MapReduce
http://github.com/mongodb/mongo/ ... sts/mr_bigobject.js
http://github.com/mongodb/mongo/blob/master/jstests/mr5.js
http://github.com/mongodb/mongo/blob/master/jstests/mr4.js
http://github.com/mongodb/mongo/blob/master/jstests/mr3.js
http://github.com/mongodb/mongo/blob/master/jstests/mr2.js
http://github.com/mongodb/mongo/blob/master/jstests/mr1.js