MongoDB installation and connection (Windows and Linux)

1. Introduction to MongoDB

MongoDB is an open source, high-performance, schema-less document database. It was originally designed to simplify development and facilitate expansion. It is one of the NoSQL database products. It is a non-relational database most
like a relational database (MySQL).
The data structure it supports is very loose, a format similar to JSON called BSON, so it can store more complex data types and is quite flexible.
A record in MongoDB is a document, which is a data structure consisting of field and value pairs (field:value). MongoDB documents are similar to JSON objects, i.e. a document
is considered an object. The data type of the field is character type, and its value can include other documents, ordinary arrays and document arrays in addition to some basic types.

2. Installation start in Windows system

2.1. Step 1: Download the installation package

MongoDB provides precompiled binary packages for 32-bit and 64-bit systems. You can download and install from the MongoDB official website. MongoDB precompiled binary package download address:
https://www.mongodb.com/download-center#community
insert image description here
According to the above Download the zip package as shown.
Tip : Version selection:
The version naming convention of MongoDB is: xyz;
when y is an odd number, it means the current version is a development version, such as: 1.5.2, 4.1.13;
when y is an even number, it means the current version is a stable version, such as: 1.6 .3, 4.0.10;
z is the revision number, the bigger the number, the better.
Details : http://docs.mongodb.org/manual/release-notes/#release-version-numbers

2.2. Step 2: Unzip and install

Unzip the tarball into a directory.
In the decompression directory, manually create a directory for storing data files, such asdata/db
insert image description here

  • Method 1: Start the service with command line parameters

Open a command line prompt in the bin directory and enter the following command:

mongod --dbpath=..\data\db

insert image description here

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.

  • Method 2: Start the service in the configuration file mode

Create a new config folder in the decompressed directory, and create a new configuration file mongod.conf in this folderFor reference as follows:

For detailed configuration items, please refer to the official documentation: https://docs.mongodb.com/manual/reference/configuration-options/
【Notice】
1) If double quotes are used in the configuration file, such as the path address, the content of the double quotes will be automatically escaped. If it is 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, no quotation marks are required.

2) The fields in the configuration file cannot be separated by Tab
. Solution:
Convert them to spaces.
Start method:

mongod -f ../config/mongod.conf
或
mongod --config ../config/mongod.conf

More parameter configuration:

systemLog:
destination: file
#The path of the log file to which mongod or mongos should send all diagnostic logging information
path: "D:/02_Server/DBServer/mongodb-win32-x86_64-2008plus-ssl-4.0.1/log/mongod.log"
logAppend: true
storage:
journal:
enabled: true
#The directory where the mongod instance stores its data.Default Value is "/data/db".
dbPath: "D:/02_Server/DBServer/mongodb-win32-x86_64-2008plus-ssl-4.0.1/data"
net:
#bindIp: 127.0.0.1
port: 27017
setParameter:
enableLocalhostAuthBypass: false

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

quit mongodb

exit

More parameters can be viewed through help:

mongo --help

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

4. 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 the compressed version, unzip it directly , execute the MongoDBCompassCommunity.exe file inside.
In the interface that opens, enter the host address, port and other relevant information, and click Connect.

5. Installation start and connection in Linux system

The steps are as follows:
(1) 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 extract 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:
#Data storage directory

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

(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

Notice:
If it is not successful after startup, the startup failed. The reason is basically a problem with the configuration file.
Check whether the service is started through the 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

(7) Use the mongo command and the compass tool to test the connection respectively.
Tip: If the remote connection fails, you need to configure the firewall to release, 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 shutdown and standard shutdown, which are explained in the following order:
(1) Quick shutdown method (quick, simple, data may be wrong)
Target: kill directly through the kill command of the system Process:
After killing it, check it to avoid some not being killed.

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

[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/weixin_46220576/article/details/123114958