MongoDB: Difference between revisions
Jump to navigation
Jump to search
Line 98: | Line 98: | ||
== Add User == | == Add User == | ||
<pre> | |||
use Myachme ; | |||
db.createUser( | |||
{ | |||
user: "myachme", | |||
pwd: "supersecret", | |||
roles: [ "readWrite", "dbAdmin" ], | |||
mechanisms: [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] | |||
} | |||
) ; | |||
</pre> | |||
<pre> | <pre> |
Latest revision as of 01:20, 16 December 2024
MongoDB
MongoDB https://www.mongodb.com/
Install MongoDB Community Edition
sudo apt-get install gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \ sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \ --dearmor
# Ubuntu 20: echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
# Ubuntu 22: echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
Prevent unintended upgrades: (pin)
echo "mongodb-org hold" | sudo dpkg --set-selections echo "mongodb-org-database hold" | sudo dpkg --set-selections echo "mongodb-org-server hold" | sudo dpkg --set-selections echo "mongodb-mongosh hold" | sudo dpkg --set-selections echo "mongodb-org-mongos hold" | sudo dpkg --set-selections echo "mongodb-org-tools hold" | sudo dpkg --set-selections
ref:
Install MongoDB Community Edition on Ubuntu - MongoDB Manual v7.0 https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
Manage MonoDB Service
sudo systemctl start mongod sudo systemctl daemon-reload sudo systemctl status mongod sudo systemctl enable mongod sudo systemctl stop mongod sudo systemctl restart mongod
Port 27017
port 27017
mongosh
mongosh
Configuration
/etc/mongod.conf
default:
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /var/lib/mongodb # engine: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # network interfaces net: port: 27017 bindIp: 127.0.0.1 # how the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo #security: #operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog:
Add User
use Myachme ; db.createUser( { user: "myachme", pwd: "supersecret", roles: [ "readWrite", "dbAdmin" ], mechanisms: [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } ) ;
use test db.createUser( { user: "myTester", pwd: passwordPrompt(), // or cleartext password roles: [ { role: "readWrite", db: "test" }, { role: "read", db: "reporting" } ] } )
{ user: "<name>", pwd: passwordPrompt(), // Or "<cleartext password>" customData: { <any information> }, roles: [ { role: "<role>", db: "<database>" } | "<role>", ... ], authenticationRestrictions: [ { clientSource: ["<IP>" | "<CIDR range>", ...], serverAddress: ["<IP>" | "<CIDR range>", ...] }, ... ], mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ], passwordDigestor: "<server|client>" }
use products db.createUser( { user: "accountUser", pwd: passwordPrompt(), // Or "<cleartext password>" roles: [ "readWrite", "dbAdmin" ] } )
ref: https://www.mongodb.com/docs/manual/tutorial/create-users/
ref: https://www.mongodb.com/docs/manual/reference/method/db.createUser/
Uninstall MongoDB
Stop service:
sudo service mongod stop
Remove packages:
sudo apt-get purge "mongodb-org*"
Remove Data directories:
sudo rm -r /var/log/mongodb sudo rm -r /var/lib/mongodb