Mongo

From bibbleWiki
Jump to navigation Jump to search

Install

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

Test connection with

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

Do some stuff to show working

mongo
use test_db
db.files.insert({"name":"rap"})
db.dropDatabase()

Create Root User

db.createUser(
      {
          user: "username",
          pwd: "password",
          roles: [ "root" ]
      }
  )

Configure

nano /etc/mongod.conf

security:
  authorization: enabled

Restart the service

systemctl restart mongod

Create a user

Start the cli interface with

mongo

Now type

use admin
db.createUser({
 user: "Employeeadmin",
 pwd: "password",
 roles:[{role: "userAdmin" , db:"test_db"}]
})
quit()

No test by Test is

mongo -u admin -p --authenticationDatabase admin
use admin
db.getUsers();

Connecting from cli

For admin

mongo -u  admin -p --authenticationDatabase  admin

For normal user

mongo -u  test_admin -p test_admin --host localhost

Create/Show/Delete a database and user

Ezzy pezzy

use mydb
show dbs
db.dropDatabase()

Grant Authority to A Role

Two commands are used

db.auth("user","password")
db.grantRolesToUser( "user", [ { role: "dbOwner", db: "test_db" } ])