How to Install MongoDB With Homebrew

Tahseen Tauseef Feb 02, 2024
  1. Install MongoDB Manually Without Homebrew
  2. Install MongoDB Using Homebrew
How to Install MongoDB With Homebrew

MongoDB is a well-known unstructured database management system that can handle large amounts of data. It’s a document-oriented database system, part of the NoSQL family (non-SQL).

The data and records are saved as documents that look and function like JSON objects. Documents are a collection of key-value pairs that comprise MongoDB’s fundamental data unit.

This database system was implemented in the mid-2000s.

MongoDB is required for developers who wish to work with organized, semi-structured, or unstructured data in their applications. MongoDB may also be used by those interested in big data analysis.

MongoDB is the most outstanding choice if an application’s data requires agility, scalability, and high performance.

It enables various applications, including real-time exploratory and predictive analytics and parallel data processing. Moreover, MongoDB can provide high-performance data storage even when dispersed over numerous servers.

There are two ways to install MongoDB in your MAC. One without Homebrew and the second with Homebrew.

Install MongoDB Manually Without Homebrew

  1. Let’s get MongoDB installed. To do so, open your browser and put google.com into the address bar.

  2. Type MongoDB into Google search, and ideally, the first link that comes up is the MongoDB link. You have two options for installing MongoDB from here.

    To install via the macOS terminal, follow these steps.

  3. Go to MongoDB-community for further information. Next, choose the version, platform, and package you want to use.

    After selecting macOS as the platform, click the download button and tgz as the file format.

  4. Once the tgz file has been downloaded, extract it using the macOS terminal.

  5. Your MongoDB will most likely be downloaded to the Downloads folder. To do so, open the MongoDB terminal and write this command.

    sudo mv mongodb-osx-ssl-x86_64-4.4.1 /usr/local/mongodb
    
  6. You have to move the MongoDB folder to your local binary storage.

    sudo mv mongodb-osx-ssl-x86_64-4.4.1 /usr/local/mongodb
    
  7. This will ask for your system password. Provide the password.

    You can change the directory to /usr/local/mongodb and see whether all the files exist or not using the ls command. Note that this step is optional.

    To change the directory, type the command below.

    cd /usr/local/mongodb
    
  8. After that, you must create the database folder. MongoDB defaults to writing or storing data in the data/db subdirectory.

    So this will be the command for it.

    sudo mkdir -p /data/db
    
  9. You may establish a directory structure using the -p parameter. For example, use the following command to see if this path and directory have been created.

    cd /data/db
    
  10. To check whether you are on the correct directory or not, type the command: pwd. To change the permission, you must first know your login.

    Type the command whoami to find out your username.

  11. Change the directory’s permissions now. The command to do so is:

    sudo chown <usr-name> /data/db
    

Install MongoDB Using Homebrew

If you wish to install MongoDB using Homebrew, follow these instructions manually.

  1. On macOS, Homebrew aids in installing and managing programs.

    brew update
    brew tap mongodb/brew
    
  2. Once the Homebrew package is installed, you can use brew to download MongoDB.

  3. In your macOS Terminal, type the following command.

    brew install mongodb-community@version-number
    
  4. The following binaries will be installed as part of this installation.

    4.1. The mongod server
    4.2. The mongo shell
    4.3. The mongos sharded cluster query router

  5. It will take a few seconds to complete the installation. After that, use the following command to establish a directory to store MongoDB data.

    sudo mkdir -p /data/db
    
  1. At this point, you must make sure that your data directory has the proper permissions. To do so, use the following command.

    sudo chown -R `id
    -un` /data/db
    
  2. This ensures that the data directory is ready and has the appropriate permissions. For example, the MongoDB installation will create the following files and folders at the following places.

    Apple M1 Intel Processors
    Log directory /opt/homebrew/var/log/mongodb /usr/local/var/log/mongodb
    Configuration file /opt/homebrew/etc/mongod.conf /usr/local/etc/mongod.conf
    Data directory /opt/homebrew/var/mongodb /usr/local/var/mongodb
  3. Now, you’ll use the MongoDB Community Edition. MongoDB may be launched on macOS using the brew command.

    However, MongoDB services on macOS require a manual operation. Use the following command to run the MongoDB daemon, known as mongod (process).

    brew services start mongodb-community
    

    This procedure will be performed as a macOS service by macOS.

  4. Use the following command to kill a mongod operating process as a macOS service.

    brew services stop mongodb-community
    
  5. To manually launch MongoDB in the background and listen for connections on a specific port, use the command:

    10.1. mongod -config /usr/local/etc/mongod.conf -fork for Macs with Intel CPUs.
    10.2. mongod -config /opt/homebrew/etc/mongod.conf –fork for Macs with Apple M1 CPUs.

  6. Finally, double-check your MongoDB version. Type the following command: mongo –version is a command that may be used to change the version.

    The command line will show you the MongoDB version installed on your Mac. When possible, developers advise utilizing the most recent version of libraries and applications.

    In addition, it will keep you safe from client-side application compatibility concerns.

  7. Type the command mongodb to display the installation list.

  8. Start MongoDB with the command mongod -config /usr/local/etc/mongod.conf.

  9. Type the following command to connect to the MongoDB service: mongo.

  10. To display all databases, use the show dbs command.

In this article, MongoDB and its uses are explained. Then installation of MongoDB is discussed in detail in Intel and MAC processors.

Finally, installation with Homebrew is given. Moreover, if you want to download it manually, the whole path is provided for this purpose too.

Related Article - MongoDB Installation