一、预准备环境:
本次搭建按照3+3+9的模式进行伪分布式结构(3个mongos对外提供服务,3个configServer保存配置信息,3个mongod存储数据,3个mongod作为数据副本,3个mongod作为仲裁节点);
(1)为配置服务configServer创建集群文件夹保存服务的信息:
例:
mkdir /home/mongo/cluster/config1/data;
mkdir /home/mongo/cluster/config1/log;
(2)为分片存储服务mongod集群文件夹保存服务的信息:
例:
mkdir /home/mongo/cluster/shard1/data;
mkdir /home/mongo/cluster/shard1/log;
二、集群
1、分别启动各个configServer服务,分别占21000,21001,21002端口:
mongod --configsvr --dbpath /home/mongo/cluster/config1/data --port 21000 --logpath /home/mongo/cluster/config1/log/config.log --relSet cs --fork
注:3.2 的版本不需要 –relSet cs 参数,3.4版本必须要有,否则无法后续启动mongose服务。如果没有写入configServer的配置信息,mongos服务将拒绝连接。
2、分别登陆configServer写入相关配置:
[root@localhost ~]#mongo 192.168.1.122:21000
mongo>use admin
mongo>cfg = {
_id:'cs',configsvr:true,members:[
{_id:0,host:'10.45.32.118:21000'}
]
};
mongo>rs.initiate(cfg);
//=============我是分割线===================//
[root@localhost ~]#mongo 192.168.1.122:21001
mongo>use admin
mongo>cfg = {
_id:'cs',host:'10.45.32.118:21001'}
]
};
mongo>rs.initiate(cfg);
//=============我是分割线===================//
[root@localhost ~]#mongo 192.168.1.122:21002
mongo>use admin
mongo>cfg = {
_id:'cs', configsvr:true, members:[
{_id:0,host:'10.45.32.118:21002'}
]
};
mongo>rs.initiate(cfg);
3、分别启动分片存储服务,占22001-22009端口:
mongod --shardsvr --replSet shard1 --port 22001 --dbpath /home/mongo/cluster/shard1/data --logpath /home/mongo/cluster/shard1/log/shard1.log --fork --nojournal --oplogSize 10
4、设置分片信息:
分别登录分片
//(1)登陆mongo 192.168.1.122:22001
[root@localhost ~]# mongo 192.168.1.122:22001
>use admin;
>config = { _id:"shard1",members:[
{_id:0,host:"192.168.1.122:22001"}, {_id:1,host:"192.168.1.122:22002"}, {_id:2,host:"192.168.1.122:22003",arbiterOnly:true}
]
};
>rs.initiate(config);
>exit
//(2)再登陆 mongo 192.168.1.122:22004
[root@localhost ~]# mongo 192.168.1.122:22004
>use admin;
>config = { _id:"shard1",members:[
{_id:0,host:"192.168.1.122:22004"},host:"192.168.1.122:22005"}, {_id:2,host:"192.168.1.122:22006",arbiterOnly:true}
]
};
>rs.initiate(config);
//(3)登陆mongo 192.168.1.122:22007;
[root@localhost ~]# mongo 192.168.1.122:22007
>use admin;
>config = { _id:"shard1",host:"192.168.1.122:22007"}, {_id:1,host:"192.168.1.122:22008"}, {_id:2,host:"192.168.1.122:22009",arbiterOnly:true}
]
};
>rs.initiate(config);
4、分别启动mongos,分别占:23000,23001,23002端口:
[root@localhost ~]# mongos --configdb cs/192.168.1.122:21000,192.168.1.122:21001,192.168.1.122:21002 --port 23000
[root@localhost ~]# mongos --configdb cs/192.168.1.122:21000,192.168.1.122:21002 --port 23001
[root@localhost ~]# mongos --configdb cs/192.168.1.122:21000,192.168.1.122:21002 --port 23002
5、连接到mongos,串联路由服务器与分片服务器:
(1)登陆mongo 192.168.1.122:23000;
[root@localhost ~]#mongo 192.168.1.122:23000
>use admin;
>>db.runCommand( { addshard : "shard1/192.168.1.122:22001,92.168.1.122:22002,92.168.1.122:22003"});
>db.runCommand( { addshard : "shard2/192.168.1.122:22004,92.168.1.122:22005,92.168.1.122:22006"});
>db.runCommand( { addshard : "shard2/192.168.1.122:22007,92.168.1.122:22008,92.168.1.122:22009"});
#查看分片服务器的配置
>db.runCommand( { listshards : 1 } );
(2)指定分片与测试;
>use acer;
>db.createCollection('test');
>db.runCommand( { enablesharding :"acer"});
#指定为哪个库的哪个集合创建分片,片键
>sh.shardCollection("acer.test",{"Uid":1}); #此命令指的是为acer库的test集合进行分片,片键为Uid,
#插入测试数据:
>use acer
>for (var i = 1; i <= 100000; i++) db.test.save({"Uid":i,"Name":"zhanjindong","Age":13,"Date":new Date()});
#查看分片结果
>db.test.stats();