[MongoDB series] 1. MongoDB 6.x installation tutorial under Windows and Linux (detailed)

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:

image-20221108200604652

In addition to .msithe installation package in the format, you can also choose .zipthe compressed package:

image-20221108200656068

msi install

Double-click to open the downloaded installation package, and keep clicking Next:

2022-11-08_200945

Tick ​​to agree:

image-20221108204242546

Choose a full install:

image-20221108204326047

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:

image-20221108204443526

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:

image-20221108204558874

Then click Next to proceed with the installation. The installation process takes about three to five minutes.

Successful installation:

image-20221108204758533

View the status of the MongoDB service

Press Ctrl + Shfit + Escto 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.

2022-11-08_203851

Or open a browser to visit https://localhost:27017:

2022-11-08_221733

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.

2022-11-08_203728

Look at the executable program in the bin directory, mongod.exewhich is used to start the MongoDB service and mongos.exemanage the fragmented cluster.

2022-11-08_203803

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:

image-20221108213028204

Just keep clicking Next to install, the installation directory can be default or customized:

2022-11-08_213239

2022-11-08_213815

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:27017to the MongoDB service:

image-20221108220829813

This is a shell environment that supports JavaScript syntax and some extended syntax for operating databases, such as viewing all databases:

show dbs

2022-11-08_213937

set environment variables

In order to facilitate the use of commands and MongoDB Shell provided by MongoDB in various paths without switching to binthe directory, the directory can be binadded to the environment variable.

Press Win + S to open the search, enter "environment variables":

image-20221108205823710

image-20221108205905029

Double-click to open the Path variable:

image-20221108205953241

Add the bin directory to save it:

image-20221108221536878

mongoshAfter 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:

image-20221108234316369

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

image-20221108234803120

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, binthe directory stores the commands provided by the database:

image-20221109094325995

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/binThe command is provided under the directory to mongoshconnect to the MongoDB database service:

image-20221109000959627

Add the bin directory to Path

At present, mongodthe path of the directory where the command is located is ~/mongodb/bin, and mongoshthe 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 

PathAppend these two directories at the end of the line, then save and exit:

image-20221109001130375

Then execute sourcethe 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 mongodthe 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.

image-20221109001827550

Connect to MongoDB service

Use mongoshthe command to connect to the service:

# mongsh

Connects to by default mongodb:localhost:27017.

image-20221109002051449

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 mongocommands 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.

Guess you like

Origin blog.csdn.net/Old_Soldier/article/details/132523336