MongoDB: Tutorial & Best Practices

A NoSQL database

What is MongoDB?

MongoDB is a powerful, open-source, NoSQL database that uses a flexible, JSON-like document structure. This means it stores data in a way that's easy to read and scale. Unlike traditional SQL databases which use tables and rows, MongoDB uses collections and documents, making it perfect for handling large volumes of unstructured data.

Installing MongoDB

Before you can dive into using MongoDB, you need to install it. MongoDB is typically not installed by default on most Linux distributions, so you'll need to add the MongoDB repository and install it via the package manager.

For Ubuntu

  1. Import the MongoDB public key:

    curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
    
  2. Create a list file for MongoDB:

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
    
  3. Update your packages list:

    sudo apt-get update
    
  4. Install MongoDB packages:

    sudo apt-get install -y mongodb-org
    

Starting and Stopping MongoDB

Once installed, you can start the MongoDB service using systemctl.

  • Start MongoDB:

    sudo systemctl start mongod
    
  • Stop MongoDB:

    sudo systemctl stop mongod
    
  • Enable MongoDB to start on boot:

    sudo systemctl enable mongod
    

Configuring MongoDB

MongoDB's configuration file is located at /etc/mongod.conf. This file allows you to customize various settings such as the port MongoDB listens on, the data directory, and network interfaces.

Example Configuration

To configure MongoDB to only accept connections from the local machine, you can adjust the bindIp setting in /etc/mongod.conf:

net:
  port: 27017
  bindIp: 127.0.0.1

After making changes to the configuration file, remember to restart the MongoDB service:

sudo systemctl restart mongod

Common Issues and Troubleshooting

High Memory Usage

MongoDB can sometimes use a lot of memory. Monitor your system's memory usage using the top command. If MongoDB is consuming too much memory, consider adjusting the wiredTigerCacheSizeGB setting in your configuration file.

Network Failure

If MongoDB isn't accessible remotely, you might be dealing with a network failure. Check your firewall settings and ensure that MongoDB is configured to listen on an appropriate network interface.

Best Practices

Regular Backups

Always ensure you have regular backups of your MongoDB data. You can use mongodump and mongorestore for this purpose.

  • Backup:

    mongodump --out /path/to/backup
    
  • Restore:

    mongorestore /path/to/backup
    

Monitoring

Use monitoring tools like mongostat and mongotop to keep an eye on the performance and health of your MongoDB server.

  • Monitor real-time stats:

    mongostat
    
  • Monitor real-time read and write activity:

    mongotop
    

By following these best practices and understanding how to set up and manage MongoDB, you'll be well on your way to leveraging this powerful database for your applications.

The text above is licensed under CC BY-SA 4.0 CC BY SA