This article mainly introduces the installation method of the latest version 6.x of MongoDB under Windows and Linux operating systems, which is slightly different from the previous version 4.x and 5.x, for your reference.
Windows installation
Enter the official website to download the Mongodb installation package, click here to jump , the website will automatically detect the current operating system and provide the latest version, and the installation package suitable for this platform:
In addition to .msi
the installation package in the format, you can also choose .zip
the compressed package:
msi install
Double-click to open the downloaded installation package, and keep clicking Next:
Tick to agree:
Choose a full install:
Keep the default. This step is used to set the name of the MongoDB service in the windows system, as well as the storage directory of data and logs:
Remove the default option, it will install a client tool for operating the MongoDB database, it is recommended to install another Robo 3T or other you are used to:
Then click Next to proceed with the installation. The installation process takes about three to five minutes.
Successful installation:
View the status of the MongoDB service
Press Ctrl + Shfit + Esc
to open the task manager, switch to the service tab, and pull down to find the MongoDB service.
Here you can observe the status of MongoDB, which is automatically started by default, that is, it starts automatically when it is turned on.
Or open a browser to visit https://localhost:27017
:
MongoDB directory
Go to the MongoDB installation directory, which is the default C:/Program Files/MongoDB/Server/6.0
, or another directory of your choice.
Among them, bin is the directory of the executable program provided by MongoDB, data is the directory of data storage, and log is the directory of log storage. Both of the latter have required manual creation by developers in the past. Now the installer will be created automatically.
Look at the executable program in the bin directory, mongod.exe
which is used to start the MongoDB service and mongos.exe
manage the fragmented cluster.
Before MongDB 6, there will be many executable programs in this directory, such as the most commonly used one mongo.exe
, which is used to connect to the MongoDB service and is a client tool in the shell environment. But now it needs to be installed separately.
Install MongoDB Shell
Click to jump to download the compressed package:
Just keep clicking Next to install, the installation directory can be default or customized:
Access MongoDB service
In the bin directory, open the command line tool,
After the installation is complete, enter the installation directory, open the command line tool, and execute it mongosh.exe
. By default, it will connect mongodb://localhost:27017
to the MongoDB service:
This is a shell environment that supports JavaScript syntax and some extended syntax for operating databases, such as viewing all databases:
show dbs
set environment variables
In order to facilitate the use of commands and MongoDB Shell provided by MongoDB in various paths without switching to bin
the directory, the directory can be bin
added to the environment variable.
Press Win + S to open the search, enter "environment variables":
Double-click to open the Path variable:
Add the bin directory to save it:
mongosh
After that, you can use the , etc. commands in any path mongod
.
Linux installation
Install MongoDB
This article takes CentOS 8 as an example. For other Linux versions, you can click here to refer to the official documentation for installation.
You can download the installation package from the official website and upload it to the server or virtual machine:
Or download directly from the server through the download tool:
# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-6.0.2.tgz
View the downloaded installation package:
# ls
mongodb-linux-x86_64-rhel80-6.0.2.tgz
Unzip and rename the directory to "mongodb":
# tar -zxf mongodb-linux-x86_64-rhel80-6.0.2.tgz
# ls
mongodb-linux-x86_64-rhel80-6.0.2 mongodb-linux-x86_64-rhel80-6.0.2.tgz
# mv mongodb-linux-x86_64-rhel80-6.0.2 mongodb
Enter the installation package directory:
# cd mongodb
# ll
total 140
drwxr-xr-x 2 root root 4096 Nov 8 22:50 bin
-rw-r--r-- 1 root root 30608 Sep 29 09:07 LICENSE-Community.txt
-rw-r--r-- 1 root root 16726 Sep 29 09:07 MPL-2
-rw-r--r-- 1 root root 1977 Sep 29 09:07 README
-rw-r--r-- 1 root root 77913 Sep 29 09:07 THIRD-PARTY-NOTICES
Among them, bin
the directory stores the commands provided by the database:
install mongodb-shell
Download the installation package:
# wget https://downloads.mongodb.com/compass/mongosh-1.6.0-linux-x64.tgz
Unzip:
# tar -zfx mongosh-1.6.0-linux-x64.tgz
Rename the extracted directory:
# mv mongosh-1.6.0-linux-x64.tgz mongosh
mongosh/bin
The command is provided under the directory to mongosh
connect to the MongoDB database service:
Add the bin directory to Path
At present, mongod
the path of the directory where the command is located is ~/mongodb/bin
, and mongosh
the path of the directory where the command is located is ~/mongosh/bin
, these two paths need to be added to the Path environment variable.
Edit the file with vim:
# vim ~/.bash_profile
Path
Append these two directories at the end of the line, then save and exit:
Then execute source
the command to reload the environment variables to make the configuration just take effect:
# source ~/.bash_profile
Start the database service
The MongoDB service needs to specify the directory where the data is stored when it starts . If not specified, /data/db/
the directory will be searched by default.
The above data directory and log directory are specified when the Windows system is installed using the msi installation package. If a zip archive is used, the default data storage directory is \data\db\
the directory of the current disk volume.
If the data directory does not exist or is not writable, then the server side will not start . Therefore, before starting the MongoDB service, make sure that the data directory exists and has write permissions to the directory.
Create two directories first:
# mkdir -p /data/mongodb/data
# mkdir -p /data/mongodb/log
Start the database service with mongod
the command:
# mongod --fork --dbpath=/data/mongodb/data --logpath=/data/mongodb/log/mongo.log
- –fork: On Unix-based systems, use fork to create a server process that runs the MongoDB service as a daemon. If --fork is specified, --logpath must also be specified.
- –dbpath: Specifies the directory where MongoDB data is stored, which must be specified when starting the service.
- –logpath: The default log is printed on the command line, use this option to specify the file for log output. If you have write permission to this directory and the file does not exist, it will be created automatically. If the log file already exists, it will be overwritten by default and all old logs will be deleted. If you want to keep old logs, you should use the --logappend option in addition to --logpath.
Connect to MongoDB service
Use mongosh
the command to connect to the service:
# mongsh
Connects to by default mongodb:localhost:27017
.
It has been connected to the MongoDB service normally, and then the database can be operated in the shell.
Summarize
This article introduces in detail how to install the latest version of MongoDB database under Windows and Linux. Compared with 5.x, the installation change of the new version of MongoDB is that the previous mongo
commands are discarded, and users need to install MongoDB Shell separately to operate the database. If you do not need to use the shell, you can omit this step.