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