This article takes you to quickly master how to install and deploy MongoDB in Windows and Linux systems


foreword

In order to consolidate the knowledge learned, the author tried to start publishing some blogs of learning notes for future review. Of course, it would be great if it could help some newcomers learn new technologies. The author is a piece of food. If there are any mistakes in the records in the article, readers and friends are welcome to criticize and correct.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

1. Installation and start in Windows system


1. Download the installation package

  • MongoDB provides precompiled binary packages for 32-bit and 64-bit systems. You can download and install them from the MongoDB official website. MongoDB precompiled binary packages download address:

https://www.mongodb.com/try/download/community

提示:版本的选择:MongoDB的版本命名规范如:x.y.z;y为奇数时表示当前版本为开发版,如:1.5.2、4.1.13;y为偶数时表示当前版本为稳定版,如:1.6.3、4.0.10; z是修正版本号,数字越大越好。

2. Unzip the installation and start

  • Unzip the archive into a directory. In the decompression directory, manually create a directory for storing data files, such as data/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 .

insert image description here

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


  • Method 2: Start the service in configuration file mode , create a new config folder in the decompression directory, and create a new configuration file mongod.conf in this folder. The contents are as follows:

storage:
#The directory where the mongod instance stores its data.Default Value is “\data\db” on Windows.
dbPath: D:\WorK_Software\mongodb-windows-x86_64-6.0.8\mongodb-win32-x86_64-windows-6.0.8\data\db

insert image description here

详细配置项内容可以参考官方文档:

https://docs.mongodb.com/manual/reference/configuration-options/

start method

mongod -f …/config/mongod.conf

mongod --config …/config/mongod.conf

insert image description here


3. Shell connection (mongo command)

  • Enter the following shell command at the command prompt (start the service, create a new window) to complete the login

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

insert image description here

  • View existing databases

show databases

insert image description here

  • exit mongodb

exit

  • More parameters can be viewed through help:

mongo --help

提示:MongoDB javascript shell是一个基于javascript的解释器,故是支持js程序的。


4. Compass- GUI client

  • Go to MongoDB official website to download MongoDB Compass,

https://www.mongodb.com/zh-cn/products/compass

如果是下载安装版,则按照步骤安装;如果是下载加压缩版,直接解压,执行里面的MongoDBCompassCommunity.exe 文件即可。

  • In the opened interface, enter the host address, port and other relevant information, and click Connect:

insert image description here


2. Installation, startup and connection in Linux system

1. Download the installation package

  • Go to the official website to download the compressed package mongod-linux-x86_64-4.0.10.tgz

https://www.mongodb.com/try/download/community

insert image description here


2. Unzip and install

  • Upload the compressed package to Linux and decompress it to the current directory:

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

insert image description here

  • Move the unzipped folder to the specified directory:

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

insert image description here

  • Create several directories under the bin directory at the same level to store data and logs respectively:

#Data storage directory
mkdir -p mongodb/single/data/db
#Log storage directory
mkdir -p mongodb/single/log

insert image description here

3. Create and modify configuration files

  • create a new file

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

4. Start the MongoDB service

  • start service

/usr/local/mongodb/bin/mongod -f /mongodb/single/mongod.conf

[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
  • Check the process to see if the service is started
[root@bobohost single]# ps -ef |grep mongod
root 90384 1 0 826 ? 00:02:13 /usr/local/mongdb/bin/mongod -f /mongodb/single/mongod.conf
  • Use the mongo command and the compass tool to connect and test

提示:如果远程连接不上,需要配置防火墙放行,或直接关闭linux防火墙

#View firewall status
systemctl status firewalld
#Temporarily close the firewall
systemctl stop firewalld
#Start the firewall
systemctl disable firewalld

5. Close the MongoDB service

  • Stop and close the service method 1

Kill the process directly through the system's kill command:

#Close the node by process number
kill -2 54410

  • Stop and close the service method 2

Shut down the server through the shutdownServer command in the mongo client

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

Summarize

Everyone is welcome to leave a message for exchange and criticism. If the article is helpful to you or you think the author's writing is not bad, you can click to follow, like, and bookmark to support.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

Guess you like

Origin blog.csdn.net/HHX_01/article/details/132127666