MongoDB Community Server 安装教程,适合本地开发使用。MongoDB 官方当前推荐入口是 MongoDB 安装文档Community Server 下载页

macOS 安装,推荐 Homebrew

  1. 安装 Homebrew,如果已经有就跳过:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. 添加 MongoDB 官方 Homebrew 源:

    brew tap mongodb/brew
    
  3. 安装 MongoDB Community:

    brew install mongodb-community@8.0
    
  4. 启动 MongoDB 服务:

    brew services start mongodb-community@8.0
    
  5. 检查是否启动成功:

    brew services list
    
  6. 进入 MongoDB Shell:

    mongosh
    

看到类似下面的提示就说明成功了:

test>

Windows 安装

  1. 打开下载页:
    https://www.mongodb.com/try/download/community

  2. 选择:

    • Version:默认最新稳定版即可
    • Platform:Windows
    • Package:MSI
  3. 下载后双击 .msi 安装包。

  4. 安装方式选择:

    • Complete
    • 勾选 Install MongoDB as a Service
    • 建议勾选安装 MongoDB Compass,这是官方图形化管理工具
  5. 安装完成后打开命令行,输入:

    mongosh
    

如果提示找不到 mongosh,需要单独安装 MongoDB Shell:
https://www.mongodb.com/try/download/shell

Ubuntu 安装,MongoDB 8.0 示例

  1. 安装依赖:

    sudo apt-get install gnupg curl
    
  2. 导入 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
    
  3. 如果是 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
    
  4. 更新并安装:

    sudo apt-get update
    sudo apt-get install -y mongodb-org
    
  5. 启动 MongoDB:

    sudo systemctl start mongod
    
  6. 设置开机自启:

    sudo systemctl enable mongod
    
  7. 测试连接:

    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 安装并运行成功。

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐