MongoDB5 installation configuration + setting automatic startup in Windows+Linux system (latest installation and configuration method)

1. Installation and start in Windows system

1. Download MongoDB

Step 1: Download the installation package
MongoDB provides pre-compiled binary packages for 32-bit and 64-bit systems. You can download and install them from the official website of MongoDB. MongoDB pre-compiled binary package download address:
https://www.mongodb.com/ download-center #community
insert image description here

Download the zip package as shown above.
Tips: Version selection:
MongoDB version naming convention such as: xyz;
when y is an odd number, it means that the current version is a development version, such as: 1.5.2, 4.1.13;
when y is an even number, it means that the current version is a stable version, such as: 1.6 .3, 4.0.10;
z is the revised version number, the bigger the number, the better.

Details:
http://docs.mongodb.org/manual/release-notes/#release-version-numbers
Step 2: Unzip the installation and start
Unzip the compressed package into a directory.
In the decompression directory, manually create a directory for storing data files, such as data/db
Method 1: Start the service with command line parameters
Open the command line prompt in the bin directory and enter the following command:

mongod --dbpath=..\data\db

We can see in the startup information that the default port of mongoDB is 27017. If we want to change the default startup port, we can specify the port through –port.
In order to facilitate our startup every time, we can set the bin directory of the installation directory to the path of the environment variable. The bin directory contains some common commands, such as mongod startup service and mongo client connection
service.
insert image description here

Here I want to explain that mongodb does not need to configure environment variables. Configuring environment variables is just to execute the commands in the bin directory in any path of the terminal (cmd window), which is a convenient function.
It is also very simple to configure environment variables: first Baidu "How to set win10 environment variables". Notice! ! After entering the environment variable, what needs to be modified is the path in the user environment variable. Select the path and double-click, open it and click "New", and paste the path of the bin directory (mine is: E:\xusheng\MongoDB\mongodb-5.0. 8\bin)
does not configure environment variables. If you want to start the database, you can only open the cmd window in the decompressed bin directory and enter mongod --dbpath=...\data\db to start it. Here you don’t need to start the database because you haven’t created a data directory or a db directory. Of course You'll create and start.

Method 2: Start the service by configuration file
Create a new conf folder in the decompression directory, and create a new configuration file mongod.cfg in this folder. For example, refer to the following: The
windows configuration conf file has been changed, and the suffix is ​​.cfg. It is recommended to view the official website configuration document

storage:
	#The directory where the mongod instance stores its data.Default Value is "\data\db" on Windows.
	dbPath: E:\xusheng\MongoDB\mongodb-5.0.8\data\db
systemLog:
	destination: file
	path: E:\xusheng\MongoDB\mongodb-5.0.8\data\log\mongod.log
storage:
	#The directory where the mongod instance stores its data.Default Value is "\data\db" on Windows.
	dbPath: E:\xusheng\MongoDB\mongodb-5.0.8\data\db
net:
	port: 27017
	#错误日志采用追加模式
	logappend=true
	#启用日志文件,默认启用
	journal=true
	#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
	quiet=true
	#端口号 默认为27017
	port=27017
	#开启子进程
	fork=true
	#开启认证,必选先添加用户,先不开启(不用验证账号密码)
	#auth=true

For detailed configuration items, please refer to the official document: https://docs.mongodb.com/manual/reference/configuration-options/

2. Create directory and configuration file

Next, create a data directory at the same level as the bin directory, and continue to create db and log under the data directory. The mongod.log file also needs to be created in the log directory. This file must be created, otherwise an error will be reported if it cannot be found.

db: indicates the folder for data storage
log: indicates the folder for log printing

insert image description here
[Note]
1) If double quotes are used in the configuration file, such as the path address, the content of the double quotes will be escaped automatically. If not escaped, an error will be reported:

error-parsing-yaml-config-file-yaml-cpp-error-at-line-3-column-15-unknown-escape-character-d

solve:

a. Replace \ with / or \
b. If there are no spaces in the path, quotes are not required.

2) Fields cannot be separated by tabs in the configuration file

Solution:
Convert it to spaces.

3. Add to service (start automatically at boot)

Only when mongodb is added to the system service can it be started automatically.

**Notice! ! **The cmd window must be opened as an administrator. Enter the file according to the following method
insert image description here
Enter the following commands in order, pay attention! ! The path here needs to be modified by yourself.
Here it means to execute the configuration file, you need to write your configuration file path:

mongod --config E:\xusheng\MongoDB\mongodb-5.0.8\conf\mongod.cfg --install --sesrviceName "MongoDB"

insert image description here
If the configuration file executes an error and fails to start in the end, then configure mongoDB on the command line by yourself, and replace the command that needs to be executed above with the following command (note that you need to modify your own path)

mongod --dbpath "E:\xusheng\MongoDB\mongodb-5.0.8\data\db" --logpath "E:\xusheng\MongoDB\mongodb-5.0.8\data\log\mongod.log" --install --serviceName "MongoDB"

insert image description here
Start mongoDB:

net start MongoDB

or

mongod -f ../conf/mongod.cfg
mongod --config ../conf/mongod.cfg

In the future, if you modify the port of mongoDB, directly modify the configuration file, delete the service (delete the service command: sc delete MongoDB), and re-execute "add to service"

4. Check

Press "WIN+R" on the keyboard, and enter the "services.msc" command to confirm.
After opening the service, find MongoDB, as shown in the figure below, it has been successful!
So far you have completed all configurations of mongoDB. Next if you need to connect to the database. There are two situations:

Use the cmd command window to connect
If you have configured the environment variable, you can directly enter mongo in the cmd window to connect successfully
If you have not configured the environment variable, you need to open the cmd window in the bin directory and enter mongo

5. Shell connection (mongo command

Enter the following shell command at the command prompt to complete the login

mongo
mongo --host=127.0.0.1 --port=27017

View existing databases

>show databases

insert image description here

exit mongodb

exit

More parameters can be viewed through help:

mongo --help

Tip:
MongoDB javascript shell is a javascript-based interpreter, so it supports js programs.

6. Compass- GUI client

Go to the MongoDB official website to download MongoDB Compass,
address:
https://www.mongodb.com/download-center/v2/compass?initial=true
If you are downloading the installation version, follow the steps to install it; if you are downloading and compressing the version, decompress it directly , execute the MongoDBCompassCommunity.exe file inside.
In the opened interface, enter the host address, port and other relevant information, and click Connect:
insert image description here

2. Installation and start in Linux system

Goal: Deploy a single-machine MongoDB in Linux for use in a production environment.
Tip: It is similar to the operation under Windows.
Proceed as follows:

1. Download and decompress

(1) First go to the official website to download the compressed package mongod-linux-x86_64-4.0.10.tgz.
(2) Upload the compressed package to Linux and decompress it to the current directory:

tar -xvf mongodb-linux-x86_64-4.0.10.tgz

(3) Move the decompressed folder to the specified directory:

mv mongodb-linux-x86_64-4.0.10 /usr/local/mongodb

(4) Create several new directories to store data and logs respectively:

#数据存储目录
mkdir -p /mongodb/single/data/db
#日志存储目录
mkdir -p /mongodb/single/log

2. Create and modify configuration files

(5) Create and modify configuration files

vi /mongodb/single/mongod.conf

The content of the configuration file is as follows:

systemLog:
#MongoDB发送所有日志输出的目标指定为文件
# #The path of the log file to which mongod or mongos should send all diagnostic logging information
destination: file
#mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
path: "/mongodb/single/log/mongod.log"
#当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
logAppend: true
storage:
#mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
##The directory where the mongod instance stores its data.Default Value is "/data/db".
dbPath: "/mongodb/single/data/db"
journal:
#启用或禁用持久性日志以确保数据文件保持有效和可恢复。
enabled: true
processManagement:
#启用在后台运行mongos或mongod进程的守护进程模式。
fork: true
net:
#服务实例绑定的IP,默认是localhost
bindIp: localhost,192.168.0.2
#bindIp
#绑定的端口,默认是27017
port: 27017

(6) Start the MongoDB service

[root@bobohost single]# /usr/local/mongodb/bin/mongod -f /mongodb/single/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 90384
child process started successfully, parent exiting

Note:
If it is not successfully started, it means that the startup failed. The reason is basically a problem with the configuration file.
Check whether the service is started by process:

[root@bobohost single]# ps -ef |grep mongod
root 90384 1 0 8月26 ? 00:02:13 /usr/local/mongdb/bin/mongod -f /mongodb/single/mongod.conf
bin/mongo mongodb://192.168.10.102:27017

insert image description here
insert image description here

3. Connection test

(7) Use the mongo command and the compass tool to connect and test.
Tip: If the remote connection fails, you need to configure the firewall to allow it, or directly close the linux firewall

#查看防火墙状态
systemctl status firewalld
#临时关闭防火墙
systemctl stop firewalld
#开机禁止启动防火墙
systemctl disable firewalld

(8) Stop and shut down the service
There are two ways to stop the service: quick shut down and standard shut down, which are described in turn below:
(1) Quick shut down method (fast, simple, and data may be wrong)
Goal: Kill directly through the system's kill command Process:
Check after killing to avoid not killing some.

#通过进程编号关闭节点
kill -2 54410

4. supplement

If it is due to data corruption, you need to do the following (understand):
1) Delete the lock file:

rm -f /mongodb/single/data/db/*.lock

2) Repair data:

/usr/local/mongdb/bin/mongod --repair --dbpath=/mongodb/single/data/db

(2) Standard shutdown method (data is not easy to make mistakes, but troublesome):
Goal: Shut down the service through the shutdownServer command in the mongo client. The
main operation steps are as follows:

//客户端登录服务,注意,这里通过localhost登录,如果需要远程登录,必须先登录认证才行。
mongo --port 27017
//#切换到admin库
use admin
//关闭服务
db.shutdownServer()

Guess you like

Origin blog.csdn.net/m0_52435951/article/details/124873142