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# chown 999.999 log     # 或者直接给目录777权限: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: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
    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 admin -p
MongoDB shell version v5.0.5
Enter password:
...
---
> db.version()
5.0.5
> use admin
switched to db admin
> db.auth("admin","admin")
1
> show users
{
        "_id" : "admin.admin",
        "userId" : UUID("c5e91caa-4e50-445b-9537-c7066313b52b"),
        "user" : "admin",
        "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进行连接

在这里插入图片描述
在这里插入图片描述

6、更新

创建一个wielun库

use admin;

db.updateUser("root", {"roles": [
    {"role": "clusterAdmin", db: "admin"}, 
    {"role": "readAnyDatabase", db: "admin"},
    {"role": "readWrite", db: "test"}, 
    {"role": "readWrite", db: "wielun"}
]});

7、删除

use wielun;
db.dropDatabase() 

8、通过eval创建数据库

Docker容器中执行

# 创建一个账号为wielun,密码为123456,数据库:test
# mongo -u admin -p admin --eval "db.createUser({user: 'wielun', pwd: '123456', roles: [{role: 'readWrite', db: 'test'}]});"

# 更新admin账户,并创建一个test数据库
# mongo -u admin -p admin --eval "db = db.getSiblingDB('admin');db.updateUser('root', {roles: [{role: 'root', db: 'admin'},{role: 'readWrite', db: 'test'}]})"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wielun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值