mongodb實戰(zhàn)Replica Set + Sharding

環(huán)境說明

三臺機(jī)器(centos6.4)

  • host1 192.168.1.201
  • host2 192.168.1.202
  • host3 192.168.1.203

mongdb版本

2.4.0

服務(wù)規(guī)劃

繪圖1.jpg
  • shard1 28017:shard1的數(shù)據(jù)服務(wù)
  • shard2 28018:shard2的數(shù)據(jù)服務(wù)
  • config 20000:存儲分片集群的的元數(shù)據(jù),其中包括在每個mongod實例的基本信息和塊信息
  • arbiter1 28031:shard1的仲裁節(jié)點
  • arbiter2 28032:shard2的仲裁節(jié)點
  • mongos 28885:數(shù)據(jù)和請求分發(fā)的中心,使單一的mongod實例組成互相關(guān)聯(lián)的集群

仲裁節(jié)點是一種特殊的節(jié)點,它本身不存儲數(shù)據(jù),主要的作用是決定哪一個備用節(jié)點在主節(jié)點掛掉之后提升為主節(jié)點。

配置文件

host1

數(shù)據(jù)節(jié)點

shard11表示分片一的第一個節(jié)點
shard21表示分片二的第一個節(jié)點

mkdir -p /home/ayou/mongodb/shard11
cat > /home/ayou/mongodb/shard11.conf <<EOF
shardsvr=true
replSet=shard1
port=28017
dbpath=/home/ayou/mongodb/shard11
oplogSize=2048
logpath=/home/ayou/mongodb/shard11.log
logappend=true
fork=true
rest=true
nojournal=true
EOF
mkdir -p /home/ayou/mongodb/shard21
cat > /home/ayou/mongodb/shard21.conf <<EOF
shardsvr=true
replSet=shard2
port=28018
dbpath=/home/ayou/mongodb/shard21
oplogSize=2048
logpath=/home/ayou/mongodb/shard21.log
logappend=true
fork=true
rest=true
nojournal=true
EOF

配置節(jié)點

mkdir -p /home/ayou/mongodb/config
cat > /home/ayou/mongodb/config1.conf <<EOF
configsvr=true
dbpath=/home/ayou/mongodb/config/
port=20000
logpath=/home/ayou/mongodb/config1.log
logappend=true
fork=true
nojournal=true
EOF

表決節(jié)點

arbiter1表示為分片一的仲裁節(jié)點
arbiter2表示為分片二的仲裁節(jié)點

mkdir -p /home/ayou/mongodb/arbiter1
cat > /home/ayou/mongodb/arbiter1.conf <<EOF
shardsvr=true
replSet=shard1
port=28031
dbpath=/home/ayou/mongodb/arbiter1
oplogSize=100
logpath=/home/ayou/mongodb/arbiter1.log
logappend=true
fork=true
rest=true
nojournal=true
EOF
mkdir -p /home/ayou/mongodb/arbiter2
cat > /home/ayou/mongodb/arbiter2.conf <<EOF
shardsvr=true
replSet=shard2
port=28032
dbpath=/home/ayou/mongodb/arbiter2
oplogSize=100
logpath=/home/ayou/mongodb/arbiter2.log
logappend=true
fork=true
rest=true
nojournal=true
EOF

路由節(jié)點

mkdir -p /home/ayou/mongodb/mongos1
cat > /home/ayou/mongodb/mongos1.conf <<EOF
configdb=host1:20000,host2:20000,host3:20000
port=28885
chunkSize=100
logpath=/home/ayou/mongodb/mongos1.log
logappend=true
fork=true
EOF

host2

數(shù)據(jù)節(jié)點

mkdir -p /home/ayou/mongodb/shard12
cat > /home/ayou/mongodb/shard12.conf <<EOF
shardsvr=true
replSet=shard1
port=28017
dbpath=/home/ayou/mongodb/shard12
oplogSize=2048
logpath=/home/ayou/mongodb/shard12.log
logappend=true
fork=true
rest=true
nojournal=true
EOF
mkdir -p /home/ayou/mongodb/shard22
cat > /home/ayou/mongodb/shard22.conf <<EOF
shardsvr=true
replSet=shard2
port=28018
dbpath=/home/ayou/mongodb/shard22
oplogSize=2048
logpath=/home/ayou/mongodb/shard22.log
logappend=true
fork=true
rest=true
nojournal=true
EOF

配置節(jié)點

mkdir -p /home/ayou/mongodb/config
cat > /home/ayou/mongodb/config2.conf <<EOF
configsvr=true
dbpath=/home/ayou/mongodb/config/
port=20000
logpath=/home/ayou/mongodb/config2.log
logappend=true
fork=true
EOF

表決節(jié)點

mkdir -p /home/ayou/mongodb/arbiter1
cat > /home/ayou/mongodb/arbiter1.conf <<EOF
shardsvr=true
replSet=shard1
port=28031
dbpath=/home/ayou/mongodb/arbiter1
oplogSize=100
logpath=/home/ayou/mongodb/arbiter1.log
logappend=true
fork=true
rest=true
nojournal=true
EOF
mkdir -p /home/ayou/mongodb/arbiter2
cat > /home/ayou/mongodb/arbiter2.conf <<EOF
shardsvr=true
replSet=shard2
port=28032
dbpath=/home/ayou/mongodb/arbiter2
oplogSize=100
logpath=/home/ayou/mongodb/arbiter2.log
logappend=true
fork=true
rest=true
nojournal=true
EOF

路由節(jié)點

mkdir -p /home/ayou/mongodb/mongos2
cat > /home/ayou/mongodb/mongos2.conf <<EOF
configdb=host1:20000,host2:20000,host3:20000
port=28885
chunkSize=100
logpath=/home/ayou/mongodb/mongos2.log
logappend=true
fork=true
EOF

host3

數(shù)據(jù)節(jié)點

mkdir -p /home/ayou/mongodb/shard13
cat > /home/ayou/mongodb/shard13.conf <<EOF
shardsvr=true
replSet=shard1
port=28017
dbpath=/home/ayou/mongodb/shard13
oplogSize=2048
logpath=/home/ayou/mongodb/shard13.log
logappend=true
fork=true
rest=true
nojournal=true
EOF
mkdir -p /home/ayou/mongodb/shard23
cat > /home/ayou/mongodb/shard23.conf <<EOF
shardsvr=true
replSet=shard2
port=28018
dbpath=/home/ayou/mongodb/shard23
oplogSize=2048
logpath=/home/ayou/mongodb/shard23.log
logappend=true
fork=true
rest=true
nojournal=true
EOF

配置節(jié)點

mkdir -p /home/ayou/mongodb/config
cat > /home/ayou/mongodb/config3.conf <<EOF
configsvr=true
dbpath=/home/ayou/mongodb/config/
port=20000
logpath=/home/ayou/mongodb/config3.log
logappend=true
fork=true
EOF

表決節(jié)點

mkdir -p /home/ayou/mongodb/arbiter1
cat > /home/ayou/mongodb/arbiter1.conf <<EOF
shardsvr=true
replSet=shard1
port=28031
dbpath=/home/ayou/mongodb/arbiter1
oplogSize=100
logpath=/home/ayou/mongodb/arbiter1.log
logappend=true
fork=true
rest=true
nojournal=true
EOF
mkdir -p /home/ayou/mongodb/arbiter2
cat > /home/ayou/mongodb/arbiter2.conf <<EOF
shardsvr=true
replSet=shard2
port=28032
dbpath=/home/ayou/mongodb/arbiter2
oplogSize=100
logpath=/home/ayou/mongodb/arbiter2.log
logappend=true
fork=true
rest=true
nojournal=true
EOF

路由節(jié)點

mkdir -p /home/ayou/mongodb/mongos3
cat > /home/ayou/mongodb/mongos3.conf <<EOF
configdb=host1:20000,host2:20000,host3:20000
port=28885
chunkSize=100
logpath=/home/ayou/mongodb/mongos3.log
logappend=true
fork=true
EOF

啟動服務(wù)

  • mongos服務(wù)可以只啟動一臺,本文在host3上啟動
  • 如果啟動不了,可以嘗試同步時間/usr/sbin/ntpdate * *表示需要與之同步的機(jī)器名

host1

mongod --config /home/ayou/mongodb/shard11.conf
mongod --config /home/ayou/mongodb/shard21.conf
mongod --config /home/ayou/mongodb/arbiter1.conf
mongod --config /home/ayou/mongodb/arbiter2.conf
mongod --config /home/ayou/mongodb/config1.conf
mongos --config /home/ayou/mongodb/mongos1.conf 

host2

mongod --config /home/ayou/mongodb/shard12.conf
mongod --config /home/ayou/mongodb/shard22.conf
mongod --config /home/ayou/mongodb/arbiter1.conf
mongod --config /home/ayou/mongodb/arbiter2.conf
mongod --config /home/ayou/mongodb/config2.conf
mongos --config /home/ayou/mongodb/mongos2.conf

host3

mongod --config /home/ayou/mongodb/shard13.conf
mongod --config /home/ayou/mongodb/shard23.conf
mongod --config /home/ayou/mongodb/arbiter1.conf
mongod --config /home/ayou/mongodb/arbiter2.conf
mongod --config /home/ayou/mongodb/config3.conf
mongos --config /home/ayou/mongodb/mongos3.conf

啟動路由

任選一臺機(jī)器,執(zhí)行如下命令

cat > /home/ayou/mongodb/mongos.conf <<EOF
configdb=host1:20000,host2:20000,host3:20000
port=41000
chunkSize=100
logpath=/home/ayou/mongodb/mongos.log
logappend=true
fork=true
EOF

mongos --config /home/ayou/mongodb/mongos.conf

查看運行狀態(tài)

Paste_Image.png
Paste_Image.png
Paste_Image.png

配置分片

  • 可以在任意臺機(jī)器上進(jìn)行如下操作
  • 可能需要關(guān)閉防火墻
mongo host1:28017/admin

config = {_id:'shard1', members: [{_id: 0, host: "host1:28017"},{_id: 1, host:"host2:28017"},{ _id : 2,host:"host3:28017",slaveDelay :7200 ,priority:0},{_id: 3, host: 'host1:28031', arbiterOnly : true},{_id: 4,host: 'host2:28031', arbiterOnly : true},{_id: 5, host: 'host3:28031',arbiterOnly : true}]};
rs.initiate(config);
mongo host2:28018/admin

config = {_id:'shard2', members: [{_id: 0, host: "host1:28018"},{_id: 1, host:"host2:28018"},{ _id : 2,host:"host3:28018",slaveDelay :7200 ,priority:0},{_id: 3, host: 'host1:28032', arbiterOnly : true},{_id: 4,host: 'host2:28032', arbiterOnly : true},{_id: 5, host: 'host3:28032',arbiterOnly : true}]};
rs.initiate(config);

添加分片

任選一臺機(jī)器

mongo host3:28885/admin
db.runCommand({"addshard" : "shard1/host1:28017,host2:28017"})

db.runCommand({"addshard" : "shard2/host1:28018,host2:28018"})

db.runCommand( { "enablesharding" : "test-shard"})

db.runCommand( { shardcollection : "test-shard.test",key : {_id: 1} } )

測試

往test-shard.test中插入大量數(shù)據(jù)

for(var i=0;i<1000000;i++) db.test.insert({name:"no"+i})

查看分片狀態(tài)

Paste_Image.png
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 配置服務(wù)器,全部設(shè)置為靜態(tài)地址 轉(zhuǎn)到/home目錄,創(chuàng)建一個mongo目錄用做實驗,我們需要創(chuàng)建如下的目錄層級和空...
    justonlyyo閱讀 1,488評論 0 0
  • MongoDB基礎(chǔ)請參考:http://blog.51cto.com/kaliarch/2044423 Mongo...
    kaliarch閱讀 2,180評論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,711評論 19 139
  • 剛接觸MongoDB,就要用到它的集群,只能硬著頭皮短時間去看文檔和嘗試自行搭建。遷移歷史數(shù)據(jù)更是讓人惱火,近10...
    davidpp閱讀 51,914評論 9 78
  • 牧牛出診閱讀 210評論 0 0

友情鏈接更多精彩內(nèi)容