Mongo: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "=Install= <syntaxhighlight lang="bash"> 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=a..."
 
No edit summary
Line 22: Line 22:
db.dropDatabase()
db.dropDatabase()
</syntaxhighlight>
</syntaxhighlight>
==Create Root User==
=Create Root User=
<syntaxhighlight>
<syntaxhighlight>
db.createUser(
db.createUser(
Line 32: Line 32:
   )
   )
</syntaxhighlight>
</syntaxhighlight>
==Configure==
=Configure=
nano /etc/mongod.conf
nano /etc/mongod.conf
<syntaxhighlight>
<syntaxhighlight>
Line 42: Line 42:
systemctl restart mongod
systemctl restart mongod
</syntaxhighlight>
</syntaxhighlight>
==Create a user==
=Create a user=
Start the cli interface with  
Start the cli interface with  
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 63: Line 63:
db.getUsers();
db.getUsers();
</syntaxhighlight>
</syntaxhighlight>
 
=Connecting from cli=
==Connecting from cli==
For admin
For admin
<syntaxhighlight>
<syntaxhighlight>
Line 73: Line 72:
mongo -u  test_admin -p test_admin --host localhost
mongo -u  test_admin -p test_admin --host localhost
</syntaxhighlight>
</syntaxhighlight>
==Create/Show/Delete a database and user==
=Create/Show/Delete a database and user=
Ezzy pezzy
Ezzy pezzy
<syntaxhighlight>
<syntaxhighlight>
Line 80: Line 79:
db.dropDatabase()
db.dropDatabase()
</syntaxhighlight>
</syntaxhighlight>
==Grant Authority to A Role
=Grant Authority to A Role=
Two commands are used
Two commands are used
<syntaxhighlight>
<syntaxhighlight>
db.auth("user","password")
db.auth("user","password")
db.grantRolesToUser( "user", [ { role: "dbOwner", db: "test_db" } ])
db.grantRolesToUser( "user", [ { role: "dbOwner", db: "test_db" } ])
</syntaxhighlight>
</syntaxhighlight>

Revision as of 02:39, 7 November 2020

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" } ])