Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Keep MongoDB Local

MongoDB's packaged configuration listens on 127.0.0.1 and port 27017. Leave it so. Applications on the same server connect locally; see what is localhost. You reach it from your own computer through an SSH tunnel, ssh -L 27018:127.0.0.1:27017 user@your_server_ip, as shown in SSH port forwarding. If another server must connect, bind a private address as well, allow only that server's address in the firewall, require TLS, and never open port 27017 to the internet.

Before You Start

  • Supported release: the vendor's supported-platforms page lists the Ubuntu releases each MongoDB series supports; at the time of writing, the 9.0 series supports Ubuntu 24.04 and, from 9.0.4, Ubuntu 26.04. If yours is not listed, wait rather than borrow another release's repository.
  • CPU features: MongoDB 5.0 and later on x86_64 need a processor with AVX. Check with grep -c avx /proc/cpuinfo; 0 means the server will not start.
  • Memory: by default the storage engine's cache takes the larger of 50% of (RAM minus 1 GB) and 256 MB, so plan for that on a small VPS.

Step 1: Add the Repository

A third-party repository gives its publisher the power to install software as root on your server, so add only the vendor's own. The documented method has three parts: install the tools, store the signing key in a keyring file, and add a repository entry that refers to that key. The commands below use series 9.0 on Ubuntu 26.04 (resolute) as an example; copy the key and repository lines for your release from the vendor's current installation page.

sudo apt install gnupg curl
curl -fsSL https://pgp.mongodb.com/server-9.asc | \
  sudo gpg -o /usr/share/keyrings/mongodb-server-9.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-9.gpg ] https://repo.mongodb.org/apt/ubuntu resolute/mongodb-org/9.0 multiverse" | \
  sudo tee /etc/apt/sources.list.d/mongodb-org-9.0.list

The first pipe converts the downloaded key into a binary keyring; nothing is run as a script. The signed-by option means apt trusts that key for this repository only. On Ubuntu 24.04, the release name in the line is noble.

Step 2: Install and Start

sudo apt update
sudo apt install mongodb-org
sudo systemctl enable --now mongod
sudo systemctl status mongod

If the service fails, read sudo journalctl -u mongod and /var/log/mongodb/mongod.log. Missing AVX and a data folder with the wrong owner are common causes.

Step 3: Connect with mongosh

mongosh

The shell connects to the local server. At this point access control is off, so anyone on the server can read and write everything. Do the next two steps before you load any data.

Step 4: Create an Administrative User

Inside mongosh:

use admin
db.createUser({
  user: "admin",
  pwd: passwordPrompt(),
  roles: [
    { role: "userAdminAnyDatabase", db: "admin" },
    { role: "readWriteAnyDatabase", db: "admin" }
  ]
})

passwordPrompt() asks for the password, so it does not stay in the shell history.

Step 5: Turn On Access Control

Open /etc/mongod.conf. Check that the net section keeps bindIp: 127.0.0.1, and add a security section:

net:
  port: 27017
  bindIp: 127.0.0.1

security:
  authorization: enabled

The file is YAML: indent with spaces, never tabs. Restart and log in again:

sudo systemctl restart mongod
mongosh --authenticationDatabase admin -u admin -p

With -p as the last option and no value, mongosh asks for the password.

Step 6: A User for the Application

use app
db.createUser({
  user: "app_user",
  pwd: passwordPrompt(),
  roles: [ { role: "readWrite", db: "app" } ]
})

The application logs in as app_user and can read and write the app database only. Keep its password in the application's configuration, not in a command line.

Firewall and Backups

Add no firewall rule for port 27017: a local application needs none, and with ufw active and no rule, the port stays closed to other machines. Back up with mongodump, which comes in the mongodb-database-tools package that mongodb-org installs, and copy the dumps off the server. To decide whether a document database fits your data at all, read SQL vs NoSQL.

Was this answer helpful?
Loading...