Skip to main content

Databases · Beginner · 13 min

How to Install MongoDB 9.0 and Create a Database and User

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · MongoDB 9.0 (MongoDB apt repo, resolute)

You will install MongoDB 9.0 Community from the official MongoDB apt repository on Ubuntu 26.04, turn on authentication, and create an admin user and a limited user for one app database.

On this page

MongoDB is a document database. Instead of tables and rows, it stores JSON-like documents in collections, which suits data whose shape changes often. Ubuntu does not package MongoDB, so you install it from MongoDB's own apt repository. MongoDB publishes packages for Ubuntu 26.04 (code name resolute) for both x86-64 and ARM64.

You will add that repository, install MongoDB 9.0, and start it. A fresh MongoDB has no users and accepts every command from the local machine, so you will create an admin user, turn on authentication, and then create a second user that can read and write one database only.

Step 1 · Add the MongoDB apt repository

Install the tools that fetch and check the repository key:

Bash
sudo apt-get update
sudo apt-get install -y curl gnupg ca-certificates

Download the signing key for the 9.0 series and look at its fingerprint before you trust it:

Bash
curl -fsSL https://pgp.mongodb.com/server-9.asc -o /tmp/mongodb-server-9.asc
gpg --show-keys --with-fingerprint /tmp/mongodb-server-9.asc

The output names MongoDB 9 Release Signing Key <packaging@mongodb.com> and the fingerprint B3B4 2B6C 39E5 CDDE C0A2 7E3C F366 D55B 602E 502D. Compare it with the key listed in MongoDB's installation guide. Then store the key and add the repository:

Bash
sudo gpg --dearmor --yes -o /usr/share/keyrings/mongodb-server-9.0.gpg /tmp/mongodb-server-9.asc
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-9.0.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
sudo apt-get update

Check it worked

Bash
apt-cache policy mongodb-org

The Candidate line shows a version that starts with 9.0., from repo.mongodb.org.

Step 2 · Install MongoDB

The mongodb-org package installs the server (mongod), the shell (mongosh), and the database tools such as mongodump.

Bash
sudo apt-get install -y mongodb-org

Start the service and turn it on for every boot:

Bash
sudo systemctl enable --now mongod
systemctl status mongod

The status shows active (running). Press q to leave it. The service is named mongod, with a d.

Check it worked

Bash
mongod --version | head -n 1
mongosh --quiet --eval 'db.version()'

The first command prints db version v9.0. and the patch number. The second prints the version number alone, for example 9.0.8.

Troubleshooting

  • MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017: the server is not running. Check systemctl status mongod and read /var/log/mongodb/mongod.log.
  • Unable to locate package mongodb-org: the repository line or the key is wrong. Check /etc/apt/sources.list.d/mongodb-org-9.0.list and run sudo apt-get update again, looking for errors about repo.mongodb.org.
  • The service fails right after you change /etc/mongod.conf: the file is YAML, so indentation matters. Use spaces, never tabs, and check the log for the line number.

Step 3 · Create an admin user

Right now anyone on the machine can do anything in the database. Create an administrator first, while that is still possible. Open the shell:

Bash
mongosh

At the test> prompt, switch to the admin database and create the user. passwordPrompt() asks you for the password in the terminal, so it is not stored in your shell history or in the shell's own history file.

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

The reply is { ok: 1 }. The role userAdminAnyDatabase lets this user create and manage users on any database, and it cannot read or write ordinary data. Use a long random password and keep it in a password manager. Type exit to leave the shell.

Check it worked

Bash
mongosh --quiet admin --eval 'db.getUsers().users.map(u => u.user + " " + u.roles.map(r => r.role).join(","))'

The output lists admin userAdminAnyDatabase. The check runs without a login because authentication is still off.

Step 4 · Turn on authentication

Edit /etc/mongod.conf. The file ships with a commented-out #security: line. Replace it with the setting that makes MongoDB demand a login:

Bash
sudo sed -i 's/^#security:/security:\n  authorization: enabled/' /etc/mongod.conf
grep -A1 '^security:' /etc/mongod.conf

The output is security: followed by authorization: enabled, indented by two spaces. Restart MongoDB so it reads the change:

Bash
sudo systemctl restart mongod

Check it worked

A command without credentials is now refused:

Bash
mongosh --quiet appdb --eval 'db.getCollectionNames()'

The shell prints an error something like MongoServerError: Command listCollections requires authentication. If it lists collections instead, authentication is not on yet, so check the indentation of the security: block.

Step 5 · Create a user for your app

Log in as the admin user. --authenticationDatabase admin tells MongoDB which database holds the admin account, and -p with no value makes the shell ask for the password:

Bash
mongosh -u admin -p --authenticationDatabase admin

At the prompt, switch to the app's database and create a user with read and write rights on that database only. The database is created the first time you write to it, so there is nothing else to set up.

JavaScript
use appdb
db.createUser({
  user: "appuser",
  pwd: passwordPrompt(),
  roles: [{ role: "readWrite", db: "appdb" }]
})

The reply is { ok: 1 }. The user is stored in appdb, so appdb is the database it logs in against. Type exit to leave.

Step 6 · Connect as the app user and store a document

Connect to appdb as appuser. The shell asks for the password:

Bash
mongosh "mongodb://appuser@127.0.0.1:27017/appdb" -p

Insert a document and read it back:

JavaScript
db.notes.insertOne({ body: "first note", createdAt: new Date() })
db.notes.find()

The first command replies with acknowledged: true and an insertedId. The second prints the document with the same _id. MongoDB made the notes collection when you inserted the first document.

Check it worked

Confirm that the user is limited to its own database. As appuser, try to read another one:

In a session you opened as appuser, run db.getSiblingDB("admin").getCollectionNames(). The shell prints an error something like MongoServerError: not authorized on admin to execute command, followed by the command details, because the user has no rights on admin.

Your app connects with a connection string like this one. Put it in an environment variable or a secrets file, never in the code:

Text
mongodb://appuser:PASSWORD@127.0.0.1:27017/appdb

If the password has characters such as @, : or /, replace each with its percent-encoded form (%40, %3A, %2F), or the driver will misread the string.

Step 7 · Check what is open to the network

The default configuration listens on the local machine only. Look at it:

Bash
grep -A3 '^net:' /etc/mongod.conf

The output shows port: 27017 and bindIp: 127.0.0.1. That is the safe setting. Do not change bindIp to 0.0.0.0 on a server with a public address. If an app on another machine must connect, add that machine's private address to bindIp, separated by a comma (bindIp: 127.0.0.1,10.0.0.5), and allow only that machine to reach port 27017 in the firewall.

The files live here:

| Path | What it holds |
|---|---|
| /etc/mongod.conf | The server configuration |
| /var/lib/mongodb | The data directory |
| /var/log/mongodb/mongod.log | The server log |

Next steps

  • Close every port you do not use: /tutorials/set-up-ufw-firewall-ubuntu-26-04.
  • Harden how you log in to the server: /tutorials/harden-ssh-on-ubuntu-26-04.
  • The reference for the repository setup and the roles is MongoDB's manual at mongodb.com/docs/manual.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.