Learn MongoDB 4.x
上QQ阅读APP看书,第一时间看更新

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.

IMPORTANT: The version of MongoDB 4.x featured in this book is the MongoDB Community Edition running on Docker.

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

For more information on different Linux distributions, have a look at the following websites: Debian ( https://www.debian.org/), SUSE ( https://www.suse.com/), pacman ( https://www.archlinux.org/pacman/), Gentoo ( https://www.gentoo.org/), and Slackware ( http://www.slackware.com/).

MongoDB Linux installations all tend to follow the same general procedure, detailed as follows:

  1. Import the public key of the MongoDB package management server.
  2. Add a MongoDB-specific Uniform Resource Locator (URL) to the package management sources list (websites from which package files are downloaded).
  3. Update the package manager from the newly added sources.
  4. Use the package manager to install either all MongoDB packages (mongodb-org) or specific components (for example, mongodb-org-server).
For specifics on a given Linux distribution, see this page: https://docs.mongodb.com/manual/installation/#mongodb-community-edition-installation-tutorials.
Do not use the version of MongoDB that is available through precompiled binaries ordinarily available through your package management system. These versions are not the latest versions and can cause problems down the road. It's best to grab the package directly from https://www.mongodb.com/.

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).

To install MongoDB on a macOS, it is recommended you use the Homebrew package manager. Otherwise, the actual installation procedure closely follows that when installing on Linux. For more information on installing MongoDB on Windows, see this documentation reference: https://docs.mongodb.com/master/tutorial/install-mongodb-on-windows/#install-mongodb-community-edition-on-windows. For information on macOS installation, have a look here: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/#install-mongodb-community-edition-on-macos

Next, we look at the MongoDB configuration.