MongoDB: Replica Sets and Sharded Cluster



1. MongoDB: Replica Sets and Sharded Cluster Monday, November 5, 2018 1:30 AM - 5:00 PM - Bull

2.About me Adamo Tonete Senior Support Engineer São Paulo / Brazil @adamotonete

3.Replicaset and Shards This is a tutorial and you can follow the steps locally Suggested hardware: ● 8GB RAM Dual Core processor MongoDB 3.6.x/4.0.x Linux/MacOS or Windows

4.Preparing the Environment cd ~ mkdir percona cd percona git clone chmod +x PL18/*.sh

5.Agenda - Single instance - Replica-sets and most of its configurations - Shards and most of its configurations

6.Choosing MongoDB The idea is to demonstrate all common configurations that can be applied in an environment according to its size requirements.


8.What to consider Choosing MongoDB instead of a relational database; ● What are the advantages and disadvantages of MongoDB ● Documents style works for me? ● Dynamic schema (or schemaless) ● Speed ● Simple queries instead of complex joins

9.Starting MongoDB First contact with MongoDB Installing MongoDB ./

10.Starting MongoDB First contact with MongoDB mkdir single_instance mongod --dbpath single_instance --logpath single_instance/log.log --bind_ip --wiredTigerCacheSizeGB 0.3 --fork mongo

11.Starting MongoDB Does it mean my database is ready for production? Well, no, but we will not cover OS optimization in this talk - Check The minimum expected right now is to create an user and password to guarantee access control.

12.User Management

13.User management ● MongoDB doesn't come with an user such as root/sa in a relational database we need to enable the authentication in order to use this feature. ● Not having user was a problem in earlier versions where MongoDB listened all the network adapters by default.

14.User management ● This is what we should expect when we leave authentication disabled.

15.User management ● Creating an user and enabling authentication killall mongod mongod --dbpath single_instance --logpath single_instance/log.log --bind_ip, -- wiredTigerCacheSizeGB 0.3 --fork --auth mongo > use admin > db.createUser({user : 'admin', pwd : '123', roles : ["root"]}) Successfully added user: { "user" : "admin", "roles" : [ "root" ] }

16.User management ● Creating read only user mongo > use admin > db.createUser({user : 'intern', pwd : '123', roles : ["readAnyDatabase"]}) Successfully added user: { "user" : "'intern'", "roles" : [ "readAnyDatabase" ] }

17.User management ● Testing credentials mongo -uadmin -p --authenticationDatabase admin use test{today : new Date()}) mongo -uintern -p --authenticationDatabase admin use test{today : new Date()})

18.Default Roles read readWrite hostManager dbAdmin backup dbOwner restore userAdmin readAnyDatabase clusterAdmin readWriteAnyDatabase clusterMonitor userAdminAnyDatabase clusterManager dbAdminAnyDatabase root __system

19.Single Instance

20.Single Instance Mongo is running, time to start working in the application.

21.Single Instance issues ● If the instance goes down the application is down (single point of failure); ● Backups/Reports may affect application performance; ● No HA in place reads and writes are limited to the box limitations. ● Scaling up becomes very expensive (vertical scaling).


23.Replica-set Concept ● The replica-set is a group of mongodb instances that keeps the SAME data in it's nodes (like standby nodes in traditional RDBMS). ● Does offer high availability by default where only the primary receives writes. ● In case of a failure the remaining instances will vote in election to promote a SECONDARY to PRIMARY

24.A standard Replica-set Source:

25.Replica-set Concept ● Initialization ● Oplog ● Heartbeat ● Elections - Priorities and Votes ● Delayed Secondaries ● Hidden Secondaries ● Tags ● Removing members

26.Initializing a replica-set Let's change the single instance to a replica-set. Currently mongodb is not configured to use replica-set and we can check this in the config file or running the following command: db.adminCommand( { getCmdLineOpts: 1 } ) Use this command to check if the config file was loaded correctly

27.Config file MongoDB uses YAML file format since version 2.6 and here is how it looks like. storage: dbPath: replicaset1/instance1 journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 0.3 systemLog: destination: file logAppend: true path: replicaset1/instance1/logs.log Also refer

28.Initializing a replica-set Using the config file available in the repository and Name your replica-set with replSet variable in all nodes and start the 3 instances with: mongod -f <config cfg[1-3]>

29.Initializing The initialization process will let the database know it will be a replica-set and start recording all the operations after the rs.initiate() command was run In the 27010 run: mongo -u admin -p --authenticationDatabase admin rs.initiate()