Mongo: Difference between revisions
Jump to navigation
Jump to search
Line 59: | Line 59: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Create | ==Test Admin== | ||
Now the admin user works | |||
<syntaxhighlight> | |||
mongosh -u bibble_admin -p --authenticationDatabase admin | |||
use admin | |||
db.getUsers(); | |||
</syntaxhighlight> | |||
==Create general user== | |||
Start the cli interface with | Start the cli interface with | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
mongosh | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Now type | Now type | ||
Line 74: | Line 81: | ||
quit() | quit() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Connecting from cli= | =Connecting from cli= | ||
For admin | For admin |
Revision as of 21:19, 20 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
sudo vi /etc/mongod.conf
security:
authorization: enabled
Restart the service
sudo systemctl restart mongod
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
db.createUser({
user: "Employeeadmin",
pwd: "password",
roles:[{role: "userAdmin" , db:"test_db"}]
})
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