MongoDB 安裝指南: How to Download MongoDB and Configure with Access Control on macOS Matually or via Docker
Tags: MongoDB
| macOS/Linux
| Docker
|Database Security
MongoDB is a powerful, NoSQL database that is widely used for its scalability and flexibility. Unlike traditional relational databases, MongoDB is a documental database, which allows for more dynamic and scalable data structures in JSON-like format.
MongoDB 是一種功能強大的 NoSQL 資料庫,因其可擴展性和靈活性而被廣泛使用。與傳統的關聯式資料庫不同,MongoDB 是一個文件資料庫,它允許 JSON-like 格式的資料結構更加動態和可擴展。
In this blog, I will guide you through the steps to install MongoDB on macOS, configure it properly, and enable access control to secure your database. By the end of this tutorial, you’ll have a fully functional MongoDB setup with restricted access to ensure your data remains secure.
在這篇部落格中,我將引導您完成在 macOS 上安裝 MongoDB 的步驟,正確配置並啟用訪問控制以保護您的資料庫。完成本教程後,您將擁有一個功能齊全的 MongoDB 設置,並限制訪問以確保您的數據保持安全。
Table of Contents
1. Introduction
• Step 1: Download MongoDB Binary Files
• Step 2: Extract the Files
• Step 3: Copy Extracted Files to Target Directory
• Step 4: Add MongoDB to PATH
• Connect to the MongoDB Server Using the Mongo Shell
• Install MongoDB Community with Docker
Manually Download via Terminals
Step 1: Download MongoDB Binary Files
First, you need to download the binary files for the desired release of MongoDB. You can do this directly from the terminal using curl from the website MongoDB’s official document.
首先,您需要下載所需版本的 MongoDB 二進位檔。您可以直接從終端使用 curl 從 MongoDB 的官方網站下載。
curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.0.15.tgz
This command downloads the MongoDB archive to your current directory.
此命令將 MongoDB 壓縮檔下載到您當前的目錄。
Step 2: Extract the Files
Once the download is complete, extract the files from the downloaded archive using the tar command:
下載完成後,使用 tar 命令從下載的壓縮檔中提取文件:
tar -zxvf mongodb-osx-x86_64-3.0.15.tgz
This command will create a directory containing the MongoDB binaries.
此命令將創建一個包含 MongoDB 二進位檔的目錄。
Step 3: Copy Extracted Files to Target Directory
Next, you need to copy the extracted files to the location where you want MongoDB to run. For simplicity, let’s use /user/local/mongodb
mkdir -p /usr/local/mongodb
cp -R -n mongodb-osx-x86_64-3.0.15/ /usr/local/mongodb
Step 4: Add MongoDB to PATH
To ensure that the MongoDB binaries are in your PATH, you need to modify your shell’s rc file. This allows you to run MongoDB commands from any terminal session.
Open your .zshrc (for Zsh) or .bashrc (for Bash) file in a text editor and add the following line:
為確保 MongoDB 二進位檔在您的 PATH 中,您需要修改 shell 的 rc 文件。這樣您就可以從任何終端會話運行 MongoDB 命令。在文本編輯器中打開您的 .zshrc(適用於 Zsh)或 .bashrc(適用於 Bash)文件,並添加以下行:
export PATH=$HOME/mongodb/bin:$PATH
Start the MongoDB Server
Now, you can start the MongoDB server using the mongod command. MongoDB requires a data directory to store its data. Create this directory and start the server:
現在,您可以使用 mongod 命令啟動 MongoDB 伺服器。MongoDB 需要一個數據目錄來存儲其數據。創建此目錄並啟動伺服器:
mkdir -p $HOME/mongodb-data
mongod --dbpath $HOME/mongodb-data
Connect to the MongoDB Server Using the Mongo Shell
Open a new terminal window and connect to the MongoDB server using the mongo command:
打開一個新的終端窗口,使用 mongosh 命令連接到 MongoDB 伺服器:
mongosh
You should see output indicating that the MongoDB server has started successfully.
您應該會看到指示 MongoDB 伺服器已成功啟動的輸出。
Docker Installation
Alternatively, you can install MongoDB using Docker. This method allows you to run MongoDB in a containerized environment.
或者,您可以使用 Docker 安裝 MongoDB。此方法允許您在容器化環境中運行 MongoDB。
Install MongoDB Community with Docker
You can run MongoDB Community Edition as a Docker container using the official MongoDB Community image.
Download the MongoDB Docker image:
docker pull mongodb/mongodb-community-server:latest
Run the MongoDB container:
運行 MongoDB 容器:
docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:latest
Verify the container is running:
驗證容器是否正在運行:
docker container ls
You should see the MongoDB container listed as Up.
您應該會看到 MongoDB 容器列為 Up。
Basic MongoDB Configuration
MongoDB uses a configuration file to set various parameters. On macOS, the configuration file is usually located at /opt/homebrew/etc/mongod.conf
Open the configuration file
MongoDB 使用配置文件來設置各種參數。在 macOS 上,配置文件通常位於 /opt/homebrew/etc/mongod.conf。打開配置文件:
sudo nano /opt/homebrew/etc/mongod.conf
Ensure your configuration file includes the following basic settings:
確保您的配置文件包含以下基本設置:
systemLog:
destination: file
path: /opt/homebrew/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /opt/homebrew/var/mongodb
net:
bindIp: 127.0.0.1, ::1
ipv6: true
Enable Access Control
To secure your MongoDB instance, you need to enable access control. Add the following section to your mongod.conf
file:
為了保護您的 MongoDB 實例,您需要啟用訪問控制。將以下部分添加到您的 mongod.conf 文件中:
security:
authorization: enabled
Save the file by pressing Ctrl + O, then Enter, and exit nano by pressing Ctrl + X.
按 Ctrl + O 保存文件,然後按 Enter,按 Ctrl + X 退出 nano。重新啟動 MongoDB 服務以
Restart the MongoDB service to apply the changes:
brew services restart mongodb-community
If using Docker, restart the Docker container:
docker restart mongodb
Create an Admin User
With access control enabled, you need to create an admin user to manage your MongoDB instance. Connect to MongoDB using the MongoDB shell (mongosh):
mongosh
For Docker, connect using the port mapping:
mongosh --port 27017
Switch to the admin database and create an admin user:
use admin
db.createUser({
user: "myUserAdmin",
pwd: passwordPrompt(), // or cleartext password
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" }
]
})
You’ll be prompted to enter a password for the new user.
Test the Configuration
Exit the MongoDB shell and reconnect using the new admin credentials:
mongosh -u myUserAdmin -p --authenticationDatabase "admin"
For Docker, include the port:
mongosh --port 27017 -u myUserAdmin -p --authenticationDatabase "admin"
Perform some basic operations to verify everything is set up correctly. For example, switch to a test database, create a collection, and insert a document:
use testDB
db.createCollection("testCollection")
db.testCollection.insertOne({name: "test", value: 1})
db.testCollection.find()
You should see the inserted document returned by the find command.
Handle .mongorc.js Warning
If you encounter a warning about .mongorc.js, you can rename it to .mongoshrc.js to comply with the MongoDB shell’s expectations:
mv ~/.mongorc.js ~/.mongoshrc.js
This should provide a clear, organized guide for your readers on how to install and configure MongoDB on macOS using both manual and Docker methods.
Good luck with your installation.