且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MongoDB的sharding功能(转)

更新时间:2022-10-04 16:49:13

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 Servers
Config服务器存储着集群的metadata信息,包括每个服务器,每个shard的基本信息和chunk信息
Config服务器主要存储的是chunk信息。每一个config服务器都复制了完整的chunk信息。



配置:(模拟2个shard服务和一个config服务)
Shard1:27020
Shard2:27021
Config:27022
Mongos启动时默认使用的27017端口

新建存放数据的目录

[falcon@www.fwphp.cn  ~/mongodata]mkdir270202702127022[falcon@www.fwphp.cn /mongodata]mkdir270202702127022[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/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]psef|grepmongofalcon26121020:15?00:00:00./mongoddbpath/home/falcon/mongodata/27020port27020falcon26191020:15?00:00:00./mongoddbpath/home/falcon/mongodata/27021port27021falcon26251020:15?00:00:00./mongoddbpath/home/falcon/mongodata/27022port27022falcon26581020:15?00:00:00./mongosconfigdblocalhost:27022falcon28042772020:31pts/000:00:00bin/mongofalcon28472812020:55pts/200:00:00grepmongo[falcon@www.fwphp.cn /mongodb/bin]ps−ef|grepmongofalcon26121020:15?00:00:00./mongod−−dbpath/home/falcon/mongodata/27020−−port27020falcon26191020:15?00:00:00./mongod−−dbpath/home/falcon/mongodata/27021−−port27021falcon26251020:15?00:00:00./mongod−−dbpath/home/falcon/mongodata/27022−−port27022falcon26581020:15?00:00:00./mongos−−configdblocalhost:27022falcon28042772020:31pts/000:00:00bin/mongofalcon28472812020:55pts/200:00:00grepmongo[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]lsR.:270202702127022mongos.log./27020:27020.logmongod.locktest.0test.1test.nstmp./27020/tmp:./27021:27021.logmongod.locktmpuser.0user001.0user001.1user001.nsuser.1user.ns./27021/tmp:./27022:27022.logconfig.0config.nsmongod.lockmongos.logtmp./27022/tmp:[falcon@www.fwphp.cn /mongodata]ls−R.:270202702127022mongos.log./27020:27020.logmongod.locktest.0test.1test.nstmp./27020/tmp:./27021:27021.logmongod.locktmpuser.0user001.0user001.1user001.nsuser.1user.ns./27021/tmp:./27022:27022.logconfig.0config.nsmongod.lockmongos.logtmp./27022/tmp:[falcon@www.fwphp.cn /mongodata]

从以上的文件可以看出,user001被分配到了27021的shard上了,但是通过mongos路由,我们并感觉不到是数据存放在哪个shard的chunk上


Sharding的管理命令

> db.cmd.findOne(isdbgrid:1);"isdbgrid":1,"hostname":"www.fwphp.cn","ok":1>db.cmd.findOne(isdbgrid:1);"isdbgrid":1,"hostname":"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


本文转自 不得闲 博客园博客,原文链接:http://www.cnblogs.com/DxSoft/archive/2010/10/21/1857363.html   ,如需转载请自行联系原作者