preface

MongoDB is a database based on distributed file storage, written by C++ language, aiming to provide scalable high-performance data storage solutions for WEB applications.

MongoDB stores data as a document, and the data structure is composed of key=>value pairs. MongoDB documents are similar to JSON objects, and the field values can contain other documents, arrays and document arrays.

The MongoDB server runs on Linux, Windows, or MAC OS X platforms and supports 32-bit and 64-bit applications. The default port number is 27017.

MongoDB supports a variety of programming languages: Python, Java, C++, PHP, C# and more.

Previously, we introduced the difference between Nosql and relational database. As an important part of Nosql, MongoDB must be installed in order to use it. MongoDB is cross-platform and can be installed on either Windows or Linux. Here we will focus on MongoDB installation on Linux.

Installation environment

  • Install it in Windows
  • Install it in Linux

Installation steps

  • Download the compressed version from the Mongodb website
  • Unzip it to a directory
  • Then create a new data empty folder
  • Create a new log file
  • Go to the bin directory and copy the path
  • System environment variables

The installation

1. Directing a download

In the mongo’s firstwebsiteDownload a stable version of the server in the case of network, clickDirect downloadIf the server is not connected to the Internet, download it to the client in advance.

2. Decompress the MongoDB installation package

There are two formats for downloading: MSI and ZIP. If you want to download zip, use the decompression tool to decompress it before using it.

If you are downloading MSI, click and double click to install it, as shown below:

configuration

1. Configure environment variables

  1. Computer right click on properties
  2. Find advanced system Settings in properties
  3. Find the environment variable option
  4. Find the path to change the environment variable (note that there must be a semicolon in front of the path, according to their own path selection, mainly to find the parent directory where the EXE file is located)
  5. inSystem variable (S)Add variable name (N) toMONGODB_HOMEA variable’s value (V)D: \ \ Program Files \ mongo \ Server \ 4.4
  6. Find path to change environment variables%MONGODB_HOME%\bin

2. Check whether the check is successful

Type Mongod –help. If the following screen appears, the environment variable has been configured.

3. Create a directory for storing data

Create two folders in the root directory of the project after decompression or installation (the same as bin) and name them data(storing data) and log(storing logs) respectively. Create the mongodb. Log file in the log folder to record logs.

Start the

1. Start the mongo

Run the following command in the bin directory of the mongodb installation directory:

$ cd %MONGODB_HOME%/bin
$ mongod.exe --port 27017 --dbpath =%MONGOBD_HOME%\data  --logpath=%MONGODB_HOME%\log\mongodb.log
Copy the code

2. Check whether MongoDB is started

$ mongod.exe
Copy the code

3. Go to the MongoDB database console

$ mongo

4. Access the admin database

$ use admin
Copy the code

The service start

The above startup method must ensure that Mongod. Exe is not closed, otherwise the client will not be able to connect to the database. In order to avoid manually starting the database every time, mongDB can be installed as the Windows service and the service can be started with the startup of Windows. In this way, we can directly connect to the database when using mongoDB, saving the tedious manually starting the service. Commands to install and enable mongoDB as a Windows service:

$mongod -- dbPath =D:\Program Files\MongoDB\Server\4.4\data -- logPath =D:\Program Files \ mongo \ Server \ \ log \ mongo. 4.4 the log - install - serviceName "mongo"Copy the code

1. Enable the service

Open CMD and type

1) Start MongoDB:net start MongoDB

Net stop MongoDB

3) Delete MongoDB: sc delete MongoDB

2. Start the service on the service page

Enter services. MSC in CMD to find the MongoDB service and right-click to start/stop it

3. Verify that the service is started

Enter http://127.0.0.1:27017/ or http://localhost:27017/ in the address bar to display It looks like you are trying to access MongoDB over HTTP on the native driver port. The installation is successful.