天天看點

Mongo分片+副本集叢集搭建測試

mongo相關概念闡述

請移步mongo官網

mongo副本集相關概念

mongo分片相關概念

搭建思路

副本集作為備份,防止将所有的資料存放在主機上,而主機出現了故障而造成的資料丢失,分片搭建是為了存儲海量資料。

搭建圖示

Mongo分片+副本集叢集搭建測試

伺服器準備

伺服器192.168.80.130

伺服器192.168.80.133

伺服器192.168.80.135

伺服器192.168.1.143

mongo伺服器部署

伺服器192.168.80.130 伺服器192.168.80.133 伺服器192.168.80.135 伺服器 192.168.1.143
Mongos mongos mongos
config server config server config server shard serverB 副節點
shard serverA 主節點 shard serverA 副節點 shard serverB 主節點 shard serverA 仲裁
shard serverB 仲裁

端口配置

mongos ----- 20001

config server ----- 21001

shard server ----- 27004

仲裁節點 ------27005

配置檔案

建好配置檔案

mkdir -p /usr/local/mongodb/mongons/log
mkdir -p /usr/local/mongodb/confign/data
mkdir -p /usr/local/mongodb/confign/log
mkdir -p /usr/local/mongodb/shardA/data
mkdir -p /usr/local/mongodb/shardA/log
mkdir -p /usr/local/mongodb/shardB/data
mkdir -p /usr/local/mongodb/shardB/log
           

config server配置

vi /usr/local/mongodb/conf/confign.conf
           
confign.conf

伺服器130 133 135 的配置confign.conf

pidfilepath = /usr/local/mongodb/confign/log/configsrv.pid
dbpath = /usr/local/mongodb/confign/data
logpath = /usr/local/mongodb/confign/log/congigsrv.log
logappend = true
 
bind_ip = 0.0.0.0
port = 21001
fork = true
 
#declare this is a config db of a cluster;
configsvr = true

#副本集名稱
replSet=configns
 
#設定最大連接配接數
maxConns=20000
           
啟動三台伺服器的config server
mongod -f /usr/local/mongodb/conf/confign.conf
           
登入任意一台配置伺服器,初始化配置副本集
#連接配接
mongo --port 21001
#config變量
config = {
...    _id : "configns",
...     members : [
...         {_id : 0, host : "192.168.80.130:21001" },
...         {_id : 1, host : "192.168.80.133:21001" },
...         {_id : 2, host : "192.168.80.135:21001" }
...     ]
... }

#初始化副本集
rs.initiate(config)
           
配置仲裁節點

配置shardA的仲裁節點—在伺服器192.168.1.143上

vi /usr/local/mongodb/conf/shardArbiter.conf
           
#配置檔案内容
#——————————————–
pidfilepath = /usr/local/mongodb/shardA/log/shardA.pid
dbpath = /usr/local/mongodb/shardA/data
logpath = /usr/local/mongodb/shardA/log/shardA.log
logappend = true

bind_ip = 0.0.0.0
port = 27005
fork = true
 
#打開web監控
httpinterface=true
rest=true
 
#副本集名稱
replSet=shardA
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#設定最大連接配接數
maxConns=20000
           

配置shardB的仲裁節點—在伺服器192.168.80.130上

vi /usr/local/mongodb/conf/shardBArbiter.conf
           
#配置檔案内容
#——————————————–
pidfilepath = /usr/local/mongodb/shardB/log/shardB.pid
dbpath = /usr/local/mongodb/shardB/data
logpath = /usr/local/mongodb/shardB/log/shardB.log
logappend = true

bind_ip = 0.0.0.0
port = 27005
fork = true
 
#打開web監控
httpinterface=true
rest=true
 
#副本集名稱
replSet=shardB
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#設定最大連接配接數
maxConns=20000
           
配置shardA分片副本集

伺服器130 伺服器133 伺服器143(仲裁)

vi /usr/local/mongodb/conf/shardA.conf
           
#配置檔案内容
#——————————————–
pidfilepath = /usr/local/mongodb/shardA/log/shardA.pid
dbpath = /usr/local/mongodb/shardA/data
logpath = /usr/local/mongodb/shardA/log/shardA.log
logappend = true

bind_ip = 0.0.0.0
port = 27004
fork = true
 
#打開web監控
httpinterface=true
rest=true
 
#副本集名稱
replSet=shardA
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#設定最大連接配接數
maxConns=20000
           

啟動三台伺服器的shardA server

mongod -f /usr/local/mongodb/conf/shardA.conf
           

登陸任意一台伺服器,初始化副本集

mongo --port 27004
#使用admin資料庫
use admin
#定義副本集配置,第三個節點的 "arbiterOnly":true 代表其為仲裁節點。
config = {
...    _id : "shardA",
...     members : [
...         {_id : 0, host : "192.168.80.130:27004" },
...         {_id : 1, host : "192.168.80.133:27004" },
...         {_id : 2, host : "192.168.1.143:27005" , arbiterOnly: true }
...     ]
... }
#初始化副本集配置
rs.initiate(config);
           
配置shardB分片副本集

伺服器130(仲裁) 伺服器135(主節點) 伺服器143(副節點)

vi /usr/local/mongodb/conf/shardB.conf
           
#配置檔案内容
#——————————————–
pidfilepath = /usr/local/mongodb/shardB/log/shardB.pid
dbpath = /usr/local/mongodb/shardB/data
logpath = /usr/local/mongodb/shardB/log/shardB.log
logappend = true

bind_ip = 0.0.0.0
port = 27004
fork = true
 
#打開web監控
httpinterface=true
rest=true
 
#副本集名稱
replSet=shardB
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#設定最大連接配接數
maxConns=20000
           

啟動三台伺服器的shardB server

mongod -f /usr/local/mongodb/conf/shardB.conf
           

登陸任意一台伺服器,初始化副本集

mongo --port 27004
#使用admin資料庫
use admin
#定義副本集配置,第三個節點的 "arbiterOnly":true 代表其為仲裁節點。
config = {
...    _id : "shardB",
...     members : [
...         {_id : 0, host : "192.168.80.130:27005" , arbiterOnly: true},
...         {_id : 1, host : "192.168.80.135:27004" },
...         {_id : 2, host : "192.168.1.143:27004" }
...     ]
... }
#初始化副本集配置
rs.initiate(config);
           
配置路由伺服器 mongos

先啟動配置伺服器和分片伺服器,後啟動路由執行個體啟動路由執行個體:(兩台機器 130 135)

mongos -f /usr/local/mongodb/conf/mongons.conf

vi /usr/local/mongodb/conf/mongons.conf
           
#内容
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = true

bind_ip = 0.0.0.0
port = 20001
fork = true

#監聽的配置伺服器,隻能有1個或者3個 configs為配置伺服器的副本集名字
configdb = configns/192.168.80.130:21001,192.168.80.133:21001,192.168.80.135:21001

#設定最大連接配接數
maxConns=20000
           
啟用分片

目前搭建了mongodb配置伺服器、路由伺服器,各個分片伺服器,不過應用程式連接配接到mongos路由伺服器并不能使用分片機制,還需要在程式裡設定分片配置,讓分片生效。

登陸任意一台mongos

mongo --port 20001
#使用admin資料庫
user  admin
#串聯路由伺服器與配置設定副本集
sh.addShard("shardA/192.168.80.130:27004,192.168.80.133:27004,192.168.1.143:27005")
sh.addShard("shardB/192.168.80.130:27005,192.168.80.135:27004,192.168.1.143:27004")
#檢視叢集狀态
sh.status()
           

設定需要分片的資料庫和表(需要在admin庫裡設定)

#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定資料庫裡需要分片的集合和片鍵
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
#設定資料
for(var i=1;i<=1000;i++){
	db.table1.save({"id":i,"x":Math.random(),"name":"xubo","time":"20150819","ops":"testinserttimes"});
}
           
測試結果

130—serverA主節點 —存儲1條資料

133—serverA副節點 —存儲1條資料

135—serverB主節點 —存儲999條資料

143—serverB副節點 —存儲999條資料