【英文】MongoDB安装笔记

Preface

MongoDB Installation Notes

Install MongoDB

  • MongoDB version numbers are divided into three sections. When the second section is odd, it represents the development version; when the second section is even, it represents the stable version.

Install Community Edition via Installation Package

Install Community Edition via brew on MacOS

  • MongoDB has announced that it is no longer open source. Since September 2, 2019, HomeBrew has also removed the mongodb module from the core repository (#43770).
  • However, you can still use brew install mongodb. MongoDB provides a separate HomeBrew Community Edition installation: https://github.com/mongodb/homebrew-brew.
1
2
brew tap mongodb/brew
brew install mongodb-community

Configuration File

MacOS/Linux

Create directory to store database files and log files

1
2
mkdir -p /usr/local/mongodb/db
mkdir -p /usr/local/mongodb/log

Create configuration file

1
touch mkdir -p /usr/local/mongodb/mongodb.conf

Modify the configuration file

systemLog.destination: Specify the log output method. The default value is to output to the console, and file will output to a file.
systemLog.path: Specify the location to store the log file.
systemLog.logAppend: Specifies whether to append mode when logging. If it is true, each restart will append the previous log. If it is false, each restart will overwrite the log.
storage.dbPath: Specify the location to store the database files.
storage.journal.enabled: Specifies whether to enable the journal. The default value is true, which means that the status before the MongoDB crashes and restarts will be restored.
processManagement.fork: Specifies whether to start as a daemon process. The default value is true, which means to start as a daemon process.
net.bindIp: Configure IP

<ip>: Local area network IP

net.port: Configure the port number.
security.authorization: Specifies whether to enable authentication.

enabled: Enable authentication.

/usr/local/mongodb/mongodb.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
systemLog:
destination: file
path: "<src>"
logAppend: true
storage:
dbPath: "/usr/local/mongodb/db"
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: localhost,<ip>
port: 27017
security:
authorization: enabled

MacOS

  • The configuration file path for MongoDB installed via brew on MacOS
    • Configuration file: /usr/local/etc/mongod.conf
    • Log file: /usr/local/var/log/mongodb
    • Database file: /usr/local/var/mongodb

MongoDB Service

Windows/Linux/MacOS

Start the service

--dbpath=<src>: Specify the directory to store the database files.
--config=<src>, -f <src>: Specify the location of the configuration file mongod.conf.
--auth: Enable authentication.

1
mongod

Stop the service

  • Log in to the MongoDB console and stop the service using the built-in function.
1
2
use admin
db.shutdownServer()

MacOS

  • MongoDB installed via brew on MacOS can manage the service through services.
1
2
3
brew services start mongodb-community
brew services stop mongodb-community
brew services restart mongodb-community

MongoDB Client

Older Versions

--host=: Specify the IP address.

127.0.0.1: Default IP address.

--port=: Specify the port number.

27017: Default port number.

1
mongo

Newer Versions

1
mongosh

Done

References

MongoDB Official Documentation
Bilibili - Black Horse Programmer