SlideShare a Scribd company logo
1 of 27
Download to read offline
MongoDB
Replication and Sharding

      Workshop
         by Harun Yardımcı

           @h_yardimci
What we will do today?
 - Following Replicated Shard
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
What is MongoDB?
Why MongoDB?

●   Document-oriented

●   High performance

●   High availability

●   Easy scalability

●   Rich query language + MapReduce
Use Cases
                                                   Well 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
What is a Document?


{'id':1, 'category_name' : 'Computer'}
Mongo Data Model

Key:
  'category_name'

Value:
  'Computer'

Field: key-value pair
  'category_name':'Computer'

Document: set of fields
  {'id':1, 'category_name' : 'Computer'}
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
Simple Usage and Introduction
First Run

$ mkdir -p /data/db/                  Create a data path
                                     and give permissions
$ chown -R mongod:mongod /data/db/

                                     Start mongod deamon
$ mongod [--dbpath /data/db/] &
$ mongo                               Connect to mongod



> show dbs
> use admin
> show collections
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})
>
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"});
>
The Big Picture!
Replication

Two Types of Replications
●   Master / Slave Replication
●   ReplicaSet
Master-Slave Rep.

$ mkdir -p /data/db/ms             Different data paths
$ mkdir -p /data/db/sl              for each instances


$ 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})
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.
ReplicaSet

Starting The Nodes                         myRSet

                               localhost   localhost   localhost
                                 27017       27018       27019
$ mkdir -p /data/r0             /data/r0    /data/r1    /data/r2

$ 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
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();
Sharding

Sharding Components

  ●   Shard Servers

  ●   Config Servers

  ●   mongos Router
Sharding
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 &
Sharding

Add Shards to Config
                                  Connect to mongos to add shards


> use admin
> db.runCommand( {addShard : "localhost:27001"} );
{"ok" : 1 , "added" : "localhost:27001"}
> db.runCommand( {addShard : "localhost:27002"} );
{"ok" : 1 , "added" : "localhost:27002"}
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 }
ReplicaSet + Sharding

host1$ mongod --shardsvr --replSet rs_a
host2$ mongod --shardsvr --replSet rs_a          Same replica set name
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)
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" } )
What is Next?

        Please
    Try it Yourself
          and
Share Your Experiences

       Thanks

More Related Content

What's hot

From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
Night Sailer
 
MongoDB: Replication,Sharding,MapReduce
MongoDB: Replication,Sharding,MapReduceMongoDB: Replication,Sharding,MapReduce
MongoDB: Replication,Sharding,MapReduce
Takahiro Inoue
 
MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤
Takahiro Inoue
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2
Takahiro Inoue
 

What's hot (20)

MongoDB Database Replication
MongoDB Database ReplicationMongoDB Database Replication
MongoDB Database Replication
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
 
Webinar: Replication and Replica Sets
Webinar: Replication and Replica SetsWebinar: Replication and Replica Sets
Webinar: Replication and Replica Sets
 
Replication and Replica Sets
Replication and Replica SetsReplication and Replica Sets
Replication and Replica Sets
 
Python Development (MongoSF)
Python Development (MongoSF)Python Development (MongoSF)
Python Development (MongoSF)
 
MongoDB: Replication,Sharding,MapReduce
MongoDB: Replication,Sharding,MapReduceMongoDB: Replication,Sharding,MapReduce
MongoDB: Replication,Sharding,MapReduce
 
MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤
 
Redis for the Everyday Developer
Redis for the Everyday DeveloperRedis for the Everyday Developer
Redis for the Everyday Developer
 
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp KrennJavantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
 
Latinoware
LatinowareLatinoware
Latinoware
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2
 
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB PerformanceMongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB Performance
 
How to use MongoDB with CakePHP
How to use MongoDB with CakePHPHow to use MongoDB with CakePHP
How to use MongoDB with CakePHP
 
Advanced Redis data structures
Advanced Redis data structuresAdvanced Redis data structures
Advanced Redis data structures
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
 
MongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right Way
MongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right WayMongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right Way
MongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right Way
 
Elastic search 검색
Elastic search 검색Elastic search 검색
Elastic search 검색
 
MySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsMySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of Things
 

Viewers also liked

Sharding with MongoDB (Eliot Horowitz)
Sharding with MongoDB (Eliot Horowitz)Sharding with MongoDB (Eliot Horowitz)
Sharding with MongoDB (Eliot Horowitz)
MongoSF
 
React Nativeはクロスプラットフォームモバイルアプリ開発の夢を見るか #DroidKaigi
React Nativeはクロスプラットフォームモバイルアプリ開発の夢を見るか #DroidKaigiReact Nativeはクロスプラットフォームモバイルアプリ開発の夢を見るか #DroidKaigi
React Nativeはクロスプラットフォームモバイルアプリ開発の夢を見るか #DroidKaigi
Yukiya Nakagawa
 

Viewers also liked (8)

MongoDB San Francisco 2013: Basic Sharding in MongoDB presented by Brandon Bl...
MongoDB San Francisco 2013: Basic Sharding in MongoDB presented by Brandon Bl...MongoDB San Francisco 2013: Basic Sharding in MongoDB presented by Brandon Bl...
MongoDB San Francisco 2013: Basic Sharding in MongoDB presented by Brandon Bl...
 
MongoDB Shard Cluster
MongoDB Shard ClusterMongoDB Shard Cluster
MongoDB Shard Cluster
 
รู้สิ่งใดไม่สู้...รู้งี้....
รู้สิ่งใดไม่สู้...รู้งี้....รู้สิ่งใดไม่สู้...รู้งี้....
รู้สิ่งใดไม่สู้...รู้งี้....
 
Sharding with MongoDB (Eliot Horowitz)
Sharding with MongoDB (Eliot Horowitz)Sharding with MongoDB (Eliot Horowitz)
Sharding with MongoDB (Eliot Horowitz)
 
Building Universal Web Apps with React ForwardJS 2017
Building Universal Web Apps with React ForwardJS 2017Building Universal Web Apps with React ForwardJS 2017
Building Universal Web Apps with React ForwardJS 2017
 
Sharding Methods for MongoDB
Sharding Methods for MongoDBSharding Methods for MongoDB
Sharding Methods for MongoDB
 
React JS and why it's awesome
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesome
 
React Nativeはクロスプラットフォームモバイルアプリ開発の夢を見るか #DroidKaigi
React Nativeはクロスプラットフォームモバイルアプリ開発の夢を見るか #DroidKaigiReact Nativeはクロスプラットフォームモバイルアプリ開発の夢を見るか #DroidKaigi
React Nativeはクロスプラットフォームモバイルアプリ開発の夢を見るか #DroidKaigi
 

Similar to Mongodb workshop

MongoDB for Time Series Data Part 3: Sharding
MongoDB for Time Series Data Part 3: ShardingMongoDB for Time Series Data Part 3: Sharding
MongoDB for Time Series Data Part 3: Sharding
MongoDB
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
Yiwei Ma
 

Similar to Mongodb workshop (20)

Get expertise with mongo db
Get expertise with mongo dbGet expertise with mongo db
Get expertise with mongo db
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
 
MongoDB and DynamoDB
MongoDB and DynamoDBMongoDB and DynamoDB
MongoDB and DynamoDB
 
Mongo db roma replication and sharding
Mongo db roma replication and shardingMongo db roma replication and sharding
Mongo db roma replication and sharding
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
 
NoSQL Infrastructure
NoSQL InfrastructureNoSQL Infrastructure
NoSQL Infrastructure
 
MongoDB and Node.js
MongoDB and Node.jsMongoDB and Node.js
MongoDB and Node.js
 
MongoDB for Time Series Data Part 3: Sharding
MongoDB for Time Series Data Part 3: ShardingMongoDB for Time Series Data Part 3: Sharding
MongoDB for Time Series Data Part 3: Sharding
 
Practical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails AppPractical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails App
 
Introduction to cloudforecast
Introduction to cloudforecastIntroduction to cloudforecast
Introduction to cloudforecast
 
NoSQL Infrastructure - Late 2013
NoSQL Infrastructure - Late 2013NoSQL Infrastructure - Late 2013
NoSQL Infrastructure - Late 2013
 
Rails with mongodb
Rails with mongodbRails with mongodb
Rails with mongodb
 
R-House (LSRC)
R-House (LSRC)R-House (LSRC)
R-House (LSRC)
 
Puppet at Opera Sofware - PuppetCamp Oslo 2013
Puppet at Opera Sofware - PuppetCamp Oslo 2013Puppet at Opera Sofware - PuppetCamp Oslo 2013
Puppet at Opera Sofware - PuppetCamp Oslo 2013
 
MongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & AnalyticsMongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & Analytics
 
OSDC 2012 | Scaling with MongoDB by Ross Lawley
OSDC 2012 | Scaling with MongoDB by Ross LawleyOSDC 2012 | Scaling with MongoDB by Ross Lawley
OSDC 2012 | Scaling with MongoDB by Ross Lawley
 
Introduction to MongoDB with PHP
Introduction to MongoDB with PHPIntroduction to MongoDB with PHP
Introduction to MongoDB with PHP
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
 

More from Harun Yardımcı

More from Harun Yardımcı (10)

Webrazzi Online: Code - GittiGidiyor’a Teknik Bakış 2016
Webrazzi Online: Code - GittiGidiyor’a Teknik Bakış 2016Webrazzi Online: Code - GittiGidiyor’a Teknik Bakış 2016
Webrazzi Online: Code - GittiGidiyor’a Teknik Bakış 2016
 
Introduction to Docker
Introduction to DockerIntroduction to Docker
Introduction to Docker
 
"It Works On My Machine" Problem
"It Works On My Machine" Problem"It Works On My Machine" Problem
"It Works On My Machine" Problem
 
What you don't learn in the school
What you don't learn in the schoolWhat you don't learn in the school
What you don't learn in the school
 
CFEX 2014 - DAU
CFEX 2014 - DAUCFEX 2014 - DAU
CFEX 2014 - DAU
 
Scalability at Gittigidiyor
Scalability at GittigidiyorScalability at Gittigidiyor
Scalability at Gittigidiyor
 
Software Development Whats & Whys
Software Development Whats & Whys Software Development Whats & Whys
Software Development Whats & Whys
 
Introduction to Mongodb
Introduction to MongodbIntroduction to Mongodb
Introduction to Mongodb
 
Gittigidiyor.com'da Acik Kaynak Uygulamalar
Gittigidiyor.com'da Acik Kaynak UygulamalarGittigidiyor.com'da Acik Kaynak Uygulamalar
Gittigidiyor.com'da Acik Kaynak Uygulamalar
 
Git Branching Model
Git Branching ModelGit Branching Model
Git Branching Model
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

Mongodb workshop

  • 1. MongoDB Replication and Sharding Workshop by Harun Yardımcı @h_yardimci
  • 2. What we will do today? - Following Replicated Shard
  • 3. 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
  • 5. Why MongoDB? ● Document-oriented ● High performance ● High availability ● Easy scalability ● Rich query language + MapReduce
  • 6. Use Cases Well 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
  • 7. What is a Document? {'id':1, 'category_name' : 'Computer'}
  • 8. Mongo Data Model Key: 'category_name' Value: 'Computer' Field: key-value pair 'category_name':'Computer' Document: set of fields {'id':1, 'category_name' : 'Computer'}
  • 9. 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
  • 10. Simple Usage and Introduction
  • 11. First Run $ mkdir -p /data/db/ Create a data path and give permissions $ chown -R mongod:mongod /data/db/ Start mongod deamon $ mongod [--dbpath /data/db/] & $ mongo Connect to mongod > show dbs > use admin > show collections
  • 12. 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}) >
  • 13. 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"}); >
  • 15. Replication Two Types of Replications ● Master / Slave Replication ● ReplicaSet
  • 16. Master-Slave Rep. $ mkdir -p /data/db/ms Different data paths $ mkdir -p /data/db/sl for each instances $ 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})
  • 17. 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.
  • 18. ReplicaSet Starting The Nodes myRSet localhost localhost localhost 27017 27018 27019 $ mkdir -p /data/r0 /data/r0 /data/r1 /data/r2 $ 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
  • 19. 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();
  • 20. Sharding Sharding Components ● Shard Servers ● Config Servers ● mongos Router
  • 22. 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 &
  • 23. Sharding Add Shards to Config Connect to mongos to add shards > use admin > db.runCommand( {addShard : "localhost:27001"} ); {"ok" : 1 , "added" : "localhost:27001"} > db.runCommand( {addShard : "localhost:27002"} ); {"ok" : 1 , "added" : "localhost:27002"}
  • 24. 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 }
  • 25. ReplicaSet + Sharding host1$ mongod --shardsvr --replSet rs_a host2$ mongod --shardsvr --replSet rs_a Same replica set name 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)
  • 26. 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" } )
  • 27. What is Next? Please Try it Yourself and Share Your Experiences Thanks