文章来源:
汇总连接:
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@~/mongodata]$ mkdir 27020 27021 27022
- [falcon@~/mongodata]$ ls
- 27020 27021 27022
- [falcon@~/mongodb/bin]$ ./mongod --dbpath
/home/falcon/mongodata/27020 --port 27020 >
/home/falcon/mongodata/27020.log &
- [falcon@~/mongodb/bin]$ ./mongod --dbpath
/home/falcon/mongodata/27021 --port 27021 >
/home/falcon/mongodata/27021.log &
- [falcon@~/mongodb/bin]$ ./mongod --dbpath
/home/falcon/mongodata/27022 --port 27022 >
/home/falcon/mongodata/27022.log &
复制代码启动mongos时,默认开启了27017端口
- [falcon@~/mongodb/bin]$ ./mongos --configdb localhost:27022 > /home/falcon/mongodata/config.log &
复制代码检查是否启动
- [falcon@~/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@~/mongodb/bin]$
- [falcon@~/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@~/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",***:"男",age:25});
- > db.user_001.find();
- { "_id" : ObjectId("4b9ce1a6c84d7f20576c4df1"), "uid" : 1, "username" : "Falcon.C", "***" : "男", "age" : 25 }
复制代码我们来看看user001库被分配到了哪个shard上
- [falcon@~/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@~/mongodata]$
复制代码从以上的文件可以看出,user001被分配到了27021的shard上了,但是通过mongos路由,我们并感觉不到是数据存放在哪个shard的chunk上
Sharding的管理命令
- > db.$cmd.findOne({isdbgrid:1});
- { "isdbgrid" : 1, "hostname" : "", "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 }
- >
复制代码参考信息:
阅读(861) | 评论(0) | 转发(0) |