Top Banner
MongoDB Replication and Sharding Workshop by Harun Yardımcı @h_yardimci
27

Mongodb workshop

May 10, 2015

Download

Technology

Harun Yardimci

Explains how to setup sharded cluster of replica sets. Sharding and replication deployment workshop
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mongodb workshop

MongoDB

Replication and Sharding

Workshop by Harun Yardımcı

@h_yardimci

Page 2: Mongodb workshop

What we will do today?- Following Replicated Shard

Page 3: Mongodb workshop

What is MongoDB?

● Document-oriented database

● in MongoDB you store JSON-like documents with dynamic schemas

● Bridge the gap between key-value stores and relational databases

Page 4: Mongodb workshop

What is MongoDB?

Page 5: Mongodb workshop

Why MongoDB?

● Document-oriented

● High performance

● High availability

● Easy scalability

● Rich query language + MapReduce

Page 6: Mongodb workshop

Use CasesWell Suited

● Archiving and event logging● Document and Content Management Systems● ECommerce Often in combination with an RDBMS for the final order processing and accounting

● Gaming Small read/writes are a good fit for MongoDB● Mobile Specifically, the server-side infrastructure of mobile systems. Geospatial.

● Operational data store of a web site MongoDB is very good at real-time inserts, updates, and queries. Specific web use case examples:● content management● comment storage, management, voting● user registration, profile, session data

● Real-time stats/analytics

Page 7: Mongodb workshop

What is a Document?

{'id':1, 'category_name' : 'Computer'}

Page 8: Mongodb workshop

Mongo Data Model

Key:

'category_name'

Value:

'Computer'

Field: key-value pair

'category_name':'Computer'

Document: set of fields

{'id':1, 'category_name' : 'Computer'}

Page 9: Mongodb workshop

Mongo Data Model

Collection: set of documents

(say categories)

{'id':1, 'category_name' : 'Computer'},

{'id':2, 'category_name' : 'Mobile'},

...

Database: set of collections

categories

products

members

Page 10: Mongodb workshop

Simple Usage and Introduction

Page 11: Mongodb workshop

First Run

$ mkdir -p /data/db/

$ chown -R mongod:mongod /data/db/

$ mongod [--dbpath /data/db/] &

$ mongo

> show dbs

> use admin

> show collections

Create a data path and give permissions

Start mongod deamon

Connect to mongod

Page 12: Mongodb workshop

Intro

CRUD Operations

> use testdb

switched to db testdb

> j = { name : "deneme" };

{"name" : "deneme"}

> t = { x : 3 };

{ "x" : 3 }

> db.col.save(j);

> db.col.insert(j); /* see the error message */

> db.col.save(t);

> for (var x = 1; x <= 20; x++) db.col.save({x:x, j:x*x})

>

Page 13: Mongodb workshop

Intro

> db.col.find();

{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }

{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }

{ "_id" : ObjectId("4f971ab51c3fde3bc55f286f"), "x" : 1, "j" : 1 }

{ "_id" : ObjectId("4f971ab51c3fde3bc55f2870"), "x" : 2, "j" : 4 }

{ "_id" : ObjectId("4f971ab51c3fde3bc55f2871"), "x" : 3, "j" : 9 }

has more

> db.col.remove({"x":"3"});

>

Page 14: Mongodb workshop

The Big Picture!

Page 15: Mongodb workshop

Replication

Two Types of Replications● Master / Slave Replication● ReplicaSet

Page 16: Mongodb workshop

Master-Slave Rep.

$ mkdir -p /data/db/ms

$ mkdir -p /data/db/sl

$ bin/mongod --master [--port <port>] [--dbpath /data/masterdb/]

$ bin/mongod --slave [--port <port>] --source <masterhostname>[:<port>] [--dbpath /data/slavedb/]

> db.printReplicationInfo() - on master

> db.printSlaveReplicationInfo() - on slave

> use admin

> db.runCommand({resync: 1})

Different data paths for each instances

Page 17: Mongodb workshop

ReplicaSet - Voting

Consensus Vote

For a node to be elected primary, it must receive a majority of votes by the following formula

(floor(5/2)+1)

Arbiters - {_id: 2, host: 'localhost:27019', arbiterOnly: true}

Reachable Node – heartbeat

Each replica set is limited to 12 total nodes and 7 voting nodes.

Page 18: Mongodb workshop

ReplicaSet

Starting The Nodes

$ mkdir -p /data/r0

$ mkdir -p /data/r1

$ mkdir -p /data/r2

$ mongod --replSet myRSet --port 27017 --dbpath /data/r0

$ mongod --replSet myRSet --port 27018 --dbpath /data/r1

$ mongod --replSet myRSet --port 27019 --dbpath /data/r2

myRSet

localhost27017

/data/r0

localhost27018

/data/r1

localhost27019

/data/r2

Page 19: Mongodb workshop

ReplicaSet

Initiating The Set

> config = {_id: 'myRSet', members: [

{_id: 0, host: 'localhost:27017'},

{_id: 1, host: 'localhost:27018'},

{_id: 2, host: 'localhost:27019'}]

}

> rs.initiate(config);

{

"info" : "Config now saved locally. Should come online in about a minute.",

"ok" : 1

}

> rs.status();

Page 20: Mongodb workshop

Sharding

Sharding Components

● Shard Servers

● Config Servers

● mongos Router

Page 21: Mongodb workshop

Sharding

Page 22: Mongodb workshop

Sharding

$ mkdir -p /data/db/s1 /data/db/s2 /data/db/config

$ mongod --shardsvr --port 27001 --dbpath /data/db/s1 &

$ mongod --shardsvr --port 27002 --dbpath /data/db/s2 &

$ mongod --configsvr --port 27003 --dbpath /data/db/config &

$ mongos --port 27017 --configdb localhost:27003 &

Page 23: Mongodb workshop

Sharding

Add Shards to Config

> use admin

> db.runCommand( {addShard : "localhost:27001"} );

{"ok" : 1 , "added" : "localhost:27001"}

> db.runCommand( {addShard : "localhost:27002"} );

{"ok" : 1 , "added" : "localhost:27002"}

Connect to mongos to add shards

Page 24: Mongodb workshop

Sharding

Enable Sharding

> db.runCommand( { enablesharding: "test_database"} );

{ "ok" : 1 }

> db.runCommand( { shardcollection : "test_database.myCollection", key : {"_id" :1} })

{ "collectionsharded" : "test_database.myCollection", "ok" : 1 }

Page 25: Mongodb workshop

ReplicaSet + Sharding

host1$ mongod --shardsvr --replSet rs_a

host2$ mongod --shardsvr --replSet rs_a

host3$ mongod --shardsvr --replSet rs_a

> cfg = {

_id : "rs_a",

members : [

{_id : 0, host : "host1:27018", priority : 1},

{_id : 1, host : "host2:27018", priority : 1},

{_id : 2, host : "host3:27018", priority : 0}

]

}

> rs.initiate(cfg)

Same replica set name

Page 26: Mongodb workshop

ReplicaSet + Sharding

host1$ mongod --configsvr

host2$ mongod --configsvr

host3$ mongod --configsvr

$ mongos --configdb host1:27019,host2:27019,host3:27019

$ mongo

> db.adminCommand( { addShard : "rs_a/host1:27018,host2:27018,host3:27018" } )

> db.adminCommand( { addShard : "rs_b/host4:27018,host5:27018,host6:27018" } )

> db.adminCommand( { addShard : "rs_c/host7:27018,host8:27018,host9:27018" } )

Page 27: Mongodb workshop

What is Next?

Please

Try it Yourself

and

Share Your Experiences

Thanks