Skip to content

MongoDB

Installation

Pre-requisites

  • Install node if not already done.

  • Install dependent package:

sh
# m from (https://github.com/aheckmann/m)
npm install -g m

Install MongoDB

  • Install MongoDB using m:
sh
m latest
# If the above doesn't work, list using `m list` and install a slighly older version as `m 8.0.5`
  • Create a symlink:
sh
sudo ln -s ~/.local/bin/mongod /usr/local/bin/mongod
  • Confirm mongodb installation:
sh
mongod --version

Mongo Shell

sh
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt update && sudo apt install -y mongodb-mongosh
mongosh --version

Install MongoDB Database Tools

  • To install MongoDB database tools, use following command:
sh
sudo apt install -y mongodb-database-tools

Setup local server

  • Create folders for all replicas
sh
mkdir -p /home/piratedev/databases/mongodb/server/mongo{1,2,3}/db
  • cd into the folder and create a keyfile:
sh
cd /home/piratedev/databases/mongodb/server/ && openssl rand -base64 755 > keyfile && chmod 400 keyfile
  • Create /home/piratedev/databases/mongodb/server/mongo1.conf file as follows:
yaml
storage:
    dbPath: mongo1/db
net:
    bindIp: 127.0.0.1,192.168.1.100
    port: 27017
security:
    authorization: enabled
    keyFile: keyfile
systemLog:
    destination: file
    path: mongo1/mongod.log
processManagement:
    fork: true
replication:
    replSetName: pirate-mongodb
  • Create mongo2.conf and mongo3.conf files similarly, but modify dbPath, systemLog -> path & port.
  • Start the daemons for all these three config files.
sh
mongod -f /home/piratedev/databases/mongodb/server/mongo1.conf && mongod -f /home/piratedev/databases/mongodb/server/mongo2.conf && mongod -f /home/piratedev/databases/mongodb/server/mongo3.conf
  • Check that the mongod daemons are running: ps -aux | grep mongod

  • Connect to the server using mongoDB shell: mongosh

  • Switch to admin database: use admin

  • Create a config object as follows:

sh
config = `{ _id: "pirate-mongodb", members: [{ _id: 0, host: "localhost:27017"}, { _id: 1, host: "localhost:27018" }, {_id: 2, host: "localhost:27019" }] }`
  • Initiate Replica Set: rs.initiate(config)
  • Create root user (that can create other users):
    • Password found in iCloud keychain.
python
db.createUser({user: "piratedev", pwd: "<password>", roles: ["root"]})
  • Authenticate against DB using the new user
sh
db.getSiblingDB("admin").auth("piratedev")
  • To check the replica set status: rs.status().
  • Create an alias for the command that starts the MongoDB server, if needed:
sh
# vi ~/.bashrc
alias start_mongo="mongod -f /home/piratedev/databases/mongodb/server/mongo1.conf && mongod -f /home/piratedev/databases/mongodb/server/mongo2.conf && mongod -f /home/piratedev/databases/mongodb/server/mongo3.conf"

Load Sample Data

sh
# Collection name will be automatically derived from filename as inventory
mongoimport --username="piratedev" --authenticationDatabase="admin" --db=piratedev ./mongoDB-essential-training-3023263/datasets/inventory.json

Test Connection

  • Download MongoDB Compass on your client machine (MacOS).
  • Use following details:
    • Host IP: 192.168.1.100
    • Port: 27017
    • Authentication Type: Username/Password
    • Username: piratedev
    • Password: <password>
    • Authentication Database: admin
  • Also check the box "Direct Connection" under General tab.
  • Save and connect to the database to see all collections.