> For the complete documentation index, see [llms.txt](https://guvi.gitbook.io/fsd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guvi.gitbook.io/fsd/docs/module-7-database/mongodb/installation-and-basic-configuration.md).

# Installation and Basic Configuration

### Introduction

MongoDB is a popular NoSQL database known for its high performance, scalability, and flexibility. This guide will walk you through the process of installing and configuring MongoDB on your system. Whether you are using Windows, macOS, or Linux, this step-by-step tutorial will help you get started with MongoDB.

### Prerequisites

Before we begin, ensure you have the following:

* Administrator access to your system
* Basic knowledge of command-line interface

### Installation

#### Windows

**Step 1: Download MongoDB**

1. Go to the [MongoDB Download Center](https://www.mongodb.com/try/download/community) and select the MongoDB Community Server.
2. Choose the version compatible with your Windows OS and download the installer.

**Step 2: Install MongoDB**

1. Run the downloaded `.msi` file.
2. Follow the setup wizard, selecting the Complete setup type.
3. Choose to install MongoDB as a service.
4. Configure the Data and Log directories (default paths are usually sufficient).

**Step 3: Verify Installation**

1. Open Command Prompt.
2. Run the command:

   ```bash
   mongod --version
   ```

   This should display the installed MongoDB version.

#### macOS

**Step 1: Install Homebrew**

If you haven't installed Homebrew, run:

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

**Step 2: Install MongoDB**

1. Tap the MongoDB formula:

   ```bash
   brew tap mongodb/brew
   ```
2. Install MongoDB:

   ```bash
   brew install mongodb-community
   ```

**Step 3: Start MongoDB**

1. Start MongoDB as a service:

   ```bash
   brew services start mongodb/brew/mongodb-community
   ```
2. Verify installation by checking the version:

   ```bash
   mongod --version
   ```

#### Linux (Ubuntu)

**Step 1: Import the Public Key**

```bash
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
```

**Step 2: Create a List File for MongoDB**

```bash
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
```

**Step 3: Reload the Local Package Database**

```bash
sudo apt-get update
```

**Step 4: Install MongoDB Packages**

```bash
sudo apt-get install -y mongodb-org
```

**Step 5: Start MongoDB**

```bash
sudo systemctl start mongod
```

**Step 6: Verify Installation**

```bash
mongod --version
```

### Basic Configuration

#### Configuration File

MongoDB’s configuration file is located at `/etc/mongod.conf` by default. Key sections include:

* **Storage**: Configures the storage engine and database file paths.
* **Net**: Configures network settings including the port MongoDB listens on.
* **Security**: Configures security options like authorization.

#### Example Configuration (`/etc/mongod.conf`)

```yaml
# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

# Where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1

# Security
security:
  authorization: enabled
```

#### Starting and Stopping MongoDB

**Windows**

Use the `net` command:

```bash
net start MongoDB
net stop MongoDB
```

**macOS**

Using Homebrew services:

```bash
brew services start mongodb/brew/mongodb-community
brew services stop mongodb/brew/mongodb-community
```

**Linux**

Using `systemctl`:

```bash
sudo systemctl start mongod
sudo systemctl stop mongod
```

#### Enabling Authentication

1. Create an administrative user.

   ```bash
   use admin
   db.createUser(
     {
       user: "admin",
       pwd: "password",
       roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
     }
   )
   ```
2. Restart MongoDB with authentication enabled.
3. Authenticate using the created user:

   ```bash
   mongo -u "admin" -p "password" --authenticationDatabase "admin"
   ```

### Conclusion

You have now installed and configured MongoDB on your system. This guide covered the essential steps to get you started with MongoDB, including installation, basic configuration, and enabling authentication. For more detailed configuration and advanced features, refer to the [MongoDB documentation](https://docs.mongodb.com/).
