本次搭建按照3+3+9的模式进行伪分布式结构(3个mongos对外提供服务,3个configServer保存配置信息,3个mongod存储数据,3个mongod作为数据副本,3个mongod作为仲裁节点);
例:
mkdir /home/mongo/cluster/config1/data;
mkdir /home/mongo/cluster/config1/log;
例:
mkdir /home/mongo/cluster/shard1/data;
mkdir /home/mongo/cluster/shard1/log;
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服务将拒绝连接。
[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',
configsvr:true,
members:[
{_id:0,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);
mongod --shardsvr --replSet shard1 --port 22001 --dbpath /home/mongo/cluster/shard1/data --logpath /home/mongo/cluster/shard1/log/shard1.log --fork --nojournal --oplogSize 10
分别登录分片
//(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"},
{_id:1,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", members:[
{_id:0,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);
[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:21001,192.168.1.122:21002 --port 23001
[root@localhost ~]# mongos --configdb cs/192.168.1.122:21000,192.168.1.122:21001,192.168.1.122:21002 --port 23002
(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();
转载自:http://www.cnblogs.com/jarven/p/6370353.html