MongoDB 基础教程

MongoDB 高级教程

MongoDB 笔记

original icon
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.knowledgedict.com/tutorial/mongodb-install.html

MongoDB 安装教程


MongoDB 主要分为社区版(Community)和企业版(Enterprise),其中社区版本是免费的,官方提供了 Windows、Linux 和 Mac OS 平台上的安装包。

MongoDB 安装

MongoDB 官方针对 Linux 平台不同的发行版,提供了 Red Hat/CentOS、Ubuntu、Debian 等安装包。

Linux 安装 MongoDB

MongoDB 提供了 Linux 各个发行版本的安装包,可以到官方网站下载 https://docs.mongodb.com/manual/installation/

首先介绍在 Red Hat/CentOS 平台的安装步骤:

  1. 配置包管理器(yum);

    首先创建 /etc/yum.repos.d/mongodb-org-4.4.repo 文件,并配置 MongoDB 相关信息,形式如下:

    [mongodb-org-4.4]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
  2. 安装 MongoDB 安装包;

    安装全套模块,运行如下命令:

    sudo yum install -y mongodb-org

    当然也可以,指定安装所需的模块,示例如下:

    sudo yum install -y mongodb-org-4.4.1 mongodb-org-server-4.4.1 mongodb-org-shell-4.4.1 mongodb-org-mongos-4.4.1 mongodb-org-tools-4.4.1

Windows 安装 MongoDB

Windows 平台安装 MongoDB 非常简便,到官方网站下载 .msi 文件,直接一次次点击下一步即可完成。

启动 MongoDB,直接双击类似如下路径的文件:

C:\Program Files\MongoDB\Server\4.4\bin\mongo.exe

Mac OS 安装 MongoDB

Mac OS 下主要有两种安装 MongoDB 的方式,一是直接到官网下载 dmg 文件并可视化安装,另一种建议通过 homebrew 安装。

通过 brew 命令安装如下:

  1. 首先搜索可安装的 MongoDB 列表;

    brew search mongodb

    执行后,输出如下:

    mongodb/brew/mongocli                     mongodb/brew/mongodb-community@3.4        mongodb/brew/mongodb-database-tools
    mongodb/brew/mongodb-community            mongodb/brew/mongodb-community@3.6        mongodb/brew/mongosh
    mongodb/brew/mongodb-community-shell      mongodb/brew/mongodb-community@4.0
    mongodb/brew/mongodb-community@3.2        mongodb/brew/mongodb-community@4.2

    ✔ 表示当前已安装的程序;

  2. 通过 install 命令安装,命令如下:

    brew install mongodb-community@4.2

    查看安装情况,类似如下:

    brew info mongodb-community@4.2
    mongodb/brew/mongodb-community@4.2: stable 4.2.9 [keg-only]
    High-performance, schema-free, document-oriented database
    https://www.mongodb.com/
    /usr/local/Cellar/mongodb-community@4.2/4.2.8 (21 files, 306.3MB)
      Built from source on 2020-08-23 at 15:24:57
    From: https://github.com/mongodb/homebrew-brew/blob/HEAD/Formula/mongodb-community@4.2.rb
    ==> Caveats
    mongodb-community@4.2 is keg-only, which means it was not symlinked into /usr/local,
    because this is an alternate version of another formula.
    
    
    To have launchd start mongodb/brew/mongodb-community@4.2 now and restart at login:
      brew services start mongodb/brew/mongodb-community@4.2
    Or, if you don't want/need a background service you can just run:
      mongod --config /usr/local/etc/mongod.conf