Skip to content

Instantly share code, notes, and snippets.

@Ghepes
Last active April 30, 2025 04:09
Show Gist options
  • Save Ghepes/f63be5ac6a5c95ac7d49941f657c41f3 to your computer and use it in GitHub Desktop.
Save Ghepes/f63be5ac6a5c95ac7d49941f657c41f3 to your computer and use it in GitHub Desktop.
MongoDB Privat
ssh user@ip_vm
# Importă cheia GPG
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg \
--dearmor
# Adaugă repository-ul
echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
# Actualizează și instalează
sudo apt update
sudo apt install -y mongodb-org
# Start mongodb
sudo systemctl start mongod
sudo systemctl enable mongod
# Verify
mongosh
# Edit to global
sudo nano /etc/mongod.conf
Add: bindIp: 0.0.0.0 # connect extern
# Security
security:
authorization: enabled
# Add admin:
mongosh
use admin
db.createUser({
user: "admin",
pwd: "parola123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
# (Recommended) Open port 27017 for your IPs only NOT All IPv4 !
sudo ufw allow from IP_YOUR to any port 27017
MongoDB comes with only the console (mongosh), but you can add a separate dashboard, such as:
🔧 Popular dashboard options for MongoDB:
Name Description Hosting
Mongo Express Simple Web UI for MongoDB Local (Node.js)
NoSQLBooster / Compass Desktop GUI applications On your PC
FerretDB UI / AdminMongo Lightweight open-source alternatives Local/Web
# Mongo Express – The simplest web dashboard
# install
sudo apt install nodejs npm -y
sudo npm install -g mongo-express
# Congig
nano ~/mongo-express-config.js
# Add
module.exports = {
mongodb: {
server: "127.0.0.1",
port: 27017,
auth: false, // sau true dacă ai setat user
},
site: {
baseUrl: "/",
port: 8081,
cookieSecret: "supersecret",
},
};
# Start with
mongo-express -c ~/mongo-express-config.js
# ✅ Acces browser:
http://IP_VM:8081
What domain/host are you using for MongoDB in the application?
a) If the application is on the same VM:
mongodb://localhost:27017/database_name
If the application is on another server, use the public IP of the VM:
mongodb://admin:[email protected]:27017/database_name
Connecting between the application and MongoDB itself (Node.js example)
const mongoose = require('mongoose');
mongoose.connect('mongodb://admin:[email protected]:27017/myapp', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('Conectat la MongoDB');
}).catch(err => {
console.error('Eroare conectare:', err);
});
my MongoDB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment