Skip to content

Instantly share code, notes, and snippets.

@axel-sirota
Last active March 17, 2021 19:53
Show Gist options
  • Save axel-sirota/fe8b7dd0a1e0b0b21a1d37a06908e180 to your computer and use it in GitHub Desktop.
Save axel-sirota/fe8b7dd0a1e0b0b21a1d37a06908e180 to your computer and use it in GitHub Desktop.
MongoDB: Next Steps Live Demos
version: "2"
services:
mongo-shard1:
image: mongo
command: mongod --shardsvr --replSet mongors1 --dbpath /data/db --port 27017
mongo-shard2:
image: mongo
command: mongod --shardsvr --replSet mongors2 --dbpath /data/db --port 27017
mongo-cfg1:
image: mongo
container_name: mongocfg1
command: mongod --configsvr --replSet mongoconf --dbpath /data/db --port 27017
mongo-cfg2:
image: mongo
container_name: mongocfg2
command: mongod --configsvr --replSet mongoconf --dbpath /data/db --port 27017
mongos:
container_name: mongos
image: mongo
depends_on:
- mongo-cfg1
- mongo-cfg2
command: mongos --configdb mongoconf/mongocfg1:27017,mongocfg2:27017 --port 27017
ports:
- 27019:27017
expose:
- 27017
Live Demo contents and commands for MongoDB: Next Steps
// Attach container
docker exec -it mongo /bin/bash
// Create data directories since the main mongod is using /data/db
mkdir -p /tmp/db{1,2,3}
// Run mongod processes for RS
mongod --replSet nextSteps --port 27000 --dbpath /tmp/db1 --oplogSize 200
mongod --replSet nextSteps --port 27001 --dbpath /tmp/db2 --oplogSize 200
mongod --replSet nextSteps --port 27002 --dbpath /tmp/db3 --oplogSize 200
// Connect to a mongod process via a shell
mongo --port 27000
// Inside the shell
rsconf = {
_id: "nextSteps",
members: [
{_id: 0, host: "localhost:27000"},
{_id: 1, host: "localhost:27001"},
{_id: 2, host: "localhost:27002"}
]
}
rs.initiate(rsconf)
db.isMaster()
rs.status()
use oreilly
for (i=0; i<200; i++) {db.testing.insert({_id: i})}
db.testing.findOne()
// Go to a secondary, let's say (is random sop choose your own) 27002
secondaryConn = new Mongo("localhost:27002")
secondaryDB = secondaryConn.getDB("oreilly")
secondaryDB.testing.find() //errors
secondaryConn.setSecondaryOk()
secondaryDB.testing.find() // success
secondaryDB.testing.insert({"_id" : 201}) // not master!
db.adminCommand({"shutdown" : 1})
secondaryDB.isMaster() // primary changed!!
// Start config server
docker-compose up
docker exec -it mongo-cfg1 /bin/bash
mongo
// Inside the shell
rsconf = {
_id: "mongoconf",
members: [
{_id: 0, host: "mongocfg1:27017"},
{_id: 1, host: "mongocfg2:27017"},
]
}
rs.initiate(rsconf)
rs.status()
mongo --host mongo-shard1
rsconf = {
_id: "mongors1",
members: [
{_id: 0, host: "mongo-shard1:27017"},
]
}
rs.initiate(rsconf)
rs.status()
mongo --host mongo-shard2
rsconf = {
_id: "mongors2",
members: [
{_id: 0, host: "mongo-shard2:27017"},
]
}
rs.initiate(rsconf)
rs.status()
docker exec -it mongos /bin/bash
mongos
use admin
sh.addShard("mongors1/mongo-shard1:27017" )
sh.addShard("mongors2/mongo-shard2:27017" )
sh.status()
sh.enableSharding("oreilly")
db.test.createIndex({ "age" : "hashed" })
sh.shardCollection("oreilly.test", {"age":"hashed"}, false, { numInitialChunks: 10 })
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
for (i=0; i<1000; i++) {db.test.insert({age: getRandomInt(0,95), grade: getRandomInt(1,10)})}
sh.status()
db.test.explain().find()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment