MongoDB Installation Tutorial – Windows | Linux | Mac OSX from Coding compiler. Here you will learn how to install MongoDB on Windows, Linux and Mac OSX machines. Learn Now.!
MongoDB Installation on the Windows Platform
Let’s start learning about how to install MongoDB on Windows machines.
MongoDB download
MongoDB provides pre-compiled binary packages for 32-bit and 64-bit systems. You can download and install them from MongoDB’s official website. MongoDB pre-compiled binary package download address: https://www.mongodb.com/download-center#community
Note: Windows XP systems are no longer supported after MongoDB 2.2. The latest version also has no installation files for 32-bit systems.
- MongoDB for Windows 64-bit is suitable for 64-bit Windows Server 2008 R2, Windows 7, and the latest version of Window.
- MongoDB for Windows 32-bit is suitable for 32-bit Windows systems and the latest Windows Vista. MongoDB’s database on a 32-bit system is up to 2GB.
- MongoDB for Windows 64-bit Legacy is suitable for 64-bit Windows Vista, Windows Server 2003, and Windows Server 2008.
Download the 32-bit or 64-bit .msi file according to your system. After downloading, double-click the file and follow the instructions to install it.
During the installation process, you can set up your installation directory by clicking the “Custom” button.
Create a data directory
MongoDB stores the data directory in the db directory. But this data directory will not be created actively, we need to create it after the installation is complete. Please note that the data directory should be placed in the root directory (eg C:\ or D:\, etc.).
In this tutorial, we have installed MongoDB on the C drive. Now let’s create a directory for data and create a db directory in the data directory.
c : \> cd c : \ c:\>mkdir data c:\>cd data c:\data>mkdir db c:\data>cd db c:\data\db>
You can also create these directories from the window’s explorer without having to go through the command line.
Run the MongoDB server from the command line
In order to run the MongoDB server from a command prompt, you must execute the mongod.exe file from the bin directory of the MongoDB directory.
C:\mongodb\bin\mongod --dbpath c:\data\db
If the execution is successful, the following information will be output:
2015-09-25T15:54:09.212+0800 I CONTROL Hotfix KB2731284 or later update is not installed, will zero-out data files 2015-09-25T15:54:09.229+0800 I JOURNAL [initandlisten] journal dir=c:\data\db\j ournal 2015-09-25T15:54:09.237+0800 I JOURNAL [initandlisten] recover : no journal fil es present, no recovery needed 2015-09-25T15:54:09.290+0800 I JOURNAL [durability] Durability thread started 2015-09-25T15:54:09.294+0800 I CONTROL [initandlisten] MongoDB starting : pid=2 488 port=27017 dbpath=c:\data\db 64-bit host=WIN-1VONBJOCE88 2015-09-25T15:54:09.296+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/W indows Server 2008 R2 2015-09-25T15:54:09.298+0800 I CONTROL [initandlisten] db version v3.0.6 ……
Connect to MongoDB
We can connect to MongoDB by running the mongo.exe command in the command window and execute the following command:
C:\mongodb\bin\mongo.exe
Configuring the MongoDB service
Open the command line window in administrator mode
Create a directory and execute the following statement to create a directory for the database and log files
mkdir c:\data\db mkdir c:\data\log
Create a configuration file
Create a configuration file. The file must have the systemLog.path parameter set, including some additional configuration options.
For example, create a configuration file located at C:\mongodb\mongod.cfg, which specifies systemLog.path and storage.dbPath. The specific configuration is as follows:
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
Install the MongoDB service
By executing mongod.exe, using the –install option to install the service, use the –config option to specify the previously created configuration file.
C:\mongodb\bin\mongod.exe --config "C:\mongodb\mongod.cfg" --install
To use an alternate dbpath, you can specify it in the configuration file (for example: C:\mongodb\mongod.cfg) or on the command line with the –dbpath option.
You can install mongod.exe or multiple instances of mongos.exe if needed. You only need to specify a different instance name by using –serviceName and –serviceDisplayName. This is only necessary if there is sufficient system resources and the design of the system needs to be done.
Start the MongoDB service
net start MongoDB
Close the MongoDB service
net stop MongoDB
Remove the MongoDB service
C:\mongodb\bin\mongod.exe --remove
MongoDB server running the command line and configure MongoDB service optional can be a way to start.
Just choose one operation.
MongoDB background management shell
If you need to enter MongoDB background management, you need to open the bin directory under the MongoDB directory and then execute the mongo.exe file, which is the interactive Javascript shell that MongoDB comes with for the operation and management interaction of MongoDB. Environment.
When you enter the MongoDB background, it will link to the test document (database) by default:
> mongo MongoDB shell version: 3.0.6 connecting to: test ……
Since it’s a JavaScript shell, you can run some simple arithmetic operations:
> 2 + 2 4 >
The db command is used to view the document (database) of the current operation:
> db test >
Insert some simple records and find it:
> db.dbase.insert({x:10}) WriteResult({ "nInserted" : 1 }) > db.runoob.find() { "_id" : ObjectId("5604ff74a274a611b0c990aa"), "x" : 10 } >
The first command inserts the number 10 into the x field of the dbase collection.
Related MongoDB Tutorials
MongoDB Installation on Linux Platform
MongoDB provides a 64-bit installation package for each Linux distribution. You can download the installation package on the official website.
Download address: https://www.mongodb.com/download-center#community
Download the installation package and extract tgz (the following demonstrates the installation on 64-bit Linux).
curl - O HTTPS : //fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz Download # tar - zxvf MongoDB - Linux - x86_64 - 3.0 . 6.tgz # decompression Mv mongodb - linux - x86_64 - 3.0 . 6 / / usr / local / mongodb # Copy the unpacked package to the specified directory
The MongoDB executable is located in the bin directory, so you can add it to your PATH path:
export PATH=<mongodb-install-directory>/bin:$PATH
<mongodb-install-directory> is the installation path for your MongoDB. As in this article /usr/local/mongodb .
Create a database directory
MongoDB’s data is stored in the db directory of the data directory, but this directory is not automatically created during the installation process, so you need to manually create the data directory and create a db directory in the data directory.
In the following example, we create the data directory in the root directory (/).
Note: /data/db is the default startup database path (–dbpath) for MongoDB.
mkdir -p /data/db
Run the MongoDB service from the command line
You can start the merged service by executing the mongodb command in the bin directory of the mongo installation directory on the command line.
Note: If your database directory is not /data/db, you can specify it with –dbpath.
$ ./mongod 2015-09-25T16:39:50.549+0800 I JOURNAL [initandlisten] journal dir=/data/db/journal 2015-09-25T16:39:50.550+0800 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed 2015-09-25T16:39:50.869+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 3.16 2015-09-25T16:39:51.206+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 3.52 2015-09-25T16:39:52.775+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 7.7
MongoDB background management shell
If you need to enter MongoDB background management, you need to open the bin directory under the mongodb directory and then execute the mongo command file.
The MongoDB Shell is an interactive Javascript shell that comes with MongoDB, an interactive environment for manipulating and managing MongoDB.
When you enter the mongoDB background, it will link to the test document (database) by default:
$ cd /usr/local/mongodb/bin $ ./mongo MongoDB shell version: 3.0.6 connecting to: test Welcome to the MongoDB shell. ……
Since it’s a JavaScript shell, you can run some simple arithmetic operations:
> 2 + 2 4 > 3 + 6 9
Now let’s insert some simple data and retrieve the inserted data:
> db.dbase.insert({x:10}) WriteResult({ "nInserted" : 1 }) > db.dbase.find() { "_id" : ObjectId("5604ff74a274a611b0c990aa"), "x" : 10 } >
The first command inserts the number 10 into the x field of the dbase collection.
MongoDB web user interface
MongoDB provides a simple HTTP user interface. If you want to enable this feature, you need to specify the parameter –rest at startup.
Note : This feature is only available for MongoDB 3.2 and earlier versions.
$ ./mongod --dbpath=/data/db --rest
MongoDB’s web interface access port is 1000 more than the service’s port.
If your MongoDB runtime port uses the default 27017, you can access the web user interface at port number 28017, which is located at https://localhost:28017.
MongoDB Installation on Mac OSX Platform
MongoDB provides a 64-bit installation package on the OSX platform. You can download the installation package on the official website.
Download address: https://www.mongodb.com/download-center#community
Starting with the MongoDB 3.0 release, only systems with OS X 10.7 (Lion) and newer versions are supported.
Next, we use the curl command to download the installation:
# Enter /usr/local cd / usr / local # Download sudo curl - O https : //fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.2.tgz # 解压 sudo tar - zxvf mongodb - osx - x86_64 - 3.4 . 2.tgz # Renamed to mongodb directory sudo mv mongodb-osx-x86_64-3.4.2 mongodb
After the installation is complete, we can add MongoDB’s binary command file directory (installation directory /bin) to the PATH path:
export PATH=/usr/local/mongodb/bin:$PATH
Install using brew
In addition, you can also use the OSX brew to install MongoDB:
sudo brew install mongodb
If you want to install the support TLS/SSL command as follows:
sudo brew install mongodb --with-openssl
Install the latest development version:
sudo brew install mongodb --devel
Run MongoDB
First, we create a database storage directory /data/db:
sudo mkdir -p /data/db
Start mongodb, the default database directory is /data/db:
sudo mongod # If you did not create a global path PATH, you need to enter the following directory cd / usr / local / mongodb / bin sudo ./mongod
Then open a terminal and enter the following command:
$ cd /usr/local/mongodb/bin $ ./mongo MongoDB shell version v3.4.2 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.2 Welcome to the MongoDB shell. …… > 1 + 1 2 >
Note: If your database directory is not /data/db, you can specify it with –dbpath.