一、預準備環境:
本次搭建按照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:21000mongo>use adminmongo>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:21001mongo>use adminmongo>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:21002mongo>use adminmongo>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"}, {_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);
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: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
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();
轉載自:http://www.cnblogs.com/jarven/p/6370353.html