MongoDB整理笔记のReplica Sets + Sharding
MongoDB Auto-Sharding 解决了海量存储和动态扩容的问题,但离实际生产环境所需的高可靠、高可用还有些距离,所以有了"Replica Sets + Sharding"的解决方案。
shard:
使用Replica Sets,确保每个数据节点都具有备份,自动容错转移,自动回复能力。
config:
使用3个配置服务器,确保元数据的完整性。
route:
使用3个路由进程,实现负载均衡,提高客户端接入性能。
配置Replica Sets + Sharding 架构图:
配置Replica Sets + Sharding
(1)配置shard1所用到的Replica Sets
在server A上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017
--dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork
[root@localhost bin]# all output going to: /data/shard1_1/shard1_1.log
forked process: 18923
在server B上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017
--dbpath /data/shard1_2 --logpath /data/shard1_2/shard1_2.log --logappend --fork
forked process: 18859
[root@localhost bin]# all output going to: /data/shard1_2/shard1_2.log
[root@localhost bin]#
在Server C 上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017
--dbpath /data/shard1_3 --logpath /data/shard1_3/shard1_3.log --logappend --fork
all output going to: /data/shard1_3/shard1_3.log
forked process: 18768
[root@localhost bin]#
用mongo 连接其中一台机器的27017 端口的mongod,初始化Replica Sets“shard1”,执行:
[root@localhost bin]# ./mongo --port 27017
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:27017/test
> config = {_id: 'shard1', members: [
... {_id: 0, host: '192.168.3.231:27017'},
... {_id: 1, host: '192.168.3.232:27017'},
... {_id: 2, host: '192.168.3.233:27017'}]
... }
……
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
(2)配置shard2所用到的Replica Sets
在server A上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018
--dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --fork
all output going to: /data/shard2_1/shard2_1.log
[root@localhost bin]# forked process: 18993
[root@localhost bin]#
在server B上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018
--dbpath /data/shard2_2 --logpath /data/shard2_2/shard2_2.log --logappend --fork
all output going to: /data/shard2_2/shard2_2.log
forked process: 18923
[root@localhost bin]#
在Server C上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018
--dbpath /data/shard2_3 --logpath /data/shard2_3/shard2_3.log --logappend --fork
[root@localhost bin]# all output going to: /data/shard2_3/shard2_3.log
forked process: 18824
[root@localhost bin]#
用mongo 连接其中一台机器的27018 端口的mongod,初始化Replica Sets “shard2”,执行:
[root@localhost bin]# ./mongo --port 27018
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:27018/test
> config = {_id: 'shard2', members: [
... {_id: 0, host: '192.168.3.231:27018'},
... {_id: 1, host: '192.168.3.232:27018'},
... {_id: 2, host: '192.168.3.233:27018'}]
... }
……
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1 db.runCommand({ enablesharding:"test" })
db.runCommand({ shardcollection: "test.users", key: { _id:1 }}) }
  (3)配置3 台Config Server
    在Server A、B、C上执行:
/Apps/mongo/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath
/data/config/config.log --logappend --fork
(4)配置3台Route Process
    在Server A、B、C上执行:
/Apps/mongo/bin/mongos --configdb
192.168.3.231:20000,192.168.3.232:20000,192.168.3.233:20000 --port 30000 --chunkSize 1
--logpath /data/mongos.log --logappend --fork
(5)配置Shard Cluster
    连接到其中一台机器的端口30000 的mongos 进程,并切换到admin 数据库做以下配置
[root@localhost bin]# ./mongo --port 30000
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:30000/test
> use admin
switched to db admin
>db.runCommand({addshard:"shard1/192.168.3.231:27017,192.168.3.232:27017,192.168.3.233:
27017"});
{ "shardAdded" : "shard1", "ok" : 1 }
>db.runCommand({addshard:"shard2/192.168.3.231:27018,192.168.3.232:27018,192.168.3.233:
27018"});
{ "shardAdded" : "shard2", "ok" : 1 }
>
激活数据库及集合的分片
db.runCommand({ enablesharding:"test" })
db.runCommand({ shardcollection: "test.users", key: { _id:1 }})
(6)验证Sharding正常工作
    连接到其中一台机器的端口30000 的mongos 进程,并切换到test 数据库,以便添加测试数据
use test
for(var i=1;i<=200000;i++) db.users.insert({id:i,addr_1:"Beijing",addr_2:"Shanghai"});
db.users.stats()
{
"sharded" : true,
"ns" : "test.users",
"count" : 200000,
"size" : 25600384,
"avgObjSize" : 128,
"storageSize" : 44509696,
"nindexes" : 2,
"nchunks" : 15,
"shards" : {
"shard0000" : {
……
},
"shard0001" : {
……
}
},
"ok" : 1
}
可以看到Sharding搭建成功了,跟我们期望的结果一致,至此我们就将Replica Sets与Sharding结合的架构也学习完毕了!
MongoDB整理笔记のReplica Sets + Sharding的更多相关文章
- MongoDB整理笔记のReplica Sets
		MongoDB支持在多个机器中通过异步复制达到故障转移和实现冗余.多机器中同一时刻只有一台机器是用于写操作,正因为如此,MongoDB提供了数据一致性的保障.而担当primary角色的机器,可以把读的 ... 
- MongoDB整理笔记のReplica oplog
		主从操作日志oplog MongoDB的Replica Set架构是通过一个日志来存储写操作的,这个日志就叫做"oplog".oplog.rs是一个固定长度的capped coll ... 
- 利用Mongodb的复制集搭建高可用分片,Replica Sets + Sharding的搭建过程
		参考资料 reference: http://mongodb.blog.51cto.com/1071559/740131 http://docs.mongodb.org/manual/tutori ... 
- MongoDB高可用架构:Replica Sets+Sharding
		MongoDB的sharding解决了海量存储和动态扩容的问题.但是遇到单点故障就显得无能为力了.MongoDB的副本集可以很好的解决单点故障的问题.所以就有了Sharding+Replica Set ... 
- Mongo之架构部署(Replica Sets+Sharding)
		一.环境 要构建一个 MongoDB Sharding Cluster,需要三种角色: •Shard Server: mongod 实例,用于存储实际的数据块. •Config Server: mon ... 
- MongoDB整理笔记の增加节点
		MongoDB Replica Sets 不仅提供高可用性的解决方案,它也同时提供负载均衡的解决方案,增减Replica Sets 节点在实际应用中非常普遍,例如当应用的读压力暴增时,3 台节点的环境 ... 
- MongoDB整理笔记の管理Replica Sets
		一.读写分离 从库能进行查询,这样可以分担主库的大量的查询请求. 1.先向主库中插入一条测试数据 [root@localhost bin]# ./mongo --port 28010 MongoD ... 
- MongoDB整理笔记のSharding分片
		这是一种将海量的数据水平扩展的数据库集群系统,数据分表存储在sharding 的各个节点上,使用者通过简单的配置就可以很方便地构建一个分布式MongoDB 集群.MongoDB 的数据分块称为 chu ... 
- MongoDB整理笔记の管理Sharding
		1.列出所有的Shard Server > db.runCommand({ listshards: 1 }) --列出所有的Shard Server { "shards" : ... 
随机推荐
- GridControl 添加全选列
			这里通过List对象绑定GridControl,且不用在GirdControl界面中添加任何列,实现CheckBox列的方法 1.列表中出现CheckBox列 非常简单,在绑定的List实体中,增加一 ... 
- 浅析Web Services
			Web Services 可使您的应用程序成为 Web 应用程序. Web Services 通过 Web 进行发布.查找和使用. 1.什么是Web Services? Web Services 是应 ... 
- git commit时候出现的问题
			git commit 提交的时候,出现*** Please tell me who you are. git config --global ...问题 1 $ git commit -a -m 'v ... 
- Image与Bitmap的区别及相互转换
			1. Image.FromFile()返回的是某个继承自Image的具体类的对象,在这里,就是Bitmap或者Metafile其中之一.这应该算是factory pattern的一种形式.所以,Ima ... 
- 在C#中控制ListBox某一行的字体颜色
			例1 private void Form1_Load(object sender, EventArgs e) { listBox1.Items.Add("红色"); listBox ... 
- arcserver 跨域问题
			http://resources.arcgis.com/en/help/rest/apiref/config.html#jcrossOriginAccesshttp://enable-cors.org ... 
- iOS学习之UIPickerView控件的关联选择
			接上篇iOS学习之UIPickerView控件的简单使用 接着上篇的代码 http://download.csdn.net/detail/totogo2010/4391870 ,我们要实现的效果如下: ... 
- 配置siebel捕捉SQL语句
			C:\Siebel\15.0.0.0.0\Client\BIN\siebel.exe /c c:\Siebel\15.0.0.0.0\Client\bin\chs\siebel.cfg /B &quo ... 
- 值得一做》关于一道暴搜BZOJ1024(EASY+)
			为什么要写这道题的DP捏? 原因很简单,因为为原来在openjudge上有一道题叫分蛋糕,有一个思路和这道题很像:“分锅”. 分锅:即为考虑计算当前情况的最优解时,把当前状态结果,分散为考虑当前状态的 ... 
- 通过snmp监控linux
			一.linux snmpd安装 yum install -y net-snmp net-snmp-utils 二.snmp的配置(vim /etc/snmp/snmpd.conf) com2sec n ... 
