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进行连接


相关推荐
景天科技苑3 小时前
【云原生开发】K8S多集群资源管理平台架构设计
云原生·容器·kubernetes·k8s·云原生开发·k8s管理系统
wclass-zhengge3 小时前
K8S篇(基本介绍)
云原生·容器·kubernetes
颜淡慕潇3 小时前
【K8S问题系列 |1 】Kubernetes 中 NodePort 类型的 Service 无法访问【已解决】
后端·云原生·容器·kubernetes·问题解决
川石课堂软件测试5 小时前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
昌sit!11 小时前
K8S node节点没有相应的pod镜像运行故障处理办法
云原生·容器·kubernetes
追风林12 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
A ?Charis14 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
城南vision14 小时前
Docker学习—Docker核心概念总结
java·学习·docker
wclass-zhengge15 小时前
Docker篇(Docker Compose)
运维·docker·容器
北漂IT民工_程序员_ZG15 小时前
k8s集群安装(minikube)
云原生·容器·kubernetes