Installing MongoDB 4.x
Now that you have an idea what the requirements are in terms of the OS, filesystem, and server hardware, and understand the most important differences between MongoDB 3 and MongoDB 4.x installation, it's time to have a look at the actual installation process. In this section, we cover a general overview of MongoDB installation that can be applied either at a customer site or inside a demo environment running in a Docker container on your own computer.
For those of you who have experience in MongoDB 3, in the next section, we briefly discuss major differences in installation between version 3 and 4.x.
MongoDB 3 and MongoDB 4.x installation differences
As mentioned in the first chapter, one of the main differences between MongoDB 3.x and MongoDB 4.x is that the WiredTiger storage engine is now the default. Accordingly, you will need to ensure that you take advantage of WiredTiger's multithreaded capabilities by adjusting the RAM and CPU usage. There is an excellent guideline given here: https://docs.mongodb.com/manual/administration/production-notes/#allocate-sufficient-ram-and-cpu.
One of the main differences between installing MongoDB 3.x compared to MongoDB 4.x is the list of operating systems supported. When considering upgrading a server currently running MongoDB 3.x, consult the supported platforms guide (https://docs.mongodb.com/manual/installation/#supported-platforms) in the MongoDB documentation. Generally speaking, each new version of MongoDB removes support for older OS versions and adds support for new versions. Also, as a general consideration, where versions of MongoDB below v3.4 supported 32-bit x86 platforms, in MongoDB 3.4 and later—including MongoDB 4.x—there is no support for 32-bit x86 platforms.
Another difference you will observe is that as of MongoDB 4.4, time zone data is now incorporated into the installation program, as seen in this screenshot:
The next section shows you how to install MongoDB 4.x on a Linux-based platform.
Installing MongoDB 4.x on Linux
It's a fact that there are literally hundreds of Linux distributions available. Most are free, but there are others for which you must pay. Many of the free Linux vendors also offer enterprise Linux versions that are stable, tested, and offer support. Obviously, for the purposes of this book, it would be impossible to detail the MongoDB installation process on every single distribution. Instead, we will concentrate on two main families of Linux: the RPM- (formerly RedHat Package Manager: https://rpm.org/) based and the Debian-based distributions.
Of the RPM-based distributions, we find RedHat, Fedora, and CentOS, among others, which are extremely popular. On the Debian side, we have Kali Linux and Ubuntu (and its offshoots!). There are many other distributions—and even other major families—that are not covered in this book. Among the more well-known distributions not covered are SUSE, pacman, Gentoo, and Slackware.
MongoDB Linux installations all tend to follow the same general procedure, detailed as follows:
- Import the public key of the MongoDB package management server.
- Add a MongoDB-specific Uniform Resource Locator (URL) to the package management sources list (websites from which package files are downloaded).
- Update the package manager from the newly added sources.
- Use the package manager to install either all MongoDB packages (mongodb-org) or specific components (for example, mongodb-org-server).
Here is a screenshot of the start of the main installation on an Ubuntu 20.04 server:
Once the main installation has completed, you will note that the installation script creates a user and group named mongodb. Here is a screenshot of the end of the main installation process:
The script also creates a /etc/mongod.conf default configuration file, covered later in this chapter. To start the mongod instance using this configuration file, use this command:
mongod -f /etc/mongod.conf &
To access the database, issue the mongo command, which starts a shell, giving direct access to the database, as shown here:
Let's now look at the installation of MongoDB on Windows and macOS.
Installing on Windows and macOS
Installing MongoDB directly on a Windows server is actually easier than with Linux, as all you need to do is to go to the downloads website for MongoDB and choose the Windows installer script matching your Windows version. To start MongoDB, you would go to the Windows Task Manager, locate MongoDB, and choose start. You can also configure MongoDB to start automatically as a service. One final consideration is that the MongoDB Windows installation also automatically installs MongoDB Compass (discussed in more detail in Chapter 9, Handling Complex Queries in MongoDB).
Next, we look at the MongoDB configuration.