docker compose部署mongodb 8.0.28
环境:
OS:Centos 7
mongodb:8.0.28
1.创建部署目录
mkdir -p /opt/mongodb
mkdir -p /opt/mongodb/data
mkdir -p /opt/mongodb/config
mkdir -p /opt/mongodb/log
修改外挂的日志文件目录权限
cd /opt/mongodb
chown -R 999:999 ./log
chmod 755 ./log
2.准备mongodb配置文件
vi /opt/mongodb/config/mongod.conf
[root@localhost conf]# more mongod.conf
net:
bindIp: 0.0.0.0
maxIncomingConnections: 10000
storage:
dbPath: "/data/db"
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 6
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
logRotate: reopen
##security:
## authorization: enabled
这里将密码验证先注释掉,等后面创建了密码后再启用
3.准备部署文件
cd /opt/mongodb
vi docker-compose.yml
[root@host134 mongodb]# more docker-compose.yml
version: '3.8'
services:
mongodb:
image: registry.cn-shenzhen.aliyuncs.com/hxlk8s/mongo:8.0.28
container_name: mongodb
restart: always
ports:
- "28001:27017"
volumes:
- ./data:/data/db
- ./config/mongod.conf:/etc/mongod.conf
- ./log:/var/log/mongodb
environment:
TZ: Asia/Shanghai
command:
- mongod
- --config
- /etc/mongod.conf
4.启动
[root@host134 mongodb]#cd /opt/mongodb
[root@host134 mongodb]#docker compose up -d
5.链接mongodb创建账号
下载客户端连接到容器mongosh
wget https://downloads.mongodb.com/compass/mongosh-2.10.0-linux-x64.tgz
tar -xvf mongosh-2.10.0-linux-x64.tgz
/soft/mongosh-2.10.0-linux-x64/bin/mongosh 192.168.1.134:28001
test>use admin
admin>db.createUser({user:"root",pwd:"hxl@123",roles:["root"]}); --创建用户
admin>db.auth("root","hxl@123"); --设置用户登陆权限,密码一定要和创建用户时输入的密码相同
admin>show users; --查看创建的用户
6.开启密码验证
修改配置文件
vi /opt/mongodb/config/mongod.conf
security:
authorization: enabled
7.重启
cd /opt/mongodb
docker compose stop
docker compose start
8.再次登录
/soft/mongosh-2.10.0-linux-x64/bin/mongosh 192.168.1.134:28001
test> show dbs
MongoServerError[Unauthorized]: Command listDatabases requires authentication
use admin
test> use admin
switched to db admin
admin> db.auth("root","hxl@123");
{ ok: 1 }
admin> show dbs
admin 132.00 KiB
config 60.00 KiB
local 72.00 KiB
9.创建开发者账号
创建新的数据库和账号
>use admin
admin>db.createUser({user:'hxldev',pwd:'hxldev123',roles:[{role:'dbOwner',db:'mgdb_hxl'}]})
要写入一条记录才会显示库名
admin> use mgdb_hxl
switched to db mgdb_hxl
mgdb_hxl> show tables;
mgdb_hxl> db.tb_test.insert({"name": "xiaoming"})
10.相应的安装工具下载
下载地址
https://www.mongodb.com/try/download/database-tools
选择相应的版本
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.18.0.tgz
[root@host134 bin]# /soft/mongodb-database-tools-rhel70-x86_64-100.18.0/bin/mongodump --version
mongodump version: 100.18.0
git version: 21a342dfee6468ad9350d156d25086da64dd03b1
Go version: go1.26.5
os: linux
arch: amd64
compiler: gc
11.备份库
[root@host134 bin]# /soft/mongodb-database-tools-rhel70-x86_64-100.18.0/bin/mongodump -h 192.168.1.134:28001 -u threedev -p "threedev123" --authenticationDatabase admin --db=mgdb_hxl -o /tmp/mgdb_hxl_20260907 --gzip
2026-09-07T14:16:03.789+0800 writing `mgdb_hxl.tb_test` to `/tmp/mgdb_hxl_20260907/mgdb_hxl/tb_test.bson.gz`
2026-09-07T14:16:03.791+0800 done dumping `mgdb_hxl.tb_test` (1 document)
浙公网安备 33010602011771号