MongoDB Basics

Installing MongoDB

Community edition Setup





Replicas

  • Docs
  • Config
  • FAQ
  • Backup/ fault tolerancy //must
  • improved read performance (like load balancer) //little sync latency issue

Sharding

  • Docs
  • To use more computing power, spreading data across nodes Note : queries run across all shards / use shardKey to target specific

Other links


Commands

  • Windows to stop service through command (or do it from services.msc UI)

    net stop MongoDB
  • MongoD Docs

  • Start manually MongoDB server with default DB path (/data/db at root level)

    mongod

    Note : default database folder path must exist. In windows - with in installed folder say 'E:\mongodb_storage\Server\4.0\data\db'

    Check all arguments options like --logpath, --dbpath, --config ..etc

    mongod --help

    Note : Use --directoryperdb setting else all collection files are stored in root folder. (Each database will be stored in a separate directory)

  • To Start MongoDB server with custom DB path

    sudo mongod --dbpath "/custom/linux/Path" --port 27018
    mongod --dbpath "drive:\custom\win\Path" --port 27018

    Note: sudo : it asks for password if permission doesn't exist //so command doesnot fail

  • Mongo Shell Docs if port is changed then open shell with port //mongo --port 27018 Can open shell with help //mongo --help //--host, --username, --password, --nodb (to just execute some JS)

  • Common with in shell commands :
    help
    show dbs //even shows size
    show collections
    db //check current db
    use db_name //will be created if not exists
    db.stats()
    .pretty() at the end of query for formatted JSON o/p

Open connections

To check no of open connections (Note: clusterMonitor role must be added to the user)

use admin
db.auth('user','pwd')
db.serverStatus().connections

To find sources of connections (say source ip adress)

sudo lsof | grep mongod | grep TCP

Note : check (ESTABLISHED) message in the lines

  • Restart mongodb instance to kill most connections

White paper

  • RDBMStoMongoDBMigration.pdf added in current folder