Install MongoDB
MongoDB is a popular, open-source NoSQL database that stores data in flexible, JSON-like documents. This guide will walk you through the installation process for common operating systems and then introduce you to the fundamental commands of the MongoDB Shell Basics.
Mongod and mongo are the two primary binaries that form the foundation of MongoDB.
- mongod: Also known as the daemon or service, this is the main database server process. It uses a proprietary binary protocol to receive commands via a network socket. “Installing MongoDB,” means setting up mongod.
- mongo: The JavaScript-based client shell interacts with, administers, and manipulates MongoDB data via the command line. It is a powerful tool and a fully functional JavaScript interpreter.
Important Note on 32-bit vs. 64-bit Installations
Remember that 32-bit MongoDB only supports databases under 2GB. MongoDB uses memory-mapped files internally. Since 32-bit versions are only suited for testing and evaluation, they should not be used in production. Since 64-bit builds and operating systems have virtually no storage constraint, they are recommended for production deployments. Starting with v3.0, MongoDB no longer supports 32-bit binaries for commercial use, and since 3.4, it no longer supports 32-bit x86 systems.
Installing MongoDB on Your Operating System
Visit the official MongoDB website to download the newest stable release before installation. MongoDB precompiles Windows, Linux, macOS, and Solaris binaries.
General Prerequisites:
- A data folder stores MongoDB files. Unix systems use /data/db and Windows C:\data\db as the default location.
- Ensure the mongod user account has read and write permissions and create this directory manually.
Installation on Windows
Use these procedures to install MongoDB on Windows:
- Download and Extract: From the MongoDB downloads page, download the relevant 64-bit Windows.zip or.msi file. Extract the data to a specified directory, like C:\mongodb.
- Create Data Directory: Open the Command Prompt and create the default data directory, C:\data\db.
- Go there first if you are extracting MongoDB to a different location. Optional data directories must be specified while launching mongod.exe.
- Start mongod.exe: Mongod.exe is located in the bin directory of your MongoDB installation (e.g., C:\mongodb\bin).
- The main MongoDB database process is launched by this command. An “waiting for connections” notice should appear, signifying that the procedure was successful. MongoDB is also available for installation as a Windows service.
Installation on Linux (Ubuntu/Debian Example)
You can manually install from binaries or utilise package managers for Linux. Because package managers have scripts for starting and terminating MongoDB, they are frequently chosen for server installations.
- Import GPG Key: Bring in the MongoDB public GPG key.
- Create MongoDB List File: Create MongoDB repository file /etc/apt/sources.list.d/mongodb.list. Ubuntu/Debian versions may vary the command.
- Update Package List: Make changes to the package database of your system.
- Install MongoDB: mongodb-org. You can choose a version or install the newest stable.
- Create Data Directory: Create and secure the default data directory /data/db.
- Start MongoDB: Get MongoDB started. Use the service command to launch the Mongod process.
- Use sudo service mongodb stop to end the process and sudo service mongodb restart to resume.
- Connect with mongo: Use the mongo shell to connect to a new terminal when mongod has finished operating.
Installation on macOS
macOS users can install manually or with Homebrew.
- Homebrew (Recommended): Homebrew is the easiest method if you have it.
- Manual Installation:
- Download and Extract: Visit the MongoDB website to download the macOS.tgz file. Use tar to extract it.
- Create Data Directory: Make the default /data/db directory and configure its permissions.
- Start mongod: Open the extracted folder’s bin directory and execute mongod.
- As default, port 27017 is used.
- Connect with mongo: In a new terminal window, navigate to the bin directory and run mongo to connect to the server.
Starting and Connecting with the Shell
Following installation, starting the server and connecting is essentially the same procedure on all platforms:
- Start mongod: Begin mongod’s Run the mongod binary from its bin directory or, if it’s in the PATH on your machine, from there.
- An “waiting for connections” message will appear as a result. Use –dbpath to indicate the non-default data directory if you have used it.
- Connect with mongo: In a new terminal window, run the mongo binary.
- It connects to a mongod instance on localhost:27017 and selects the test database by default. The output should show connecting to: test.
Basic Shell Commands
You can interact with your database using a number of commands after you’re connected to the Mongo Shell:
- Show every database that is available.
- The server’s databases are all listed by this command. At first, you may see local and test. In the absence of a database, you may need to add at least one document to it in order for it to show.
- Choose or build a database.
- By using this command, the current database is changed to mydb. The instant you insert your first document into MongoDB, it will immediately build the database mydb if it doesn’t already exist.
- Display every collection in the database as it stands right now.
- All of the collections in the database that is now selected are listed by this command.
- To display a new database, insert a document: Assume you chose to utilise mydb. Put a document into a collection in mydb so that it appears in show dbs.
- Now, mydb ought to show up in the list if you run show dbs once more.
- For assistance and statistics, contact:
- db.help(): A list of methods that are accessible on the current database object is displayed by the db.help() function.
- db.stats(): Displays the database’s name, collections, and documents.
Following these instructions will install and enable MongoDB on your operating system.