Mongo: Difference between revisions
Jump to navigation
Jump to search
(10 intermediate revisions by the same user not shown) | |||
Line 20: | Line 20: | ||
Test connection with | Test connection with | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
mongosh --eval 'db.runCommand({ connectionStatus: 1 })' | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Do some stuff to show working | Do some stuff to show working | ||
<syntaxhighlight lang="mongo"> | <syntaxhighlight lang="mongo"> | ||
mongosh | |||
use test_db | use test_db | ||
db.files.insert({"name":"rap"}) | db.files.insert({"name":"rap"}) | ||
db.dropDatabase() | db.dropDatabase() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Configure Mongo= | |||
=Create | ==Create Admin User== | ||
Enter the shell | |||
<syntaxhighlight> | <syntaxhighlight> | ||
mongosh | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=Configure= | Then use this command changed appropriately | ||
<syntaxhighlight> | |||
db.createUser({ | |||
user: "new_admin", | |||
pwd: "not_saying", | |||
roles: [ { role: "userAdminAnyDatabase", db: "admin" } , "readWriteAnyDatabase" ] | |||
}) | |||
</syntaxhighlight> | |||
Then exit shell | |||
<syntaxhighlight> | |||
exit | |||
</syntaxhighlight> | |||
==Configure Security and IP== | |||
In /etc/mongod.conf add | |||
<syntaxhighlight> | <syntaxhighlight> | ||
security: | security: | ||
authorization: enabled | authorization: enabled | ||
</syntaxhighlight> | |||
Also in /etc/mongod.conf change if you want to | |||
<syntaxhighlight> | |||
# network interfaces | |||
net: | |||
port: 27017 | |||
bindIp: 127.0.0.1, 192.168.1.70 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Restart the service | Restart the service | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl restart mongod | sudo systemctl restart mongod | ||
</syntaxhighlight> | |||
Test listening | |||
<syntaxhighlight lang="bash"> | |||
sudo lsof -i | grep mongo | |||
</syntaxhighlight> | |||
==Test Admin== | |||
Now the admin user works | |||
<syntaxhighlight> | |||
mongosh -u bibble_admin -p --authenticationDatabase admin | |||
use admin | |||
db.getUsers(); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=Create | ==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 | ||
<syntaxhighlight> | <syntaxhighlight> | ||
use admin | use admin | ||
// Create the user | |||
db.createUser({ | db.createUser({ | ||
user: " | user: "mongoadmin", | ||
pwd: "password", | pwd: "password", | ||
roles:[{role: "userAdmin" , db:" | roles:[{role: "userAdmin" , db:"CatalogDb"}] | ||
}) | }) | ||
// Grant a role | |||
db.grantRolesToUser( "mongoadmin", [ {role: "readWrite", db: "CatalogDb"} ] ) | |||
quit | |||
db. | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=Connecting from cli= | =Connecting from cli= | ||
For admin | For admin |
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