MongoDB: Replica Sets and Sharded Cluster

在本教程中,我们将从头开始构建一个碎片,从单个实例开始,然后详细学习副本集的工作方式。您将了解如何将单个实例转换为副本集,以及为什么单个实例不适合生产。在理解了副本集概念之后,我们将使用最佳实践启动一个分片群集,了解分片如何在内部工作,分块键和分片键是什么,如何跨不同区域分割数据(地理分区),如何执行升级,以及如何向上和向下扩展CLU。特尔。所有这些步骤都将在安全的环境中执行,并由PMM启用身份验证和监控。

展开查看详情

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 https://github.com/adamobr/PL18.git 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.

7.Beginning

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 ./prepare_environment.sh

10.Starting MongoDB First contact with MongoDB mkdir single_instance mongod --dbpath single_instance --logpath single_instance/log.log --bind_ip 127.0.0.1 --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 https://www.percona.com/blog/2016/08/12/tuning-linux-for-mongodb/ 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 127.0.0.1,192.168.88.110 -- 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 db.foo.insert({today : new Date()}) mongo -uintern -p --authenticationDatabase admin use test db.foo.insert({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).

22.Replicasets

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: https://docs.mongodb.com

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 http://yaml.org/start.html

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