Open4

mongo db

ピン留めされたアイテム
aGlnYWtpbg==aGlnYWtpbg==

接続

docker run -it --entrypoint bash mongoclient/mongoclient


db 変更

use <db名>

db一覧

show dbs;

collection一覧

show collections;

create(insert)


read(find)

db.['collection名'].find({"検索条件"},{"取得フィールド"});

update (update, upsert)

delete(remove)

aGlnYWtpbg==aGlnYWtpbg==

mongo sharding

コンテナ環境

version: '2'

services:
  mongodb-sharded:
    image: docker.io/bitnami/mongodb-sharded:4.4
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-sharded
      - MONGODB_SHARDING_MODE=mongos
      - MONGODB_CFG_PRIMARY_HOST=mongodb-cfg
      - MONGODB_CFG_REPLICA_SET_NAME=cfgreplicaset
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_ROOT_PASSWORD=password123
    ports:
      - "27017:27017"

  mongodb-shard0:
    image: docker.io/bitnami/mongodb-sharded:4.4
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-shard0
      - MONGODB_SHARDING_MODE=shardsvr
      - MONGODB_MONGOS_HOST=mongodb-sharded
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_REPLICA_SET_NAME=shard0
    volumes:
      - 'shard0_data:/bitnami'

  mongodb-cfg:
    image: docker.io/bitnami/mongodb-sharded:4.4
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-cfg
      - MONGODB_SHARDING_MODE=configsvr
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_REPLICA_SET_NAME=cfgreplicaset
    volumes:
      - 'cfg_data:/bitnami'
volumes:
  shard0_data:
    driver: local
  cfg_data:
    driver: local

sharingを確認

docker-compose exec mongodb-cfg bash
mongo -u root -ppassword123
sh.status()


--- Sharding Status ---
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("61eea9c525597c5a2a097f79")
  }
  shards:
        {  "_id" : "shard0",  "host" : "shard0/mongodb-shard0:27017",  "state" : 1 }
  active mongoses:
        "4.4.12" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  unknown
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard0	1024
                        too many chunks to print, use verbose if you want to force print
        {  "_id" : "mydatabase",  "primary" : "shard0",  "partitioned" : true,  "version" : {  "uuid" : UUID("d9c98c3f-69e0-4e3f-8a49-82c3397f9185"),  "lastMod" : 1 } }
aGlnYWtpbg==aGlnYWtpbg==

shardを追加してみる

version: '2'

services:
  mongodb-sharded:
    image: docker.io/bitnami/mongodb-sharded:4.4
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-sharded
      - MONGODB_SHARDING_MODE=mongos
      - MONGODB_CFG_PRIMARY_HOST=mongodb-cfg
      - MONGODB_CFG_REPLICA_SET_NAME=cfgreplicaset
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_ROOT_PASSWORD=password123
    ports:
      - "27017:27017"

  mongodb-shard0:
    image: docker.io/bitnami/mongodb-sharded:4.4
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-shard0
      - MONGODB_SHARDING_MODE=shardsvr
      - MONGODB_MONGOS_HOST=mongodb-sharded
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_REPLICA_SET_NAME=shard0
    volumes:
      - 'shard0_data:/bitnami'
  mongodb-shard1:
    image: docker.io/bitnami/mongodb-sharded:4.4
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-shard1
      - MONGODB_SHARDING_MODE=shardsvr
      - MONGODB_MONGOS_HOST=mongodb-sharded
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_REPLICA_SET_NAME=shard1
    volumes:
      - 'shard1_data:/bitnami'
  mongodb-cfg:
    image: docker.io/bitnami/mongodb-sharded:4.4
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-cfg
      - MONGODB_SHARDING_MODE=configsvr
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_REPLICA_SET_NAME=cfgreplicaset
    volumes:
      - 'cfg_data:/bitnami'
  mongodb-client:
    build: .
    tty: true
    volumes:
      - ./src/:/mnt
volumes:
  shard0_data:
    driver: local
  shard1_data:
    driver: local
  cfg_data:
    driver: local

mongo sharding設定確認

cfgreplicaset:PRIMARY> sh.status()
--- Sharding Status ---
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("61eea9c525597c5a2a097f79")
  }
  shards:
        {  "_id" : "shard0",  "host" : "shard0/mongodb-shard0:27017",  "state" : 1 }
        {  "_id" : "shard1",  "host" : "shard1/mongodb-shard1:27017",  "state" : 1 }
  active mongoses:
        "4.4.12" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  unknown
        Collections with active migrations:
                config.system.sessions started at Tue Jan 25 2022 13:18:28 GMT+0000 (UTC)
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard0	1024
                        too many chunks to print, use verbose if you want to force print
        {  "_id" : "mydatabase",  "primary" : "shard0",  "partitioned" : true,  "version" : {  "uuid" : UUID("d9c98c3f-69e0-4e3f-8a49-82c3397f9185"),  "lastMod" : 1 } }
aGlnYWtpbg==aGlnYWtpbg==

そもそもshardingとは

シャーディングとは、データを複数のサーバーに分散させるための仕組み

mongo shadingの構成について

複数サーバーの同じデータをコピ0

  • mongos(ルータ)
    • アプリケーションからシャードクラスタへのインターフェースを提供する
    • シャードクラスタにアクセスする場合、必ずmongosを経由してアクセスする
  • shard(シャード)
    • collectionを分割したデータを保存する(チャンク)
    • レプリカセットの構成
  • config
    • shardに関するメタデータを持つサーバー
    • どのshardにデータがあるのかという情報もこのconfigサーバーが保持している

シャードキー

シャードキーにデータを登録するときに利用するための情報
mongoはcollectionのdocumentをシャードキーを利用して分割する。