Docker部署MongoDB 5.0.5

1、查看目录

复制代码
root@wielun:~# tree mongo
mongo
├── conf
│   └── mongod.conf
├── data
├── docker-compose.yml
└── log

root@wielun:~# cd mongo
root@wielun:~/mongo# chmod 777 log

2、配置docker-compose.yml

复制代码
root@wielun:~/mongo# cat docker-compose.yml
version: '3'
services:
  mongo:
    image: mongo:5.0.5
    privileged: true
    restart: always
    container_name: mongo
    hostname: mongo
    environment:
      TZ: Asia/Shanghai
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root
    ports:
      - 27017:27017
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./conf/mongod.conf:/etc/mongod.conf
      - ./log:/var/log/mongodb
      - ./data:/data/db
    command: mongod --config /etc/mongod.conf

3、配置mongod.conf

复制代码
root@wielun:~/mongo# cat conf/mongod.conf
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /data/db
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:
  #authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

4、配置mongo

复制代码
root@wielun:~/mongo# docker-compose up -d
root@wielun:~/mongo# docker exec -it mongo /bin/bash
root@mongo:/# ps -ef   #默认添加了--auth,代表需要账号密码登录
UID          PID    PPID  C STIME TTY          TIME CMD
mongodb        1       0  4 12:45 ?        00:00:00 mongod --config /etc/mongod.conf --auth

root@mongo:/# mongo -u root -p
MongoDB shell version v5.0.5
Enter password:
...
---
> db.version()
5.0.5
> use admin
switched to db admin
> db.auth("root","root")
1
> show users
{
        "_id" : "admin.root",
        "userId" : UUID("c5e91caa-4e50-445b-9537-c7066313b52b"),
        "user" : "root",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}

> db.createUser({user:'root',pwd:'root',
... roles:[
... {
... "role" : "clusterAdmin",
... "db" : "admin"
... },
... {
... "role" : "readAnyDatabase",
... "db" : "admin"
... },
... {
... "role" : "readWrite",
... "db" : "test"
... }
... ]});
Successfully added user: {
        "user" : "root",
        "roles" : [
                {
                        "role" : "clusterAdmin",
                        "db" : "admin"
                },
                {
                        "role" : "readAnyDatabase",
                        "db" : "admin"
                },
                {
                        "role" : "readWrite",
                        "db" : "test"
                }
        ]
}
> db.auth("root","root")
1
> db.test.insert({ item: "wielun"})
WriteResult({ "nInserted" : 1 })

创建用户:

复制代码
db.createUser({user:'root',pwd:'root',
roles:[
	{
		"role" : "clusterAdmin",
		"db" : "admin"
	},
	{
		"role" : "readAnyDatabase",
		"db" : "admin"
	},
	{
		"role" : "readWrite",
		"db" : "test"
	}
]});

5、查看结果

这里使用的MongoDB Compass进行连接


相关推荐
喜欢你,还有大家2 小时前
k8s集群监控的部署
云原生·容器·kubernetes
喜欢你,还有大家3 小时前
实战演练——wordpress-k8s集群版
云原生·容器·kubernetes
德育处主任4 小时前
『NAS』轻松获取群晖自带的壁纸
服务器·docker
e***87704 小时前
Node.js 与 Docker 深度整合:轻松部署与管理 Node.js 应用
docker·容器·node.js
q***71016 小时前
从零到上线:Node.js 项目的完整部署流程(包含 Docker 和 CICD)
docker·容器·node.js
ArabySide6 小时前
【ASP.NET Core】ASP.NET Core应用的Docker容器化与阿里云托管
阿里云·docker·asp.net
好奇的菜鸟8 小时前
在 WSL 中安装 Docker
运维·docker·容器
杨浦老苏8 小时前
AI驱动的图表生成器Next-AI-Draw.io
人工智能·docker·ai·群晖·draw.io
秋邱11 小时前
高等教育 AI 智能体的 “导学诊践” 闭环
开发语言·网络·数据库·人工智能·python·docker