How to install MongoDB server and shell on a MAC

Sérgio Ildefonso
7 min readDec 26, 2021

This article explains how to install MongoDB server (Community Server) and the command interpreter shell, on a MAC.

What is MongoDB?

MongoDB is a database technology which uses a specific way of storing and organizing data. All of the information is saved on a JSON format placeholder called Document that belongs to a hierarchical aggregator called Collection. The data model isn’t relational (per se) and the internal storage of the information is BSON (Binary JSON) which is very useful in storing and searching big amounts of information.

Why MongoDB?

Because MongoDB is more flexible. We don’t need to define schemas to organize our data. We can aggregate data in the data we save on the database. We use JSON format to represent data and we can incapsulate JSON inside JSON (and so on) instead of using JOIN commands as we use on relational models.

Installation, step-by-step

Step 1: Download the installer

To download the installer, we should go to www.mongodb.com.

On the Products menu we can see the all of the MongoDB available products. We must choose the Community Server to be redirected to the download page.

On the download page, we must choose the version (it is advisable to choose the latest one), the target platform (in this case, the macOS) and the package format (in this case, a zipped tgz file). To start the download, we just need to press the Download button.

After the download, we will get a mongodb-macos-x86_x64-x.y.z.tgz file where the x.y.z is the version we chose. We must copy this file to a folder we want to keep mongoDB on our system. Then, in Finder, by right-click the icon and choose Open (in portuguese, “Abrir”).

After the extract, we should see some information files and a bin folder.

At this time, we should open a Terminal window and jump into the bin folder.

Then, we must copy all of the files inside the bin folder to the /usr/local/bin folder, so we can run them from every place in the system. To do so, we must run the following command:

sudo cp ./* /usr/local/bin/

This command will copy 4 files:

  • install_compass : the GUI client installer
  • mongo : the client
  • mongod : the daemon
  • mongos : the shard utility

At this time, the MongoDB binaries are installed on our system.

Step 2: Run MongoDB client

To start the MongoDB client, we must run the mongo command in the Terminal. Usually, on a first run, we get a security message saying that it isn’t possible to confirm the developer of the command.

What we will need to do, is to allow (in portuguese, “Permitir”) the run of the “mongo” command on the System preferences -> Security and Privacy.

We might be asked to confirm the opening of the file (in portuguese, “Abrir”) on the next time we run “mongo” command, but after that, the system will allow it.

As we see in the next window, the command was allowed to run. But we got a “connection refused” message.

This is because we were trying to run the client, bu we didn’t have a server running.

Step 3: Start the MongoDB server

First things, first. To start the MongoDB server we need first to define the path of the files (database, logs, etc.) that will support the server. So, hands-on and we need to create a folder structure. Here is an example:

As we can see, in this example, we have a MongoDB folder containing 2 subfolders (data and logs). An important note is that we should have read and write permissions to access these folders, so we won’t have security issues.

To start the MongoDB server, we should runt he “mongod” command. If we get the same security message saying that it isn’t possible to confirm the developer of the command, we should do the same steps we did for “mongo” command.

Once the security issue is overcome, we can start the MongoDB server. In order to use the folders we created, we need to add some arguments to the mongod command.

mongod --dbpath /(put the path to the data folder)/ --logpath /(put the path to the log folder)/mongo.log

If everything worked well, we can see a result similar to the following image.

Also, inside the logs folder, we will see a log file created.

And inside the data folder, we will see some files created also.

We should have the server online when needed.

To stop the server, we should press the Ctrl + C keys combination.

Step 4: Connect MongoDB client to MongoDB server

To connect MongoDB client to MongoDB server, the server must be running. So, keeping the “server” Terminal window opened and running, we should open a new Terminal window and run the “mongo” command.

This will be the result.

When we see the “>” character in the end of the displayed information, we know that the client is connected to the server and the server is waiting for the insertion of commands from the client.

Some useful MongoDB (simple) commands

Here are some useful commands:

show dbs

This command shows the existing databases on the server.

On a clean installation, we can see 3 databases:

  • admin
  • config
  • local

These databases contain various server settings, like configurations and users who are authenticated to connect, etc.

use database_name

This command “points” the execution of the commands to the specified database. If the specified database doesn’t exist, this command will create the specified database.

db.createCollection(“collectionName”, { options})

This command will create a collection in the specified database.

db.collection_name.insert({key:”value”, keyB:”valueB”, …})

This command will insert a document inside the specified collection, on the specified database.

db.collection_name.find({keyFilter:”value_filter”})

This command searches for documents in the specified collection that match the specified filter.

This is a starting point to work on MongoDB databases. There is an additional topic that isn’t needed but can be useful.

“New” MongoDB Shell setup

We are using the “mongo” command that is the traditional shell client. But there is a new one that is considered more useful. To install it, here ar the steps.

Step 1: Download the installer

To download the MongoDB Shell, we should go to the download page (https://www.mongodb.com/try/download/shell), choose the version, platform and package format, and then press the Download button.

Step 2: Extract and copy to bin folder

As we did for the MongoDB package files, we must copy the following files inside the bin folder to the /usr/local/bin/, so we can run them from every place in the system.

  • mongocryptd-mongosh
  • mongosh

This is the command to execute from the Terminal window:

sudo cp ./* /usr/local/bin/

Step 3: Execute the “New” MongoDB Shell

To execute the “New” MongoDB Shell we must run the “mongosh” command.

As we can see, the shell is connected to a “test” database. We can jump into other databases and run the same kind of commands as the “mongo” shell.

This shell is more pretty.

Note: The “mongo” command can still be used.

If you are new to MongoDB, I expect this quick read was was useful and brought some ideas for your initial steps.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response