# Install MongoDB and Mongosh

## In this learning path

- [Introduction](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb-on-azure/)
- [What are Cobalt 100 and MongoDB?](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb-on-azure/background/)
- [Create an Arm-based cloud virtual machine using Cobalt 100](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb-on-azure/create-instance/)
- [Install MongoDB and Mongosh](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb-on-azure/deploy/)
- [MongoDB Baseline Testing](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb-on-azure/baseline-testing/)
- [Monitor MongoDB with mongotop](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb-on-azure/mongotop/)
- [Monitor MongoDB with mongostat](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb-on-azure/benchmarking/)
- [Monitor MongoDB with mongostat](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb-on-azure/mongostat/)
- [Next Steps](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb-on-azure/_next-steps/)

## Get started
Install MongoDB and **mongosh** on Ubuntu Pro 24.04 LTS (Arm64). You will download the binaries, add them to your `PATH`, create data and log directories, and start the server locally to verify the setup.

> **Note**  
> For this exercise, `mongod` runs locally and **access control is disabled** by default. Keep the server bound to `127.0.0.1` until you configure users and authentication. To accept remote connections later, set `--bind_ip` (or `bindIp` in the config) and enable authorization.

## Install system dependencies
Install the required system packages to support MongoDB:

```
sudo apt update
sudo apt install -y curl wget tar fio openssl libcurl4 net-tools
```

## Download and extract MongoDB
Fetch and unpack the MongoDB binaries for Arm64:

```
wget https://fastdl.mongodb.org/linux/mongodb-linux-aarch64-ubuntu2404-8.0.12.tgz
tar -xvzf mongodb-linux-aarch64-ubuntu2404-8.0.12.tgz
sudo mv mongodb-linux-aarch64-ubuntu2404-8.0.12 /usr/local/mongodb
```

## Add MongoDB to the system PATH
Enable running MongoDB from any terminal session:

```
echo 'export PATH=/usr/local/mongodb/bin:$PATH' | sudo tee /etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh
```

## Create data and log directories
Create the database data and log directories and assign ownership to your user:

```
sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb
sudo chown -R $USER:$USER /var/lib/mongo /var/log/mongodb
```

## Start the MongoDB server (local)
Start `mongod` in the background and write logs to `/var/log/mongodb/mongod.log`:

```
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
```

Sample output:
```
about to fork child process, waiting until server is ready for connections.
forked process: 3356
child process started successfully, parent exiting
```

## Install mongosh (MongoDB shell)
**mongosh** is the MongoDB shell for running queries and database operations.

Download and install the MongoDB shell for Arm64:

```
wget https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-arm64.tgz
tar -xvzf mongosh-2.3.8-linux-arm64.tgz
sudo mv mongosh-2.3.8-linux-arm64 /usr/local/mongosh
```

Add `mongosh` to the system `PATH`:

```
echo 'export PATH=/usr/local/mongosh/bin:$PATH' | sudo tee /etc/profile.d/mongosh.sh
source /etc/profile.d/mongosh.sh
```

## Verify MongoDB and mongosh installation
Confirm that MongoDB and `mongosh` are installed and available:

```
mongod --version
mongosh --version
```

You should see output similar to:
```
db version v8.0.12
Build Info: {
    "version": "8.0.12",
    "gitVersion": "b60fc6875b5fb4b63cc0dbbd8dda0d6d6277921a",
    "openSSLVersion": "OpenSSL 3.0.13 30 Jan 2024",
    "modules": [],
    "allocator": "tcmalloc-google",
    "environment": {
        "distmod": "ubuntu2404",
        "distarch": "aarch64",
        "target_arch": "aarch64"
    }
}
2.3.8
```

## Connect to MongoDB using mongosh
Connect to the local instance and verify an interactive prompt:

```
mongosh mongodb://127.0.0.1:27017
```

You should see output similar to:
```
Current Mongosh Log ID: 68b573411523231d81a00aa0
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.3.8
Using MongoDB:          8.0.12
Using Mongosh:          2.3.8
mongosh 2.5.7 is available for download: https://www.mongodb.com/try/download/shell
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
------
   The server generated these startup warnings when booting
   2025-09-01T09:45:32.382+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2025-09-01T09:45:33.012+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2025-09-01T09:45:33.012+00:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
   ...
```

With this verification complete, proceed to MongoDB baseline testing on your Azure Cobalt 100 VM.
