官网地址
- https://www.mongodb.com/zh-cn
docker 网络
# 创建 mongo_network 网络
docker network create mongo_network
# 查看网络
docker network list
# 容器连接到 mongo_network 网络
docker network connect mongo_network mongo
# 断开容器 mongo_network 网络
docker network disconnect mongo_network mongo
mongod.conf
# 容器内查看 mongod.conf
cat /etc/mongod.conf.orig
- 缩主机上创建 mongo/configdb/mongod.conf
# 数据
storage:
dbPath: /data/db
journal:
enabled: true
# 日志
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# 端口
net:
port: 27017
# bindIp: 127.0.0.1
# 安全
security:
authorization: enabled
部署 MongoDB
- https://hub.docker.com/_/mongo
# MongoDB镜像
docker pull mongo:4
docker run --name mongo \
--network mongo_network \
-p 27017:27017 \
-v $PWD/mongo/data:/data/db \
-v $PWD/mongo/configdb:/data/configdb \
-v $PWD/mongo/logs:/var/log/mongodb \
-e MONGO_INITDB_ROOT_USERNAME="root" \
-e MONGO_INITDB_ROOT_PASSWORD="4NuMDwIzp05BdKp7Bdmf" \
-d mongo:4 \
--config /data/configdb/mongod.conf
- 指定服务执行器与缓存大小
docker run --name mongo \
--network mongo_network \
-p 27017:27017 \
-v $PWD/mongo/data:/data/db \
-v $PWD/mongo/configdb:/data/configdb \
-v $PWD/mongo/logs:/var/log/mongodb \
-e MONGO_INITDB_ROOT_USERNAME="root" \
-e MONGO_INITDB_ROOT_PASSWORD="4NuMDwIzp05BdKp7Bdmf" \
-d mongo:4 \
--config /data/configdb/mongod.conf \
--serviceExecutor adaptive --wiredTigerCacheSizeGB 1.2
部署 mongo-express
- https://hub.docker.com/_/mongo-express
# mongo-express镜像
docker pull mongo-express:1.0.2
docker run --name mongo-express \
--network mongo_network \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_SERVER=mongo \
-e ME_CONFIG_MONGODB_ADMINUSERNAME="root" \
-e ME_CONFIG_MONGODB_ADMINPASSWORD="4NuMDwIzp05BdKp7Bdmf" \
# 设置 mongo-express 登录账号密码
-e ME_CONFIG_BASICAUTH=true \
-e ME_CONFIG_BASICAUTH_USERNAME="admin" \
-e ME_CONFIG_BASICAUTH_PASSWORD="123456" \
-d mongo-express:1.0.2
- 查看默认登录账号密码
# 进入 mongo-express 容器内
docker exec -it mongo-express /bin/bash
# 查看config.default.js
cat /app/config.default.js
注意: 默认登录账号密码是 admin/pass,docker配置中已经将登录账号密码设置为 admin/123456
- http://localhost:8081/
docker-compose.yml
services:
mongo:
image: mongo:4
container_name: mongo
restart: always
ports:
- 27017:27017
volumes:
- /etc/localtime:/etc/localtime
- ./mongo/data:/data/db
- ./mongo/configdb:/data/configdb
- ./mongo/logs:/var/log/mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 4NuMDwIzp05BdKp7Bdmf
command: --config /data/configdb/mongod.conf --wiredTigerCacheSizeGB 1
# command: --config /data/configdb/mongod.conf --wiredTigerCacheSizeGB 1.2 --serviceExecutor adaptive
mongo-express:
image: mongo-express:1.0.2
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_OPTIONS_EDITORTHEME: "ambiance"
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: 4NuMDwIzp05BdKp7Bdmf
ME_CONFIG_MONGODB_SERVER: mongo
# ME_CONFIG_MONGODB_URL: mongodb://root:4NuMDwIzp05BdKp7Bdmf@mongo:27017/
ME_CONFIG_BASICAUTH: true
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: 123456
depends_on:
- mongo
- http://localhost:8081/
注意: 登录账号密码已改为 admin/123456
MongoDB shell
# 进入 mongo 容器内
docker exec -it mongo /bin/bash
# 或直接进容器内的 MongoDB shell
docker exec -it mongo mongo
# 切换数据库
> use admin
# 管理员身份连接(MONGO_INITDB_ROOT_USERNAME配置值)
> db.auth("root",passwordPrompt())
# 输入密码(MONGO_INITDB_ROOT_PASSWORD配置值)
# 显示数据库
> show dbs
# 显示用户
> show users
文章来源:https://www.toymoban.com/news/detail-860258.html
# 帮助
$ help
# 退出
$ exit
文章来源地址https://www.toymoban.com/news/detail-860258.html
到了这里,关于Docker 部署 MongoDB 数据库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!