~~~~~~~~~~~~~~~~~~~~~ ~~ CONGFIG servers ~~ ~~~~~~~~~~~~~~~~~~~~~ -- IMPORTANT: note that the SECURITY are initially disabled ~~ example mongod.conf for CONFIG server -----------------------------------------------> storage: dbPath: "/data/mongodb/" journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 1 net: port: 27017 bindIp: 127.0.0.1,ivera-conf01,ivera-conf01-priv #security: #authorization: 'enabled' #keyFile: /app/mongodb/conf/keyfile.basic replication: replSetName: ivera_conf oplogSizeMB: 100 enableMajorityReadConcern: true sharding: clusterRole: configsvr <----------------------------------------------- -- replication setup cfg = { _id : "ivera_conf", members : [ { "_id" : 0, "host":"ivera-conf01-priv:27017"},], configsvr: true, } rs.initiate(cfg) rs.add('ivera-conf02-priv:27017'); rs.conf(); rs.status(); -- security setup on PRIMARY use admin db.createUser({ user: "superhero", pwd: "secret", roles: ["root"]}); -- uncomment SECURITY lines from config file on PRIMARY/SECONDARY and restart MongoDB instances ~~~~~~~~~~~~~~~~~~ ~~ DATA servers ~~ ~~~~~~~~~~~~~~~~~~ -- on DATA servers, the security can be implemented before or after replication setup ~~ example mongod.conf for DATA server -----------------------------------------------> storage: dbPath: "/data/mongodb/" journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 1 net: port: 27017 bindIp: 127.0.0.1,ivera-mongo01,ivera-mongo01-priv security: authorization: 'enabled' keyFile: /app/mongodb/conf/keyfile.basic replication: replSetName: ivera_data_01_02 oplogSizeMB: 100 enableMajorityReadConcern: true sharding: clusterRole: shardsvr <----------------------------------------------- -- replication setup cfg = { _id : "ivera_conf", members : [ { "_id" : 0, "host":"ivera-conf01-priv:27017"},], configsvr: true, } rs.initiate(cfg) rs.add('ivera-conf02-priv:27017'); rs.conf(); rs.status(); ~~~~~~~~~~~~~~~~~~~~ ~~ ROUTER servers ~~ ~~~~~~~~~~~~~~~~~~~~ ~~ example mongos.conf -----------------------------------------------> net: port: 27017 bindIp: 127.0.0.1,ivera-router01,ivera-router01-priv sharding: configDB: "ivera_conf/ivera-conf01:27017,ivera-conf01:27017" security: keyFile: /app/mongodb/conf/keyfile.basic <----------------------------------------------- -- create SYSTEMD service for MongoDB Router -- create service unit file /etc/systemd/system/mongos.service -----------------------------------------------> [Unit] Description=MongoDB Router After=multi-user.target [Service] Type=simple # (file size) LimitFSIZE=infinity # (cpu time) LimitCPU=infinity # (virtual memory size) LimitAS=infinity # (locked-in-memory size) LimitMEMLOCK=infinity # (open files) LimitNOFILE=64000 # (processes/threads) LimitNPROC=64000 User=mongod Group=mongod ExecStart=/app/mongodb/product/server/current_version/bin/mongos --config=/app/mongodb/conf/mongos.conf --logpath=/app/mongodb/log/mongos.log [Install] WantedBy=multi-user.target <----------------------------------------------- systemctl daemon-reload systemctl start mongos systemctl status mongos systemctl enable mongos -- connect to MongoDB Router un authentified mode and add shards mongo --username "superhero" --password "******" sh.addShard( "ivera_data_01_02/ivera-mongo01-priv:27017") sh.addShard( "ivera_data_01_02/ivera-mongo02-priv:27017") sh.addShard( "ivera_data_03_04/ivera-mongo03-priv:27017") sh.addShard( "ivera_data_03_04/ivera-mongo04-priv:27017") sh.addShard( "ivera_data_05_06/ivera-mongo05-priv:27017") sh.addShard( "ivera_data_05_06/ivera-mongo06-priv:27017") -- NOTE: a MongoDB router don't have any data locally -- except the mongos.conf file -- We can create multiple MongoDB routers and use a load balancer to redirect user's calls ~~~~~~~~~~~~~~~~~~ ~~ Test Cluster ~~ ~~~~~~~~~~~~~~~~~~ -- create a database and activate sharding at the database level use exampleDB sh.enableSharding("exampleDB") -- check database sharding use config db.databases.find() -- create a collection and hash value of _id db.exampleCollection.ensureIndex( { _id : "hashed" } ) -- shard the collection sh.shardCollection( "exampleDB.exampleCollection", { "_id" : "hashed" } ) -- insert documents for (var i = 1; i <= 500; i++) db.exampleCollection.insert( { x : i } ) -- display collection documents distributions across shards db.exampleCollection.getShardDistribution()