Install MongoDB on Windows: SQL in Sixty Seconds #091

To install MongoDB on Windows, you need two downloads: the server and the shell. The server installer does not include the shell. This post walks through the setup for MongoDB 9, then proves it with real output.

Gouache painting: a freshly assembled wooden filing cabinet with one drawer pulled open, holding a single paper folder with a vermilion tab, packing straw on the floor beside it

What You Install

Four pieces exist when you install MongoDB on Windows, and only two are required. The server, called mongod, is the database itself. The shell, called mongosh, is the command line where you type queries. Compass is an optional visual tool, and the installer offers it as a checkbox. The Database Tools, such as mongodump, are a separate optional package.

If you come from SQL Server, the pieces map neatly. The match is loose, though. A document is not a row, because documents in one collection can have different fields.

In MongoDBClosest SQL Server match
mongod, the serverThe SQL Server service
mongosh, the shellsqlcmd
CompassSQL Server Management Studio
CollectionTable
DocumentRow

Here is my sixty second video on the install.

The video comes from 2020, when the server package still carried the old mongo shell. That shell is gone, so today you install mongosh yourself. The steps below are for MongoDB 9, and my test computer runs version 9.0.2.

Install the Server

Download the Community Server for Windows from the MongoDB download page. Choose the MSI package for 64-bit Windows. Before you install MongoDB on Windows, check the file. The download page publishes a SHA-256 sum for each package, and Get-FileHash prints the sum of your copy. The two must match.

Get-FileHash .\mongodb-windows-x86_64-9.0.2.zip -Algorithm SHA256

Both zip files on my test computer matched their published sums. For the MSI, type its own file name. Then follow these steps.

  1. Run the MSI file and accept the license.
  2. Choose the Complete setup type.
  3. On the service screen, keep Install MongoDB as a Service and Run service as Network Service user.
  4. Keep the default data and log folders. They sit under C:\Program Files\MongoDB\Server\9.0.
  5. Leave the Compass checkbox on if you want the visual tool, or turn it off.
  6. Click Install, then Finish.

Network Service is a built-in Windows account with limited rights. A database service does not need more than that. The service is named MongoDB, and it starts by itself. To check it, open PowerShell and run the line below. The status should read Running.

Get-Service MongoDB

What the Installer Leaves Behind

The setup creates three things you will meet again. The data folder holds the database files. The log folder holds the server log, which is the first place to look when the service refuses to start. A configuration file named mongod.cfg sits in the bin folder, and it holds the port and the bind address.

If you edit that file, restart the MongoDB service so the change takes effect. Compass and any application connect with the connection string mongodb://localhost:27017. That string works until you change the port.

Install the Shell

Download mongosh from the MongoDB Shell page. It comes as an MSI or as a zip file. The zip is small: the 2.13.0 package is 60 MB, while the 9.0.2 server zip is 942 MB. Install it, then open a new terminal window, because an old window does not see the new PATH.

My test computer has no installer or service. I started the same server from the official zip file by hand, on a custom local port. The commands below use the default port, and every output comes from my run. I ran the script twice and got identical results.

Prove It Works

Type mongosh with no arguments. It connects to the server on the local computer, port 27017. Then run these commands.

mongosh
use installCheck
db.version()
// 9.0.2
version()
// 2.13.0
db.runCommand({ ping: 1 })
// { ok: 1 }
db.serverStatus().storageEngine.name
// wiredTiger

The first two checks show the server version and the shell version. They are separate numbers, so look at both. The ping returns ok: 1 when the server answers. The storage engine is the part that writes data to disk, and wiredTiger is the default one.

A version number is not the whole proof. Write one document and read it back. The word acknowledged means the server accepted the write, and insertedId echoes the _id you chose.

db.visits.insertOne({ _id: 1, page: 'home', ok: true })
// { acknowledged: true, insertedId: 1 }
db.visits.find()
// [ { _id: 1, page: 'home', ok: true } ]
db.visits.countDocuments({})
// 1

The use command does not create a database. MongoDB creates databases and collections on the first write, and the drop at the end removes this one again.

Card titled Install MongoDB on Windows: Download: two packages, the server and the shell; Server: MSI, Complete setup, run as Network Service; Service: named MongoDB, status should read Running; Connect: mongodb://localhost:27017; Prove: ping returns ok: 1, then read a document back. Tip: Open a new terminal after installing the shell.

Or Start It by Hand From the Zip

The server also comes as a zip file. It needs no installer, no administrator rights and no service. Unpack it, create a data folder, and start mongod from the bin folder.

mkdir C:\mongo-data
mongod --dbpath C:\mongo-data --bind_ip localhost

The first option names the folder where MongoDB keeps its files. The second keeps the server reachable from this computer only. The server stops when you close the window. That makes this route a fit for a test or a classroom machine, not for daily work.

When the First Run Fails

Check three things when the first run fails. If Windows says mongosh is not recognized, open a new terminal, or call the shell by its full path. If mongosh says the connection was refused, the service is not running. Start it from an elevated PowerShell with Start-Service MongoDB. If mongod is not recognized, the server’s bin folder is not on your PATH. The MSI does not add it, and you only need mongod directly when you start the server by hand.

You could say a container or a cloud cluster avoids the installer. Fair point. For a quick test, either one works. A local install runs offline, which helps on a plane or behind a locked down network.

A Short Checklist

After you install MongoDB on Windows, run three checks. Read the server version, send a ping, and write a document you can read back. The default setup listens on the local computer only and has no users. Keep it that way until you create an administrator account. A database that accepts connections from your network without a password is a mistake that is easy to avoid.

When you finish testing, remove the example database.

use installCheck
db.dropDatabase()
// { ok: 1, dropped: 'installCheck' }

A MongoDB install is not finished when the wizard closes, it is finished when a document comes back.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

MongoDB, NoSQL, SQL in Sixty Seconds, Windows
Previous Post
SQL in Sixty Seconds – Need Your Feedback
Next Post
How to Install SQL Server 2019? – Interview Question of the Week #287 – SQL in Sixty Seconds #092

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.