===============================================================
Goal : 2 Shards with 3 replica Set each with security Enabled
===============================================================
-- rs_a mongod@27017 mongod@27018 mongod@27019
--> |
-- rs_b mongod@37017 mongod@37018 mongod@37019
-----------------------------------------------------
Config mongod@27020 mongod@27021 mongod@27022
---------------------------------------
-- Cleanup first if you have old stuff
---------------------------------------
stopmongo
service mongod stop
kill ???
-- Create directories
cd /apps/data
rm -rdf c1 c2 c3 b1 b2 b3 a1 a2 a3 ar1
rm -f *.log
mkdir c1 c2 c3 b1 b2 b3 a1 a2 a3 ar1
mongod --logpath /apps/data/a1_mongod.log --logappend --fork --port 27017 --dbpath /apps/data/a1 --rest --replSet rs_a --oplogSize 64
mongod --logpath /apps/data/a2_mongod.log --logappend --fork --port 27018 --dbpath /apps/data/a2 --rest --replSet rs_a --oplogSize 64
mongod --logpath /apps/data/a3_mongod.log --logappend --fork --port 27019 --dbpath /apps/data/a3 --rest --replSet rs_a --oplogSize 64
-- Login to primary and create 3 Node replica
mongo --port 27017
rs_a:PRIMARY> rs.initiate()
rs_a:PRIMARY> rs.add('node2:27018')
rs_a:PRIMARY> rs.add('node2:27019')
Note : Now We have 3 replica Set ready which will not require MONGOS or CONFIG servers because you have no shards
To convert this in a Shard Cluster ( 3 replica set and 2 Shards)
-- rs_a mongod@27017 mongod@27018 mongod@27019
--> |
-- rs_b mongod@37017 mongod@37018 mongod@37019
-----------------------------------------------------
Config mongod@27020 mongod@27021 mongod@27022
1) need to create 3 Config servers
2) Start MongoS
3) Login to MongoS and As
-- Create config
mongod --configsvr --dbpath /apps/data/c1 --port 27020 --logpath /apps/data/c1.log --logappend --fork
mongod --configsvr --dbpath /apps/data/c2 --port 27021 --logpath /apps/data/c2.log --logappend --fork
mongod --configsvr --dbpath /apps/data/c3 --port 27022 --logpath /apps/data/c3.log --logappend --fork
-- Create MongoS
mongos --logpath /apps/data/s.log --port 47017 --configdb node2:27020,node2:27021,node2:27022 --fork
mongo --port 47017
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("51dae574d9f428fe3a662adf")
}
shards:
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
-- Adding Stard
mongos> sh.addShard("rs_a/node2.carfax.cfx:27017,node2:27018,node2:27019")
{ "shardAdded" : "rs_a", "ok" : 1 }
-- Now chk the status
mongos> sh.addShard("rs_a/node2.carfax.cfx:27017,node2:27018,node2:27019")
{ "shardAdded" : "rs_a", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("51dae574d9f428fe3a662adf")
}
shards:
{ "_id" : "rs_a", "host" : "rs_a/node2.carfax.cfx:27017,node2:27018,node2:27019" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Now creating another replicase rs_b with 3 members and will add this in the existing shard
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
mongod --logpath /apps/data/b1_mongod.log --logappend --fork --port 37017 --dbpath /apps/data/b1 --rest --replSet rs_b --smallfiles --oplogSize 64
mongod --logpath /apps/data/b2_mongod.log --logappend --fork --port 37018 --dbpath /apps/data/b2 --rest --replSet rs_b --smallfiles --oplogSize 64
mongod --logpath /apps/data/b3_mongod.log --logappend --fork --port 37019 --dbpath /apps/data/b3 --rest --replSet rs_b --smallfiles --oplogSize 64
mongo --port 37017
rs.status() <-- will show Just one member in this replica set
rs.initiate()
rs_b:PRIMARY> rs.add('node2:37018')
rs_b:PRIMARY> rs.add('node2:37019')
[root@node2 mongo]# mongo --port 37017
Now you see all three members
rs_b:PRIMARY> rs.status()
Now you see all three members
rs_b:PRIMARY> rs.status()
{
"set" : "rs_b",
"date" : ISODate("2013-07-08T16:19:26Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "node2.carfax.cfx:37017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 90,
"optime" : {
"t" : 1373300338,
"i" : 1
},
"optimeDate" : ISODate("2013-07-08T16:18:58Z"),
"self" : true
},
{
"_id" : 1,
"name" : "node2:37018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 33,
"optime" : {
"t" : 1373300338,
"i" : 1
},
"optimeDate" : ISODate("2013-07-08T16:18:58Z"),
"lastHeartbeat" : ISODate("2013-07-08T16:19:25Z"),
"lastHeartbeatRecv" : ISODate("2013-07-08T16:19:26Z"),
"pingMs" : 0,
"syncingTo" : "node2.carfax.cfx:37017"
},
{
"_id" : 2,
"name" : "node2:37019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 28,
"optime" : {
"t" : 1373300338,
"i" : 1
},
"optimeDate" : ISODate("2013-07-08T16:18:58Z"),
"lastHeartbeat" : ISODate("2013-07-08T16:19:26Z"),
"lastHeartbeatRecv" : ISODate("2013-07-08T16:19:26Z"),
"pingMs" : 0,
"syncingTo" : "node2.carfax.cfx:37017"
}
],
"ok" : 1
}
+++++++++++++++++++++++++++++++++
-- Now goto MongoS and Add Shards
+++++++++++++++++++++++++++++++++
mongo --port 47017
mongos> sh.addShard("rs_b/node2.carfax.cfx:37017,node2:37018,node2:37019")
{ "shardAdded" : "rs_b", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("51dae574d9f428fe3a662adf")
}
shards:
{ "_id" : "rs_a", "host" : "rs_a/node2.carfax.cfx:27017,node2:27018,node2:27019" }
{ "_id" : "rs_b", "host" : "rs_b/node2.carfax.cfx:37017,node2:37018,node2:37019" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
Now
====
Observation
Login to MomgoS and Create a database
This will create Database only on one replica set
but once this database is enabled in the Shard
Mono will create database on all Replica Nodes
Connect to S
mongo --port 47017
> use mydb
switched to db mydb
MONGOS> for( var i=1; i <= 150000 ; i++) {db.myinfo.insert({x:i,y:"hello how are you today "})}
1) Need to enable database for sharding before you start enable collection
2) enable the Collection for Sharding
mongos> sh.enableSharding('mydb')
{ "ok" : 1 }
mongos> use mydb
mongos> db.myinfo.ensureIndex( { _id : "hashed" })
mongos> show dbs
admin (empty)
config 0.046875GB
mydb 0.203125GB
test 0.203125GB
mongos> db.myinfo.stats()
{
"sharded" : false,
"primary" : "rs_b",
"ns" : "mydb.myinfo",
"count" : 50000,
"size" : 3600016,
"avgObjSize" : 72.00032,
"storageSize" : 5591040,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 4194304,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 3245872,
"indexSizes" : {
"_id_" : 1627024,
"_id_hashed" : 1618848
},
"ok" : 1
mongos> sh.shardCollection("mydb.myinfo",{"_id":"hashed"})
{ "collectionsharded" : "mydb.myinfo", "ok" : 1 }
mongos> db.myinfo.stats()
{
"sharded" : true,
"ns" : "mydb.myinfo",
"count" : 50000,
"numExtents" : 12,
"size" : 3600008,
"storageSize" : 16773120,
"totalIndexSize" : 3842720,
"indexSizes" : {
"_id_" : 1635200,
"_id_hashed" : 2207520
},
"avgObjSize" : 72.00016,
"nindexes" : 2,
"nchunks" : 1,
"shards" : {
"rs_a" : {
"ns" : "mydb.myinfo",
"count" : 50000,
"size" : 3600008,
"avgObjSize" : 72.00016,
"storageSize" : 11182080,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 8388608,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 3826368,
"indexSizes" : {
"_id_" : 1627024,
"_id_hashed" : 2199344
},
"ok" : 1
},
"rs_b" : {
"ns" : "mydb.myinfo",
"count" : 0,
"size" : 0,
"storageSize" : 5591040,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 4194304,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 16352,
"indexSizes" : {
"_id_" : 8176,
"_id_hashed" : 8176
},
"ok" : 1
}
},
"ok" : 1
}
=================================== Security =========================================
Note1 : First Create the Users without adding any auth
Add security key 'keyfile' parameter in config
Restart Mogo with Changed keyfile
Note2 ; Very Important
You must Create Users in All replica sets ( just All Primary)
Must Create Admin and Monitor user in all THREE the Config database's Admin schema only
Note3: Must Restart All Mongod with --keyfile option
Must Restart All MongoS with --keyfile option
Must Restart All Configservers with --keyfile option
Must copy keyfile in all MONGOS, MONGOD,CONFIG servers
---------------------------------------------------
-- Creating users when mongo is non security Mode
---------------------------------------------------
-- Craete User in MONGOD
mongod> db
use admin
db.addUser( { user: "mongoadmin",pwd: "mongo_admin",roles:[ "userAdminAnyDatabase","clusterAdmin","dbAdminAnyDatabase","readWriteAnyDatabase"] } )
db.addUser( { user: "mongomonitor",pwd: "mongo_monitor",roles:[ "clusterAdmin","readAnyDatabase","dbAdminAnyDatabase"] } )
db.system.users.find()
Login to Primary Node on ALL replica Set
use mydb
db.addUser( { user: "dba",pwd: "dba",roles:[ "dbAdmin"] } )
db.addUser( { user: "userrw",pwd: "userrw",roles:[ "readWrite"] } )
db.addUser( { user: "userro",pwd: "userro",roles:[ "read"] } )
-- Now Create User in ALL THREE config ( Just admin and monitor no individual database users)
---------------------
-- Add security Key
---------------------
mkdir /data/mongokey/
change user and permissions
mkdir /data/mongokey/
chmod -R 700 /data/mongokey/
chown -R mongod:mongod /data/mongokey/
Generate key
------------
login to any one server
root> openssl rand -base64 753 > /data/mongokey/authkey
chown -R mongod:mongod /data/mongokey/
--------
-- test
--------
mongo localhost:27017/admin -u mongoadmin -p mongo_admin
MONGOS>
db = db.getSiblingDB('admin')
mongo localhost:47017/admin -u test1 -p test1
mongo localhost:47017/mydb -u dba -p mongodba
To remove a user you need to remove the corresponding document for that user in the system.users collection.
db.system.users.find()
db.system.users.remove({"user" : "mongoadmin"});
db.system.users.remove({"user" : "mongomonitor"});
db.system.users.remove({"user" : "web"});
db.system.users.remove({"user" : "web"});
db.addUser( { user: "dba", pwd: "dba", roles: [ "readWrite" ] } )
===============================================
Creating Data
===============================================
use mydb
switched to db mydb
rs_a:PRIMARY> for( var i=1; i <= 50000 ; i++) {db.myinfo.insert({x:i,y:"hello how are you today "})}
rs_a:PRIMARY> show collections
myinfo
system.indexes
=============================== Startup Scripts Without Auth =================================
echo -------------- starting mongod_rs_a
mongod --logpath /apps/data/a1_mongod.log --logappend --fork --port 27017 --dbpath /apps/data/a1 --rest --replSet rs_a --smallfiles
mongod --logpath /apps/data/a2_mongod.log --logappend --fork --port 27018 --dbpath /apps/data/a2 --rest --replSet rs_a --smallfiles
mongod --logpath /apps/data/a3_mongod.log --logappend --fork --port 27019 --dbpath /apps/data/a3 --rest --replSet rs_a --smallfiles
echo ------------ starting mongod_rs_b
mongod --logpath /apps/data/b1_mongod.log --logappend --fork --port 37017 --dbpath /apps/data/b1 --rest --replSet rs_b --smallfiles
mongod --logpath /apps/data/b2_mongod.log --logappend --fork --port 37018 --dbpath /apps/data/b2 --rest --replSet rs_b --smallfiles
mongod --logpath /apps/data/b3_mongod.log --logappend --fork --port 37019 --dbpath /apps/data/b3 --rest --replSet rs_b --smallfiles
echo -----------starting config
mongod --configsvr --logpath /apps/data/config1.log --dbpath /apps/data/c1 --port 27020 --logappend --fork
mongod --configsvr --logpath /apps/data/config2.log --dbpath /apps/data/c2 --port 27021 --logappend --fork
mongod --configsvr --logpath /apps/data/config3.log --dbpath /apps/data/c3 --port 27022 --logappend --fork
echo ------------starting mongos
mongos --port 47017 --configdb node2:27020,node2:27021,node2:27022 --logpath /apps/data/s.log --fork
=============================== Startup Scripts With Auth =================================
echo --------------starting mongod_rs_a
mongod --logpath /apps/data/a1_mongod.log --keyFile /apps/data/mongokey --logappend --fork --port 27017 --dbpath /apps/data/a1 --rest --replSet rs_a --smallfiles
mongod --logpath /apps/data/a2_mongod.log --keyFile /apps/data/mongokey --logappend --fork --port 27018 --dbpath /apps/data/a2 --rest --replSet rs_a --smallfiles
mongod --logpath /apps/data/a3_mongod.log --keyFile /apps/data/mongokey --logappend --fork --port 27019 --dbpath /apps/data/a3 --rest --replSet rs_a --smallfiles
echo --------------starting mongod_rs_b
mongod --logpath /apps/data/b1_mongod.log --keyFile /apps/data/mongokey --logappend --fork --port 37017 --dbpath /apps/data/b1 --rest --replSet rs_b --smallfiles
mongod --logpath /apps/data/b2_mongod.log --keyFile /apps/data/mongokey --logappend --fork --port 37018 --dbpath /apps/data/b2 --rest --replSet rs_b --smallfiles
mongod --logpath /apps/data/b3_mongod.log --keyFile /apps/data/mongokey --logappend --fork --port 37019 --dbpath /apps/data/b3 --rest --replSet rs_b --smallfiles
echo --------------starting config
mongod --configsvr --logpath /apps/data/config1.log --keyFile /apps/data/mongokey --dbpath /apps/data/c1 --port 27020 --logappend --fork
mongod --configsvr --logpath /apps/data/config2.log --keyFile /apps/data/mongokey --dbpath /apps/data/c2 --port 27021 --logappend --fork
mongod --configsvr --logpath /apps/data/config3.log --keyFile /apps/data/mongokey --dbpath /apps/data/c3 --port 27022 --logappend --fork
echo starting mongos
mongos --port 47017 --keyFile /apps/data/mongokey --configdb node2:27020,node2:27021,node2:27022 --logpath /apps/data/s.log --fork
========================= Mongo Agent ====================
python agent.py
nohup python agent.py > /apps/mms_software/mms-agent/agent.log 2>&1 &
tail -100f /apps/mms_software/mms-agent/agent.log