欢迎投稿

今日深度:

【MongoDB】在windows平台下搭建mongodb的分片集群(二

【MongoDB】在windows平台下搭建mongodb的分片集群(二),mongodb集群


在上一片博客中我们讲了Mongodb数据库中分片集群的主要原理。在本篇博客中我们主要讲描述分片集群的搭建过程。配置分片集群主要有两个步骤,第一启动所有需要的mongodmongos进程。第二步就是启动一个mongos与集群通信。下面我们一步步来描述集群的搭建过程。


1-1  MongoDB分片集群中的组件


图 1-2 由实例分片集群构成的进程全貌


备注:以上两者图片均引自于Kyle Banker著 丁雪丰译《MongoDB 实战》  

2.1 创建分片目录

备注:此处于创建复制集类似,详细请查看博客:http://blog.csdn.net/sxb0841901116/article/details/40596361


2.2 启动副本集

    我们用如下命令启动两个副本集:

mongod --shardsvr --replSet shard-a --dbpath "D:\Program Files\mongodb\data\slide\data\rs-a-1" --port 3000 --logpath "D:\Program Files\mongodb\data\slide\log\rs-a-1.log" --nojournal

mongod --shardsvr --replSet shard-a --dbpath "D:\Program Files\mongodb\data\slide\data\rs-a-2" --port 3001 --logpath "D:\Program Files\mongodb\data\slide\log\rs-a-2.log" --nojournal

mongod --shardsvr --replSet shard-a --dbpath "D:\Program Files\mongodb\data\slide\data\rs-a-3" --port 3002 --logpath "D:\Program Files\mongodb\data\slide\log\rs-a-3.log" --nojournal

mongod --shardsvr --replSet shard-b --dbpath "D:\Program Files\mongodb\data\slide\data\rs-b-1" --port 30100 --logpath "D:\Program Files\mongodb\data\slide\log\rs-b-1.log" --nojournal

mongod --shardsvr --replSet shard-b --dbpath "D:\Program Files\mongodb\data\slide\data\rs-b-2" --port 30101 --logpath "D:\Program Files\mongodb\data\slide\log\rs-b-2.log" --nojournal

mongod --shardsvr --replSet shard-b --dbpath "D:\Program Files\mongodb\data\slide\data\rs-b-3" --port 30102 --logpath "D:\Program Files\mongodb\data\slide\log\rs-b-3.log" --nojournal

当成功运行后,会出现类似于下面的6个黑窗口:


2.3 启动副本集

   启动第一副本集shard-a

 

大家可用用rs.status()命令来查看第一副本集的状态。 

启动第第二副本集shard-b

 

2.4 创建配置服务器目录





2.5 用如下命令启动配置服务器

mongod --configsvr --dbpath "D:\Program Files\mongodb\data\slide\data\config-1" --port 27019 --logpath "D:\Program Files\mongodb\data\slide\log\config-1.log" --nojournal
mongod --configsvr --dbpath "D:\Program Files\mongodb\data\slide\data\config-2" --port 27020 --logpath "D:\Program Files\mongodb\data\slide\log\config-2.log" --nojournal
mongod --configsvr --dbpath "D:\Program Files\mongodb\data\slide\data\config-3" --port 27021 --logpath "D:\Program Files\mongodb\data\slide\log\config-3.log" --nojournal

shell连接或者查看log文,确保每台配置服务器都已启动并正常运行,并验证每个进程都在监听配置的端口。查看每台配置服务器的日志,应该能看到这样的内容。 

 

查看任务管理窗口,正在运行的进程:


2.6 启动Mongos

启动mongos必须使用configdb选项来启动。它接受一个用逗号分割的配置服务器地址列表:

 

Mongos --configdb WIN--20141018KO:27019,WIN--20141018KO:27020,WIN--20141018KO:27021 --logpath "D:\Program Files\mongodb\data\slide\log\mongos.log" --port 40000

二、 配置集群

登陆mongos上,查看可用的命令:

 

 

把分片shard-a, shard-b添加到分片集群中:

 

 

两种方式查看分片集群:

 

 



mongodb集群配置好了之后怎保存,下次开机还可以使用?windows环境

本来就是可以的
对于副本集模式,集群参数是写在每一个节点内部的
对于分片模式,集群参数写在配置服务器里
 

mongodb在加keyfile的情况下,怎实现分机集群部署,1台路由,3台配置,三台分片

在配置分片的节点
mongod --port 27017 --fork --logpath ar/log/mongo_shard1.log --dbpath /data0/mongo/shard1 --shardsvr
mongod --port 27018 --fork --logpath ar/log/mongo_shard2.log --dbpath /data0/mongo/shard2 --shardsvr
mongod --port 27217 --fork --logpath ar/log/mongo_config.log --dbpath /data0/mongo/config --configsvr
配置路由
mongos --port 27417 --fork --logpath ar/log/mongos.log --configdb 127.0.0.1:27217 --chunkSize 1
在客户端配置shard
mongo --port 27417
MongoDB shell version: 1.6.5
connecting to: 127.0.0.1:27417/test
> use admin;
switched to db admin
> db.runCommand({addshard:"127.0.0.1:27017"})
{ "shardAdded" : "shard0000", "ok" : 1 }
> db.runCommand({addshard:"127.0.0.1:27018"})
{ "shardAdded" : "shard0001", "ok" : 1 }

下面我们为DataBase “foo”启用Sharding,并将其中的 Collection “col” 的 shard key设置为“{_id: 1}”,用来测试Sharding功能:
> db.runCommand({enablesharding:'foo'});
{ "ok" : 1 }
> db.runCommand({shardcollection:"foo.col", key:{_id:1}});
{ "collectionsharded" : "foo.col", "ok" : 1 }
 

www.htsjk.Com true http://www.htsjk.com/shujukunews/4493.html NewsArticle 【MongoDB】在windows平台下搭建mongodb的分片集群(二),mongodb集群 在上一片博客中我们讲了 Mongodb 数据库中分片集群的主要原理。在本篇博客中我们主要讲描述分片集群的搭建过程。配置分...
评论暂时关闭