In this article, we will learn how to install and configure MongoDB on Windows and Linux

1. Windows environment

The blogger runs Windows 10 and plans to install MongoDB version 4.2.0

(1) Install MongoDB

  • When the download is complete, run it.msiInstall MongoDB file, there are a few things to note

Here we choose custom installation

Change the location of the installation file, here the blogger chooses to save it on disk D (it is recommended not to install it on disk C)

You can then change the location of the data and log files as well, which is not changed here

Next, select whether you want to install MongoDB Compass

MongoDB Compass is a graphical management tool that you can download from the official website if you need it

  • When the installation is complete, you should be able to see the directoryD: \ mongo \ Server \ 4.2The file structure of
Exe - installCompass.ps1 - libeay32.dll - mongo.exe - mongod. CFG - mongod mongod.pdb - mongodump.exe - mongoexport.exe - mongofiles.exe - mongoimport.exe - mongorestore.exe - mongos.exe - Mongos. PDB - mongostat. Exe - mongotop.exe - ssleay32.dll + data // Used to store data files + log // Used to store log files - license-community MPL-2 - README - THIRD-PARTY-NOTICES - THIRD-PARTY-NOTICES.gotoolsCopy the code

(2) Start the MongoDB service

Create a db directory in the data directory, open CMD in the bin directory, and run the following command to start the MongoDB service

>Mongod -- dbpath D: \ mongo \ Server \ \ data \ 4.2 db
Copy the code

Two additional commands to stop/start the MongoDB service (need to run CMD as administrator)

>Net stop MongoDB :: Stops the MongoDB service
>Net start MongoDB :: Starts the MongoDB service
Copy the code

(3) Connect to MongoDB

Open CMD in the bin directory and enter the following command to connect to MongoDB for some basic interactive operations

> mongo
Copy the code

How to specify a connected database using mongo command

> mongo mongodb://[username:password@]host[:port][/[database][?options]]
Copy the code
parameter instructions
mongodb Fixed format
username:password Specify the account and password
host Specifies the host address. The default is 127.0.0.1
port Specifies the port. The default is 27017
database Specify the database name, which defaults to test
options Connection option

For example, use a specific account and password to connect to a local database

> mongo mongodb://admin:123456@localhost/myDB
Copy the code

2. Linux environment

The blogger runs CentOS 7, and MongoDB 3.2.7 is planned

Notice Before running the following command, switch to user root (su can be used)

(1) Install MongoDB

  • Download the MongoDB package from the official source
>Wget HTTP: / / https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.7.tgz
Copy the code
  • Unpack the
>Tar - ZXVF mongo - Linux - x86_64-3.2.7. TGZ
Copy the code
  • Copy the decompressed file to a specified directory
>Cp -r mongo - Linux - x86_64-3.2.7 / root/mongo
Copy the code

(2) Add system environment variables

  • Create and open using the VI editor directly/etc/profile.d/mongo.shThe script file
> vi /etc/profile.d/mongo.sh
Copy the code
  • Write the following contents in the file, save the file and exit
export PATH=$PATH:/root/mongodb/bin
Copy the code
  • Finally, run the following command to make the configuration file take effect
> source /etc/profile
Copy the code

(3) Start the MongoDB service

  • Create data files and log files
> mkdir -p /data/mongodata
Copy the code
  • Create and open using the VI editor directly/etc/mongodb.confThe configuration file
> vi /etc/mongodb.conf
Copy the code
  • Write the following contents in the file, save the file and exit
dbpath = /data/mongodata
logpath = /data/mongolog/mongodb.log
port = 27017
fork = true
Copy the code
  • Start the MongoDB service using the configuration file
> mongod --config /etc/mongodb.conf
Copy the code

3. Install adminMongo

Recently on the Internet to see a good visualization tool, here is also recommended to everyone: adminMongo, installation method is also very simple

  • Cloning of warehouse
> git clone https://github.com/mrvautin/adminMongo
Copy the code
  • Entering the warehouse
> cd adminMongo
Copy the code
  • Install dependencies
> npm install
Copy the code
  • Start the application
> npm start
Copy the code
  • In this case, access the address http://127.0.0.1:1234 to open