MongoDB Community Server 安装教程,适合本地开发使用。MongoDB 官方当前推荐入口是 MongoDB 安装文档 和 Community Server 下载页。
macOS 安装,推荐 Homebrew
安装 Homebrew,如果已经有就跳过:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
添加 MongoDB 官方 Homebrew 源:
brew tap mongodb/brew
安装 MongoDB Community:
brew install mongodb-community@8.0
启动 MongoDB 服务:
brew services start mongodb-community@8.0
检查是否启动成功:
brew services list
进入 MongoDB Shell:
mongosh
看到类似下面的提示就说明成功了:
test>
Windows 安装
打开下载页:
https://www.mongodb.com/try/download/community
选择:
Version:默认最新稳定版即可
Platform:Windows
Package:MSI
下载后双击 .msi 安装包。
安装方式选择:
Complete
勾选 Install MongoDB as a Service
建议勾选安装 MongoDB Compass,这是官方图形化管理工具
安装完成后打开命令行,输入:
mongosh
如果提示找不到 mongosh,需要单独安装 MongoDB Shell:
https://www.mongodb.com/try/download/shell
Ubuntu 安装,MongoDB 8.0 示例
安装依赖:
sudo apt-get install gnupg curl
导入 MongoDB GPG key:
curl -fsSL https://pgp.mongodb.com/server-8.0.asc | \ sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \ --dearmor
如果是 Ubuntu 22.04:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | \ sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
更新并安装:
sudo apt-get update sudo apt-get install -y mongodb-org
启动 MongoDB:
sudo systemctl start mongod
设置开机自启:
sudo systemctl enable mongod
测试连接:
mongosh
常用命令
# macOS 启动 brew services start mongodb-community@8.0 # macOS 停止 brew services stop mongodb-community@8.0 # Linux 启动 sudo systemctl start mongod # Linux 停止 sudo systemctl stop mongod # 进入 MongoDB mongosh
简单测试
进入 mongosh 后输入:
use testdb
db.users.insertOne({ name: "Tom", age: 18 })
db.users.find()
如果能查到刚插入的数据,就说明 MongoDB 安装并运行成功。













