MongoDB by Tonny
-
Upload
agate-studio -
Category
Education
-
view
1.130 -
download
1
description
Transcript of MongoDB by Tonny
@agatestudio
Replication and Sharding on MongoDB
Tonny KPublishing
Agate Studio
@agatestudio
CONTENT
Introduction
Replication
• Replication: Concepts
• Replication: How to
Sharding
• Sharding: Concepts
• Sharding: How to
@agatestudio
Introduction
• Replication increases data availability
With multiple copies of data on different database servers, replication protects a database from the loss of a single server
• Sharding addresses the challenge of scaling to support large data sets
As the size of the data increases, a single machine may not be sufficient to store the data. With Sharding we can distribute data across multiple servers
@agatestudio
Replication: Concepts
• Replication is the process of synchronizing data across multiple servers
• You can use replication to increase read capacity
• Provide high availability using automatic failover
Replication: Concepts• Replica set Members
– Primary: accepts all write operations from clients
– Secondary: maintains a copy of the primary’s data set.
Replication: Concepts• Automatic Failover
The loss of a primary triggers an election where one of the secondaries becomes the new primary
@agatestudio
Replication: How to1. Install MongoDB
– # yum install mongo-10gen mongo-10gen-server
2. Configure – port = 27017 – bind_ip = 192.168.56.107– dbpath = /srv/mongodb/ – fork = true – replSet = rs1
3. Start Instances– # mongod --config /etc/mongodb.conf
4. Connect To Instance– $ mongo <host>
5. Initiate and Add Replica set member– rs.initiate()– rs.add("<hostname>:<port>") – rs.conf()
@agatestudio
Sharding: Concepts
• Sharding is the process of storing data records across multiple machines
• Sharding reduces the amount of data that each server needs to store
• MongoDB distributes data, or shards, at the collection level
• Sharding partitions a collection’s data by the shard key.
@agatestudio
Sharding: Concepts
• Shard Key
– A shard key is either an indexed field or an indexed compound field that exists in every document in the collection
– MongoDB divides the shard key values into chunks and distributes the chunks evenly across the shards
Sharding: Concepts
• Data Partitioning– Range Based Sharding
– Hash Based Sharding
Sharding: Concepts
• Components
– Router
routes the reads and writes from applications to the shards
– Config Server
holds metadata about the cluster
– Shard
holds a subset of a collection’s data
Sharding: Concepts
• Maintaining a Balanced Data Distribution
Ch
un
k Migratio
n
Chunk Splitting
@agatestudio
Sharding: How to
1. Create Data directory for each Config Server & start instances– mkdir /data/configdb
– mongod --configsvr --dbpath <path> --port <port>
2. Start Router (mongos instance)– mongos --configdb <config server hostnames>
3. Connect To Mongos Instance
– mongo --host <hostname> --port <port>
4. Add each shard to the cluster– sh.addShard( "mongodb0.example.net:27017" )
5. Enable Sharding for A Database– sh.enableSharding("<database>")
6. Enable Sharding for Collections– sh.shardCollection("<database>.<collection>", shard-key-pattern)
THANK YOU