curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
echo "deb [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 update && sudo apt install -y mongodb-org
sudo systemctl enable --now mongod
A fresh install has no authentication. Create users first:
// mongosh
use admin
db.createUser({ user: "admin", pwd: passwordPrompt(), roles: ["root"] })
use shop
db.createUser({ user: "shopapp", pwd: passwordPrompt(), roles: [{ role: "readWrite", db: "shop" }] })
Then edit /etc/mongod.conf:
security:
authorization: enabled
net:
bindIp: 127.0.0.1,10.0.1.20
sudo systemctl restart mongod
mongosh "mongodb://shopapp@10.0.1.20/shop" --authenticationDatabase shop
Leaving MongoDB reachable without authentication has caused many public data leaks. Restrict the bind address and the firewall, and plan the move to MongoDB 8.0 (due this autumn) on a test system first.