This guide will help you set up a remote MongoDB instance on your VPS for development, allowing you to access the same database from any device.
📖 Related Documentation:
- Local MongoDB Setup - Local database setup (recommended)
- Development Setup - Complete development environment setup
- Environment Management - Environment configuration
- A VPS with SSH access
- Root or sudo access on the VPS
- MongoDB installed on the VPS (or ability to install it)
# Import MongoDB public GPG key
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
# Add MongoDB repository
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
# Update package list
sudo apt-get update
# Install MongoDB
sudo apt-get install -y mongodb-org
# Start MongoDB service
sudo systemctl start mongod
sudo systemctl enable mongod# Create MongoDB repository file
sudo vi /etc/yum.repos.d/mongodb-org-7.0.repo
# Add the following content:
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc
# Install MongoDB
sudo yum install -y mongodb-org
# Start MongoDB service
sudo systemctl start mongod
sudo systemctl enable mongodEdit the MongoDB configuration file:
sudo nano /etc/mongod.confUpdate the net section to bind to all interfaces:
net:
port: 27017
bindIp: 0.0.0.0 # Change from 127.0.0.1 to allow remote connectionsConnect to MongoDB:
mongoshCreate an admin user:
use admin
db.createUser({
user: "admin",
pwd: "your-secure-password-here",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
})Create a database-specific user for your POWERBACK application:
use powerback
db.createUser({
user: "powerback_dev",
pwd: "your-dev-password-here",
roles: [ { role: "readWrite", db: "powerback" } ]
})Exit MongoDB shell:
exitEdit /etc/mongod.conf:
sudo nano /etc/mongod.confAdd security section:
security:
authorization: enabledRestart MongoDB:
sudo systemctl restart mongod# Allow MongoDB port
sudo ufw allow 27017/tcp
# Or restrict to specific IP (more secure)
sudo ufw allow from YOUR_DEVICE_IP to any port 27017# Allow MongoDB port
sudo firewall-cmd --permanent --add-port=27017/tcp
sudo firewall-cmd --reload
# Or restrict to specific IP (more secure)
sudo firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='YOUR_DEVICE_IP' port port='27017' protocol='tcp' accept"
sudo firewall-cmd --reloadCreate or update your .env file in the project root:
# Database - Remote MongoDB on VPS
MONGODB_URI=mongodb://powerback_dev:your-dev-password-here@your-vps-ip:27017/powerback?authSource=powerback
# Or with authentication options:
MONGODB_URI=mongodb://powerback_dev:your-dev-password-here@your-vps-ip:27017/powerback?authSource=powerback&retryWrites=true&w=majorityConnection String Format:
mongodb://[username]:[password]@[host]:[port]/[database]?[options]
Test the connection from your local machine:
# Install MongoDB shell tools locally (optional)
# Then test connection:
mongosh "mongodb://powerback_dev:your-dev-password-here@your-vps-ip:27017/powerback?authSource=powerback"Or test from your Node.js application:
cd /home/jonathan/dev/powerback
npm run devCheck the logs to confirm successful connection.
Instead of exposing MongoDB directly, use an SSH tunnel:
# Create SSH tunnel (run this on your local machine)
ssh -L 27017:localhost:27017 user@your-vps-ip
# Then in another terminal, update .env to use localhost:
MONGODB_URI=mongodb://powerback_dev:your-dev-password-here@127.0.0.1:27017/powerback?authSource=powerbackOnly allow connections from your development IPs:
# Ubuntu/Debian
sudo ufw delete allow 27017/tcp
sudo ufw allow from YOUR_DEVICE_IP to any port 27017
# CentOS/RHEL
sudo firewall-cmd --remove-port=27017/tcp --permanent
sudo firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='YOUR_DEVICE_IP' port port='27017' protocol='tcp' accept"
sudo firewall-cmd --reloadGenerate strong passwords:
# Generate random password
openssl rand -base64 32For production-like security, enable SSL/TLS encryption. See MongoDB documentation for SSL setup.
The application has been updated to support remote connections with improved timeout settings:
serverSelectionTimeoutMS: 30000- 30 seconds to select server (increased from 10s)socketTimeoutMS: 45000- 45 seconds for socket operationsmaxPoolSize: 10- Connection pool size
- Check MongoDB is running:
sudo systemctl status mongod - Check firewall rules:
sudo ufw statusorsudo firewall-cmd --list-all - Verify bindIp in
/etc/mongod.confis0.0.0.0 - Check MongoDB logs:
sudo tail -f /var/log/mongodb/mongod.log
- Verify username and password in connection string
- Check
authSourceparameter matches the database where user was created - Verify user exists:
mongosh -u admin -p admin-password --authenticationDatabase admin - Check user roles:
db.getUser("powerback_dev")
- Check network connectivity:
ping your-vps-ip - Verify port is open:
telnet your-vps-ip 27017 - Check MongoDB logs for errors
- Increase timeout values if needed (already configured in db.js)
Without authentication (not recommended):
MONGODB_URI=mongodb://your-vps-ip:27017/powerback
With authentication:
MONGODB_URI=mongodb://username:password@your-vps-ip:27017/powerback?authSource=powerback
With multiple options:
MONGODB_URI=mongodb://username:password@your-vps-ip:27017/powerback?authSource=powerback&retryWrites=true&w=majority&ssl=false
- Update your
.envfile with the remote MongoDB connection string - Test the connection by starting your development server
- Consider setting up an SSH tunnel for additional security
- Keep your MongoDB credentials secure and never commit them to git