MongoDB
Installation
Pre-requisites
Install
nodeif not already done.Install dependent package:
sh
# m from (https://github.com/aheckmann/m)
npm install -g mInstall 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 --versionMongo Shell
- Install MongoDB Shell using the following commands:
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 --versionInstall MongoDB Database Tools
- To install MongoDB database tools, use following command:
sh
sudo apt install -y mongodb-database-toolsSetup local server
- Create folders for all replicas
sh
mkdir -p /home/piratedev/databases/mongodb/server/mongo{1,2,3}/dbcdinto 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.conffile 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.confandmongo3.conffiles similarly, but modifydbPath,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.confCheck that the mongod daemons are running:
ps -aux | grep mongodConnect to the server using mongoDB shell:
mongoshSwitch to admin database:
use adminCreate 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
- Clone this GitHub repo
- Import using
mongoimportcommand:
sh
# Collection name will be automatically derived from filename as inventory
mongoimport --username="piratedev" --authenticationDatabase="admin" --db=piratedev ./mongoDB-essential-training-3023263/datasets/inventory.jsonTest 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
- Host IP:
- Also check the box "Direct Connection" under
Generaltab. - Save and connect to the database to see all collections.
