Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active January 27, 2023 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/844b28c877783e9397ae63bbae905f35 to your computer and use it in GitHub Desktop.
Save gilangvperdana/844b28c877783e9397ae63bbae905f35 to your computer and use it in GitHub Desktop.
MongoDB

MongoDB notes for Ubuntu 20.04LTS

Installation

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl start mongod
sudo systemctl status mongod

Create Super User

mongo
use admin
db.createUser(
{
    user: "yourUsername",
    pwd: "yourPassword",
    roles: [
              { role: "userAdminAnyDatabase", db: "admin" },
              { role: "readWriteAnyDatabase", db: "admin" },
              { role: "dbAdminAnyDatabase", db: "admin" },
              { role: "clusterAdmin", db: "admin" }
           ]
})

Activate Authorization Feature

nano /etc/mongod.conf

## from
#security:

## TO
security:
  authorization: 'enabled'
  
## Restart
systemctl restart mongod

Login from CLI

mongo
db.auth("yourUsername", "yourPassword")

Connect from Compass

mongodb://yourUsername:yourPassword@172.20.1.164:27018/?authMechanism=DEFAULT

Create Spesific User

mongo
use some_db
db.createUser(
  {
    user: "myNormalUser",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "some_db" },
             { role: "read", db: "some_other_db" } ]
  }
)

Open publicly

nano /etc/mongod.conf

## from
net:
  port: 27017
  bindIp: 127.0.0.1

## to
net:
  port: 27017
#  bindIp: 127.0.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment