MongoDB documentation--basic installation-linux installation (mongodb environment construction)-docker installation (mounting data volumes)-and detailed version comparison

Adam:

        I learned some basic concepts of mongodb earlier. This section of the article explains and summarizes the installation of mongodb.

 Portal:

MongoDB Documentation--Architecture System_Yidancheng Blog-CSDN Blog

 MongoDB Documentation--Basic Concepts - A Single Cheng Blog - CSDN Blog

 MongoDB Documentation - Basic Use - Using MongoDB Basic Statements in the Client (dos Window)/Visualization Tool Use regular expressions to find in

MongoDB document-advanced use-spring-boot integration using MongoDB --- MongoRepository to complete additions, deletions, changes and queries_One Single Cheng Blog-CSDN Blog

MongoDB document-advanced use-spring-boot integration using MongoDB---MongoTemplate to complete additions, deletions, modifications and queries_One Single Cheng's Blog-CSDN Blog

The official website tutorial is as follows: 

Install MongoDB - MongoDB-CN-Manual

 Version Features

The following is the selection of each version, please refer to it when installing and selecting a version:

MongoDB 2.x version:

This version introduces the Aggregation Pipeline function, which is a function in MongoDB that uses the principle of data pipelines to implement data aggregation operations, similar to the GROUP BY and HAVING clauses in SQL. In addition, the 2.x version also adds important features such as Sharded Cluster and Pluggable Storage Engines.

MongoDB 3.2 version:

This version improves the Sharded Cluster, making it easier and more flexible to configure the Sharded Cluster. In addition, version 3.2 also adds features such as Partial Indexes, Document Validation, and Aggregation Pipeline.

MongoDB 3.4 version:

This version has been improved in data replication (Replication) and fault recovery (Fault Tolerance), making the system more reliable and available. In addition, version 3.4 also adds features such as Geospatial Indexing, Binary Data, and User-Defined Functions.

MongoDB 4.0 version:

This version introduces MongoDB 4.0, which focuses on scalability (Scalability), performance (Performance) and reliability (Reliability). It introduces new features such as Multi-Document Transactions, Aggregation Pipeline, and ACID Guarantees.

MongoDB 4.2 version:

This version has greatly improved in terms of query and aggregation, and has added features such as Text Search, Geo-Spatial Query, and Fixed View. At the same time, version 4.2 also enhances the aggregation pipeline (Aggregation Pipeline) to support more operations and operators.

 In short:
no transactions are required, it is recommended to choose 3.4
for simple transactions, it is recommended to choose 4.0
for distributed transactions or other necessary functions, it is recommended to choose 4.2
for learning purposes, it is recommended to choose the latest version

According to the official announcement, the MongoDB 7.0.0-rc10 (release candidate) version mainly introduces the following new features and improvements:

Current latest: 

  1. A new data repair tool has been introduced that improves data availability and consistency.
  2. Improved failure recovery for replica sets, providing faster failover and data recovery.
  3. The aggregation pipeline has been enhanced to support more operations and operators.
  4. New geospatial query and indexing capabilities have been introduced to make it easier to perform geospatial queries.
  5. Improved performance and scalability, providing higher throughput and better durability guarantees.

In addition, some known bugs and bugs have been fixed, improving system stability and security. Overall, MongoDB 7.0.0-rc10 introduces many new features and improvements that help improve data availability, consistency, and performance

This article is divided into two parts:

        Ordinary linux installation - and configure mongodb environment variables

        docker installation - mount the data volume to the outside

Ordinary linux installation

Official webpage (official download URL)

Install MongoDB Community Kubernetes Operator | MongoDB

1. Download the tgz package corresponding to the selected version from the official website.

 

I chose the latest version here because of my learning needs. 

 Note that the packaging format must be tgz

2. Upload the file to the linux system

Use the server connection tool to complete. Pay attention to your storage path.

 

 3. Unzip the mongdb installation package

First enter the directory where the compressed package is located

Decompress the target file, the code for decompressing the file in linux is as follows

cd /压缩包存在路径
tar -zxvf 目标压缩文件.tgz

After the decompression is complete, we will get a mongodb folder. At this time, you can delete the previous compressed package.

 

This folder contains folders such as bin. 

 4. Create the necessary directories for mongodb

4/1. Create a mongodb folder in the /usr/local directory

cd /usr/local
mkdir mongodb

Pay attention to where to create the folder according to where you store mongodb.

4/2. Create the data directory and logs directory under mongodb, as well as the log file mongodb.log

cd mongodb #进入文件夹
mkdir data
mkdir logs
touch /usr/local/mongodb/logs/mongodb.log

 5. Move the mongodb installation directory

Move all the files in the decompressed file to the folder we created ourselves for easy management:

 Note on linux syntax: /* is to use mv syntax for all subfiles to move files

mv mongodb-linux-x86_64-rhel70-7.0.0-rc10/* /usr/local/mongodb

Notice:

1. Under the current folder, that is to say, according to the path of the article, it needs to be under the /usr/local path

2. The following path is the target path to move to

6. Set the environment variables of mongodb

If you don't do environment configuration, you have to write a lot of startup parameters every time you start.

Configure the startup of environment variables:

vim /etc/profile

Add a configuration to the file

export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH

When writing the configuration file, you must pay attention to the specifications in the configuration file, do not have extra spaces, and pay attention to the path in the environment variable.

Notice! ! ! ! :

After modifying the environment variables, be sure to restart or use the following code to reload the modified configuration file

source /etc/profile

 Using the above code will reload  /etc/profile the file and apply the latest environment variable settings without closing and reopening the terminal.

 Or close and reopen the terminal to reload the environment

7. Add mongodb configuration file

Add the corresponding configuration file and use code to edit the configuration file.

vim /etc/mongodb.conf

Add a common configuration:

#指定数据库路径
dbpath=/usr/local/mongodb/data
#指定MongoDB日志文件
logpath=/usr/local/mongodb/logs/mongodb.log
# 使用追加的方式写日志
logappend=true
#端口号
port=27017 
#方便外网访问,外网所有ip都可以访问,不要写成固定的linux的ip
bind_ip=0.0.0.0
fork=true # 以守护进程的方式运行MongoDB,创建服务器进程
#auth=true #启用用户验证
#bind_ip=0.0.0.0 #绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定则默认本地所有IP

8. Start and shut down MongoDB

Give permissions to the mongoDB folder

sudo chmod -R 777 /usr/local/mongodb

Enter the bin directory

cd /usr/local/mongodb/bin

Start MongoDB (-conf uses the configuration file to start)

mongod -f /etc/mongodb.conf

Close MongoDB (-conf is closed by configuration file)

mongod --shutdown -f /etc/mongodb.conf

Check if running

#查看进程
ps -ef | grep mongod

 Connectivity testing with visualization tools

Notice! ! ! :

Before connecting, make sure whether the online security group allows the port number, and whether the firewall port number on Linux has been opened.

Below are some instructions for turning off the firewall.

在Linux中,关闭防火墙的指令取决于您使用的具体发行版。大多数发行版都使用了防火墙工具,如iptables或firewalld。

以下是几个常见发行版的关闭防火墙指令:

在Ubuntu或Debian上,可以使用以下命令关闭防火墙:
sudo ufw disable
在CentOS或RHEL上,可以使用以下命令关闭防火墙:
sudo systemctl stop firewalld
或者

sudo iptables -P INPUT ACCEPT
sudo iptables -P output ACCEPT
sudo iptables -P forward ACCEPT
sudo iptables -t nat -P PREROUTING ACCEPT
sudo iptables -t nat -P POSTrouting ACCEPT
sudo iptables -t mangle -P PREROUTING ACCEPT
sudo iptables -t mangle -P POSTrouting ACCEPT
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT DROP
sudo iptables -t nat -P PRERouting DROP
sudo iptables -t nat -P postrouting DROP
sudo iptables -t mangle -P PREROUTING DROP
sudo iptables -t mangle -P POSTrouting DROP
这些命令将关闭iptables防火墙,但是请注意,这些命令可能会对系统造成安全风险

 Docker installs mongodb and mounts data volumes - the version is 4.2 as an example

1. Pull the image of mongodb4.2 version

docker pull mongo:4.2

  

2. Check if there is a Mongo image

docker images

3. Create directories and configuration files

Create a directory:

mkdir -p /root/mongo/data
mkdir -p /root/mongo/conf
mkdir -p /root/mongo/log

Create a file:

cd /root/mongo/conf
vim mongodb.conf
#端口
port=27017
#数据库文件存放目录
dbpath=/root/mongo/data
#日志文件存放路径
logpath=/root/mongo/log
#使用追加方式写日志
logappend=true
#以守护线程的方式运行,创建服务器进程
fork=true
#最大同时连接数
maxConns=100
#不启用验证
#noauth=true
#每次写入会记录一条操作日志
journal=true
#存储引擎有mmapv1、wiredTiger、mongorocks
storageEngine=wiredTiger
#访问IP
bind_ip=0.0.0.0
#用户验证
#auth=true

Note that the above path and the path of the connection configuration can be customized, and the configuration has been arranged according to your own needs.

4. Run the mongo container (externally mount configuration files and data) to mount the data volume

docker run -d \
--name mongodb  \
-p 27017:27017 \
-v /root/mongo/data:/data/db \
-v /root/mongo/conf:/data/conf \
-v /root/mongo/log:/data/log \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=123456 \
--privileged=true \
--restart always \
mongo:4.2

This code is used to run a container named "mongodb" in Docker and map it to port 27017 of the local host. The data, configuration and log directories of the container are assigned to the local /root/mongo/data, /data/conf and /data/log directories.

At the same time, the container also sets two environment variables: MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_password, which are set to "admin" and "123456" respectively. These environment variables can be used inside the container for initial database setup of MongoDB.

In addition, the container also uses the privileged mode (--privileged=true), which allows the container to have more system permissions. Finally, the --restart always option is used, which means the container will always automatically restart after exiting.

To sum up, the function of this code is to run a MongoDB container named "mongodb" in Docker, and map it to port 27017 of the local host, and map the container's data, configuration and log directories to the local /root/mongo/data, /data/conf and /data/log directories, and set the initial database user name and password of MongoDB. Containers use privileged mode and will always restart automatically.

Note: The user and password lines are not required. How can this be done without this

-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=123456 \

Just delete these two lines of code.

5. Check whether it is running normally 

docker ps -a

6. Try to use the visualization tool to connect - use the account password

Use the account password connection method.

 Enter your username and password below.

Guess you like

Origin blog.csdn.net/weixin_72186894/article/details/132054891