Docker中安装MongoDB

 

Docker 安装 MongoDB

MongoDB 是一个免费的开源跨平台面向文档的 NoSQL 数据库程序。

1、查看可用的 MongoDB 版本

访问 MongoDB 镜像库地址: https://hub.docker.com/_/mongo?tab=tags&page=1
可以通过 Sort by 查看其他版本的 MongoDB,默认是最新版本 mongo:latest
notion image
你也可以在下拉列表中找到其他你想要的版本:
notion image
此外,我们还可以用 docker search mongo 命令来查看可用版本:

2、取最新版的 MongoDB 镜像

这里我们拉取官方的最新版本的镜像:
notion image

3、查看本地镜像

使用以下命令来查看是否已安装了 mongo:
在上图中可以看到我们已经安装了最新版本(latest)的 mongo 镜像。
 
notion image

4、运行容器

安装完成后,我们可以使用以下命令来运行 mongo 容器:
参数说明:
  • p 27017:27017 :映射容器服务的 27017 端口到宿主机的 27017 端口。外部可以直接通过 宿主机 ip:27017 访问到 mongo 的服务。
  • -auth:需要密码才能访问容器服务。
notion image

5、安装成功

最后我们可以通过 docker ps 命令查看容器的运行信息:
notion image
接着使用以下命令添加用户和设置密码,并且尝试连接。
notion image
notion image

© ljl 2021 - 2026