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(Shard#1) primary
mmm193 : MongoDB(Shard#2) primary

 

-- 1. MongoDBインストール[mmm190,mmm191,mmm192,mmm193で実施]

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で実施]

vim /etc/mongod.conf

bindIp: 0.0.0.0

replication:
  replSetName: rs01

sharding:
  clusterRole: configsvr


systemctl restart mongod

mongosh

rs.initiate(
  {
    _id: "rs01",
    configsvr: true,
    members: [
      { _id : 0, host : "192.168.137.191:27017", priority : 10 }
    ]
  }
)


-- 3. Shard#1 構築[mmm192で実施]

vim /etc/mongod.conf

bindIp: 0.0.0.0

replication:
  replSetName: rs02

sharding:
  clusterRole: shardsvr

systemctl restart mongod

mongosh

rs.initiate(
  {
    _id: "rs02",
    members: [
      { _id : 0, host : "192.168.137.192:27017", priority : 10 }
    ]
  }
)

 

-- 4. Shard#2 構築[mmm193で実施]

vim /etc/mongod.conf

bindIp: 0.0.0.0

replication:
  replSetName: rs03

sharding:
  clusterRole: shardsvr

systemctl restart mongod

mongosh

rs.initiate(
  {
    _id: "rs03",
    members: [
      { _id : 0, host : "192.168.137.193:27017", priority : 10 }
    ]
  }
)


-- 5. mongos 構築[mmm190で実施]

vim /etc/mongos.conf

net:
  bindIp: 127.0.0.1

sharding:
  configDB: rs01/192.168.137.191:27017


systemctl stop mongod
systemctl disable mongod

mongos --config /etc/mongos.conf

 

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


mongosh


sh.status()


sh.addShard("rs02/192.168.137.192:27017")
sh.addShard("rs03/192.168.137.193:27017")

sh.status()


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


-- 7.1 Ranged Sharding
The default range size for a sharded cluster is 128 megabytes. 

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

 

for (var i = 0; i <= 2000*1000; i=i+20){
db.tab1.insertMany([
   { col1: i+1,  col2: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" },
   { col1: i+2,  col2: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" },
   { col1: i+3,  col2: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" },
   { col1: i+4,  col2: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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1: i+19, col2: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" },
   { col1: i+20, col2: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" },
]);
}


show dbs;
db.tab1.find().count()

db.tab1.find().sort({ col1: -1 }).limit(1)
db.tab1.find().sort({ col1:  1 }).limit(1)

sh.status();

db.tab1.find({col1: 0}).explain()
db.tab1.find({col1: 2000020}).explain()

 

-- 7.2 Hash Sharding

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()