登录容器 docker exec -it mongodb bash
登录mongo mongo -u root -p 123456
查看db show dbs
查看collection show collections
进入db use yapi
查看数据 db.<collection_name>.find()
带条件查看 db.<collection_name>.find({ <field>: <value> })
限制返回条数 db.<collection_name>.find().limit(<limit>)
恢复数据 mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
docker 运行 mongodb
sudo docker run -d \
--name mongodb \
--restart always \
--net=docker \
-p 27017:27017 \
-v ./db:/data/db \
-e MONGO_INITDB_DATABASE=yapi \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=123456 \
mongo:4.2.21
docker exec -it mongodb /bin/bash 进入容器
mongo localhost:27017 进入mongo
use admin;切换数据库
db.auth("root ", "root"); 验证密码
use yapi;
db.createUser({
user: 'yapi',
pwd: '123456',
roles: [
{ role: "dbAdmin", db: "yapi" },
{ role: "readWrite", db: "yapi" }
]
});创建用户
exit 退出
exit 退出容器
备份数据
bash
#####容器宿主机
#!/bin/bash
# 获取当前日期
current_date=$(date +%Y-%m-%d)
month=$(date +%Y-%m)
backup_dir="backup_yapi/${month}/${current_date}"
to_backup_dir="/bak/backup_yapi/${month}" # CP
mkdir -p $to_backup_dir
echo $backup_dir.tar.gz
# 备份MongoDB数据库
docker exec -it mongodb ./data/db/bak.sh
docker cp mongodb:/data/db/$backup_dir.tar.gz $to_backup_dir
# 输出备份完成信息
echo "MongoDB备份完成!"
#####容器内 //./data/db/bak.sh
#!/bin/bash
current_date=$(date +%Y-%m-%d)
host="127.0.0.1"
port="27017"
username="root"
password="123456"
database="yapi" #
month=$(date +%Y-%m)
backup_dir="/data/db/backup_yapi/${month}" U
mkdir -p $backup_dir
mongodump --host $host --authenticationDatabase admin --port $port --username $username --password $password --db $database --out $backup_dir/$current_date
tar -czvf $backup_dir/$current_date.tar.gz $backup_dir/$current_date
rm -rf $backup_dir/$current_date
docker 运行YAPI
初使化
docker run -d --rm --name yapi-init --link mongodb:mongo --net=docker -v ./config.json:/yapi/config.json yapipro/yapi server/install.js
启动YAPI
docker run -d \
--name yapi \
--link mongodb:mongo \
--restart always \
--net=docker \
-p 3000:3000 \
-v ./config.json:/yapi/config.json \
yapipro/yapi \
server/app.js
config.json
{
"port": "3000",
"adminAccount": "999@qq.com",
"timeout":120,
"db": {
"servername": "mongo",
"DATABASE": "yapi",
"port": 27017,
"user": "yapi",
"pass": "123456",
"authSource": ""
},
"mail": {
"enable": true,
"host": "smtp.qq.com",
"port": 端口,
"from": "999@qq.com",
"auth": {
"user": "999@qq.com",
"pass": "999"
}
}
}
adminAccount 管理员账号名:"999@qq.com",密码:"ymfe.org"
参考 YApi-高效、易用、功能强大的可视化接口管理平台------(一)使用 Docker 本地部署_docker yapi_I'm Jie的博客-CSDN博客