首先选择一个目录在其中建立以下2个文件夹:data和log
在data下建立9个文件夹:
其中前3个为配置服务器所在文件夹,按照官网要求,一个集群需要3个config server
rs-a-n和rs-b-n为两个replica set(副本集) ,我们的目的就是将它们给集群了。
一.初始化副本集
初始化mongod:
初始化副本集,这里形成两个副本集shard-a和shard-b,图示为shard-a初始化:(Wonder为我的主机名)
同理,shard-b也可以初始化。使用rs.status()可以查看副本集的状态:
shard-a:PRIMARY> rs.status()
{
"set" : "shard-a",
"date" : ISODate("2015-08-04T12:11:31.793Z"),
"myState" : ,
"members" : [
{
"_id" : ,
"name" : "Wonder:30000",
"health" : ,
"state" : ,
"stateStr" : "PRIMARY",
"uptime" : ,
"optime" : Timestamp(, ),
"optimeDate" : ISODate("2015-08-04T12:11:07Z"),
"electionTime" : Timestamp(, ),
"electionDate" : ISODate("2015-08-04T12:04:58Z"),
"configVersion" : ,
"self" : true
},
{
"_id" : ,
"name" : "Wonder:30001",
"health" : ,
"state" : ,
"stateStr" : "SECONDARY",
"uptime" : ,
"optime" : Timestamp(, ),
"optimeDate" : ISODate("2015-08-04T12:11:07Z"),
"lastHeartbeat" : ISODate("2015-08-04T12:11:29.800Z"),
"lastHeartbeatRecv" : ISODate("2015-08-04T12:11:30.115Z"
),
"pingMs" : ,
"syncingTo" : "Wonder:30000",
"configVersion" :
},
{
"_id" : ,
"name" : "Wonder:30002",
"health" : ,
"state" : ,
"stateStr" : "SECONDARY",
"uptime" : ,
"optime" : Timestamp(, ),
"optimeDate" : ISODate("2015-08-04T12:11:07Z"),
"lastHeartbeat" : ISODate("2015-08-04T12:11:29.800Z"),
"lastHeartbeatRecv" : ISODate("2015-08-04T12:11:29.804Z"
),
"pingMs" : ,
"configVersion" :
}
],
"ok" :
}
二.启动配置服务器:
三.启动mongos,配置集群
启动mongos,将config server的信息传入
将shard加入mongos:
查看mongos的集群:
四.选择分片键:
选择分片collection:
设置分片键,这里仅仅简单的以_id为分片键:
再查看此时config server的样子状态:
我向mongos插入了一些数据后,查看分片情况: