MongoDBレプリケーション+シャーディング

 

https://changineer.info/server/nosql/nosql_mongo_sharding.html

前提
OS : Rocky Linux 9.1
MongoDB version : 6.0.3

mmm190 : mongos
mmm191 : MongoDB(config) primary
mmm192 : MongoDB(config) secondary
mmm193 : MongoDB(config) secondary
mmm194 : MongoDB(Shard#1) primary
mmm195 : MongoDB(Shard#1) secondary
mmm196 : MongoDB(Shard#1) secondary
mmm197 : MongoDB(Shard#2) primary
mmm198 : MongoDB(Shard#2) secondary
mmm199 : MongoDB(Shard#2) secondary

 

-- 1. MongoDBインストール[全ノードで実施]

vim /etc/yum.repos.d/mongodb-org-6.0.repo

[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc


dnf install -y mongodb-org

 

systemctl enable mongod
systemctl restart mongod
systemctl status mongod

mongod --version

-- 2. Config Server設定[mmm191,mmm192,mmm193で実施]

vim /etc/mongod.conf

bindIp: 0.0.0.0

replication:
  replSetName: rs01
  oplogSizeMB: 1024

sharding:
  clusterRole: configsvr


systemctl restart mongod

-- 3. Config Server構築[mmm191で実施]

mongosh

rs.initiate(
  {
    _id: "rs01",
    configsvr: true,
    members: [
      { _id : 0, host : "192.168.137.191:27017", priority : 4 },
      { _id : 1, host : "192.168.137.192:27017", priority : 2 },
      { _id : 2, host : "192.168.137.193:27017", priority : 1 }
    ]
  }
)

rs.conf()
rs.status();

-- 4. Shard#1 設定[mmm194,mmm195,mmm196で実施]

vim /etc/mongod.conf

bindIp: 0.0.0.0

replication:
  replSetName: rs02
  oplogSizeMB: 1024

sharding:
  clusterRole: shardsvr

systemctl restart mongod

-- 5. Shard#1 構築[mmm194で実施]

mongosh

rs.initiate(
  {
    _id: "rs02",
    members: [
      { _id : 0, host : "192.168.137.194:27017", priority : 4 },
      { _id : 1, host : "192.168.137.195:27017", priority : 2 },
      { _id : 2, host : "192.168.137.196:27017", priority : 1 }
    ]
  }
)

rs.conf()
rs.status();


-- 6. Shard#2 設定[mmm197,mmm198,mmm199で実施]

vim /etc/mongod.conf

bindIp: 0.0.0.0

replication:
  replSetName: rs03
  oplogSizeMB: 1024

sharding:
  clusterRole: shardsvr

systemctl restart mongod

-- 7. Shard#2 構築[mmm197で実施]

mongosh

rs.initiate(
  {
    _id: "rs03",
    members: [
      { _id : 0, host : "192.168.137.197:27017", priority : 4 },
      { _id : 1, host : "192.168.137.198:27017", priority : 2 },
      { _id : 2, host : "192.168.137.199:27017", priority : 1 }
    ]
  }
)

rs.conf()
rs.status();

-- 8. mongos 設定[mmm190で実施]

vim /etc/mongos.conf

net:
  bindIp: 127.0.0.1

sharding:
  configDB: rs01/192.168.137.191:27017,192.168.137.192:27017,192.168.137.193:27017


systemctl stop mongod
systemctl disable mongod

mongos --config /etc/mongos.conf

 

-- 9. クラスタへシャード追加[mmm190で実施]

別ターミナルから実行

mongosh


sh.status()


sh.addShard("rs02/192.168.137.194:27017,192.168.137.195:27017,192.168.137.196:27017")
sh.addShard("rs03/192.168.137.197:27017,192.168.137.198:27017,192.168.137.199:27017")

sh.status()


-- 10. 動作確認[mmm190で実施]


db.dropDatabase()
sh.shardCollection("test.tab1" , { col1 : "hashed"})
db.tab1.getIndexes()

db.tab1.insertMany([
   { col1: -10, col2: "val1" },
   { col1:   0, col2: "val2" },
   { col1:  10, col2: "val3" }
]);

sh.status();

db.tab1.find({col1: -10}).explain()
db.tab1.find({col1:  10}).explain()


mmm191,mmm194,mmm197を強制停止して再実行

killall mongod
ps -ef | grep mongod