Mongo: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 85: Line 85:
<syntaxhighlight>
<syntaxhighlight>
use admin
use admin
// Create the user
db.createUser({
db.createUser({
  user: "Employeeadmin",
  user: "mongoadmin",
  pwd: "password",
  pwd: "password",
  roles:[{role: "userAdmin" , db:"test_db"}]
  roles:[{role: "userAdmin" , db:"CatalogDb"}]
})
})
quit()
 
// Grant a role
db.grantRolesToUser( "mongoadmin", [  {role: "readWrite", db: "CatalogDb"} ] )
 
quit
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 21:11, 22 February 2025

Install

This was last done on Ubuntu 24.04

sudo bash
apt-get install gnupg -y

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor


echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

apt-get update 

sudo apt install -y mongodb-org

sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod

Test connection with

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

Do some stuff to show working

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

Configure Mongo

Create Admin User

Enter the shell

mongosh

Then use this command changed appropriately

db.createUser({
  user: "new_admin",
  pwd: "not_saying",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } , "readWriteAnyDatabase" ]
})

Then exit shell

exit

Configure Security and IP

In /etc/mongod.conf add

security:
  authorization: enabled

Also in /etc/mongod.conf change if you want to

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 192.168.1.70

Restart the service

sudo systemctl restart mongod

Test listening

sudo lsof -i | grep mongo

Test Admin

Now the admin user works

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

Create general user

Start the cli interface with

mongosh

Now type

use admin
// Create the user
db.createUser({
 user: "mongoadmin",
 pwd: "password",
 roles:[{role: "userAdmin" , db:"CatalogDb"}]
})

// Grant a role
db.grantRolesToUser( "mongoadmin", [  {role: "readWrite", db: "CatalogDb"} ] )

quit

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

Running a container

This is here we ease of use. Don't use this username or password

docker volume create  mongodata
docker run -d --rm --name mongo -p 27017:27017 -v mongodata -e MONGO_INITDB_ROOT_USERNAME=mongo_admin -e  MONGO_INITDB_ROOT_PASSWORD=pass1y### mongo