mongodb集群搭建

mongodb版本:4.2.2

1.關閉系統(tǒng)防火墻

2.配置域名解析
在3臺虛擬機上分別執(zhí)行以下3條命令,注意替換實際IP地址
echo "192.168.119.137 geekdemo1 member1.example.com member2.example.com" >> /etc/hosts
echo "192.168.119.138 geekdemo2 member3.example.com member4.example.com" >> /etc/hosts
echo "192.168.119.139 geekdemo3 member5.example.com member6.example.com" >> /etc/hosts

3.準備分片目錄
在各服務器上創(chuàng)建數(shù)據(jù)目錄,我們使用/data,請按自己需要修改為其他目錄:
在member1/member3/member5上執(zhí)行以下命令:
mkdir -p /data/shard1/
mkdir -p /data/config/
在member2/member4/member6上執(zhí)行以下命令:
mkdir -p /data/shard2/
mkdir -p /data/mongos/

4.創(chuàng)建第一個分片用的復制集
在member1/member3/member5上執(zhí)行以下命令。
mongod --bind_ip 0.0.0.0 --replSet shard1 --dbpath /data/shard1 --logpath /data/shard1/mongod.log --port 27010 --fork --shardsvr --wiredTigerCacheSizeGB 1

5.初始化第一個分片復制集
mongo --host member1.example.com:27010
rs.initiate({
_id:"shard1",
"members":[
{
"_id":0,
"host":"member1.example.com:27010"
},
{
"_id":1,
"host":"member3.example.com:27010"
},
{
"_id":2,
"host":"member5.example.com:27010"
}
]
});

查看狀態(tài)rs.status();

6.創(chuàng)建config server復制集
在member1/member3/member5上執(zhí)行以下命令
mongod --bind_ip 0.0.0.0 --replSet config --dbpath /data/config --logpath /data/config/mongod.log --port 27019 --fork --configsvr --wiredTigerCacheSizeGB 1

7.初始化config server復制集

mongo --host member1.example.com:27019

rs.initiate({
_id:"config",
"members":[
{
"_id":0,
"host":"member1.example.com:27019"
},
{
"_id":1,
"host":"member3.example.com:27019"
},
{
"_id":2,
"host":"member5.example.com:27019"
}
]
});

8.在第一臺機器上搭建mongos

mongos --bind_ip 0.0.0.0 --logpath /data/mongos/mongos.log --port 27017 --fork --configdb config/member1.example.com:27019,member3.example.com:27019,member5.example.com:27019

連接到mongos,添加分片

mongo --host member1.example.com:27017

sh.addShard("shard1/member1.example.com:27010,member3.example.com:27010,member5.example.com:27010");

9.創(chuàng)建分片表

連接到mongos,創(chuàng)建分片集合

mongo --host member1.example.com:27017

mongos>sh.status()
mongos>sh.enableSharding("foo");
mongos>sh.shardCollection("foo.bar",{_id:'hashed'});
mongos>sh.status();

插入測試數(shù)據(jù)

use foo
for(var i=0;i<10000;i++){
db.bar.insert({i:i});
}

10.創(chuàng)建第2個分片的復制集
在member2/member4/member6上執(zhí)行以下命令。
mongod --bind_ip 0.0.0.0 --replSet shard2 --dbpath /data/shard2 --logpath /data/shard2/mongod.log --port 27011 --fork --shardsvr --wiredTigerCacheSizeGB 1

11.初始化第二個分片的復制集
mongo --host member2.example.com:27011
rs.initiate({
_id:"shard2",
"members":[
{
"_id":0,
"host":"member2.example.com:27011"
},
{
"_id":1,
"host":"member4.example.com:27011"
},
{
"_id":2,
"host":"member6.example.com:27011"
}
]
});

12.加入第2個分片

連接到mongos,添加分片

mongo --host member1.example.com:27017

sh.addShard("shard2/member2.example.com:27011,member4.example.com:27011,member6.example.com:27011");
sh.status();

測試:

連接mongos

mongo --host member1.example.com:27017

連接分片1

mongo --host member1.example.com:27010

連接分片2

mongo --host member2.example.com:27011

查詢所有的文檔:
use foo;
db.bar.find({});
db.bar.count();
db.bar.find({i:"10001"});
db.bar.insert({i:10001});

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

相關閱讀更多精彩內容

  • 1 部署方案圖 端口的規(guī)劃: mongos為 20000 config server 為 21000 shard1...
    蓋世猴紙閱讀 273評論 0 0
  • 自己是在一臺window機器部署整套,包含了一個分片Shard,分片包含了三個復制集(1個primary,2個se...
    4f03f33f33be閱讀 541評論 0 0
  • 拓撲結構介紹:(兩個分片的分片集群) 共采用3臺虛擬機進行模擬,采用的是一機多實例的模式 hosts文件: 192...
    深度遺忘閱讀 823評論 0 0
  • 在這此前建議先了解一下mongo的片鍵概念,結合自己的業(yè)務關系確定一個合適的片鍵,才能更好的發(fā)揮集群的作用。 進入...
    繁繁繁閱讀 800評論 0 1
  • 系統(tǒng)版本 Centos7 系統(tǒng)用戶 root MongoDB版本 3.4.2 s...
    IT_小白閱讀 1,268評論 0 1

友情鏈接更多精彩內容