Server Admin

Import mongo config files

mongod --config=mongod.conf (replace with file path)

Replica Sets

  • we can have multiple replica sets, but one will be primary one and others are secondary.
  • primary copies all the data to secondary nodes
  • if primary goes away, one of the secondary becomes primary one
  • create replica with name cookingSet (this should be same for all replica sets)
  • dbpath defines where to store the db data
  • you can create multiple instances run simultaneously by changing port and dbpath
mongod --replSet cookingSet --dbpath=/store/data/rs1 --port 27017 --smallfiles --oplogSize 200
  • after we have multiple instances running, get inside by mongo command in one of them
config={
    _id: "cookingSet",
    members: [
        { _id: 0, host: "localhost:27017" },
        { _id: 0, host: "localhost:27018" },
        { _id: 0, host: "localhost:27019" }
    ]
};

start replica set

rs.initiate(config);

large summary of replica set

rs.status();

small summary

db.isMaster()

Sharding

  • it is breaking up the data into multiple servers
  • mongos process is used
  • Sharding key is used and mongos keeps track of what data is where like a table
  • mongos fetches the right data from right server and merge it together

Authentication and Authorization

  • First create a db named admin by inserting any record
  • Create a user
use admin
db.createUser(
    {
        user: "taco",
        pwd: passwordPrompt(),
        roles: [
            { role: "userAdminAnyDatabase", db: "admin" },
            "readWriteAnyDatabase"
        ]
    }
);
  • Now shutdown the database
db.adminCommand({ shutdown: 1 });
  • Then open up mongo.conf file and enable security.authorization field
  • Then you can login as follows:
    • First way
    mongo --authenticationDatabase "admin" -u "taco" -p
    • Second way
    use admin;
    db.auth("taco", passwordPrompt());

Backup

  • To backup prevent write first then backup then resume write on the db

preventing write

db.fsyncLock();

do the regular copy of the actual storage location in filesystem

cp -R /data/db/* /backup/location

resume write

db.fsyncUnlock();

Backup and Restore

  • To restore you need to stop mongo server the replace the folder/files and start it again
  • backing up secondary replica set is a good idea
  • You can also use mongodump which will create dump/ folder

Backup

mongodump

Restore

mongorestore /path/to/dump