mongodb實戰Replica Set + Sharding

環境說明

三臺機器(centos6.4)

  • host1 192.168.1.201
  • host2 192.168.1.202
  • host3 192.168.1.203

mongdb版本

2.4.0

服務規劃

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

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

配置文件

host1

數據節點

shard11表示分片一的第一個節點
shard21表示分片二的第一個節點

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

配置節點

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

表決節點

arbiter1表示為分片一的仲裁節點
arbiter2表示為分片二的仲裁節點

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

路由節點

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

數據節點

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

配置節點

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

表決節點

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

路由節點

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

數據節點

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

配置節點

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

表決節點

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

路由節點

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

啟動服務

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

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

啟動路由

任選一臺機器,執行如下命令

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

查看運行狀態

Paste_Image.png
Paste_Image.png
Paste_Image.png

配置分片

  • 可以在任意臺機器上進行如下操作
  • 可能需要關閉防火墻
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);

添加分片

任選一臺機器

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中插入大量數據

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

查看分片狀態

Paste_Image.png
Paste_Image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容