一、实验环境
操作系统:CentOS7.5 Minimal
IP: 192.168.1.105
二、安装docker,拉取mongoDB镜像
【Docker】CentOS7.x上docker的安装方式
https://www.jianshu.com/p/d1a07a13a76f
# docker pull mongo:4.2
# docker run -it --rm mongo:4.2 cat /etc/issue
# docker run -it --rm mongo:4.2 cat /etc/passwd
# docker run -it --rm mongo:4.2 mongo --version
三、创建相关安装目录
# mkdir -p /opt/mongoconfig
# mkdir -p /opt/mongoconfigdb
# mkdir -p /opt/mongodata
拷贝原始配置文件模板
# docker run -it --rm mongo:4.2 cat /etc/mongod.conf.orig > /opt/mongoconfig/mongod.conf
# cat /opt/mongoconfig/mongod.conf
三、初始化mongDB容器
# docker run -it -d \
--name mongod \
-p 27017:27017 \
-v /opt/mongoconfigdb:/data/configdb \
-v /opt/mongodata:/data/db \
mongo:4.2
登录mongDB,创建管理员账号
# docker exec -it mongod mongo
> db.createUser({ user: 'admin', pwd: 'Admin@123', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
修改配置文件,增加身份认证
# grep -Ev "^$|^#" /opt/mongoconfig/mongod.conf
###########################################
storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /dev/stdout
net:
port: 27017
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
###########################################
# vim /opt/mongoconfig/mongod.conf
一开始就开启了auth认证,是不能创建用户的,必须先以普通方式启动
创建好用户后,再以认证方式启动,才能以用户名加密码的方式连接。
重启mongoDB
# docker rm -f mongod
# docker run -it -d \
--name mongod \
-p 27017:27017 \
-v /opt/mongoconfigdb:/data/configdb \
-v /opt/mongodata:/data/db \
-v /opt/mongoconfig/mongod.conf:/etc/mongod.conf \
mongo:4.2 mongod -f /etc/mongod.conf
# docker exec -it mongod mongo -u admin -p "Admin@123" --authenticationDatabase admin
# docker logs -f mongod
以 admin 用户身份进入mongo
# docker exec -it mongod mongo admin
对 admin 用户 进行身份认证
> db.auth("admin","admin@123");
创建 用户、密码和数据库
> db.createUser({ user: 'test', pwd: 'Test@123', roles: [ { role: "readWrite", db: "test" } ] });
# docker exec -it mongod mongo admin
> db.auth("test","Test@123");
> use test;
> db.test.save({name:"Michael"});
四、将服务注册成系统服务
# docker rm -f mongod
编写unit文件
# vim /etc/systemd/system/mongod.service
############################################################
[Unit]
Description=mongoDB Server
Documentation=https://www.mongodb.com
After=network-online.target docker.service
Requires=docker.service
[Service]
ExecStartPre=-/usr/bin/docker rm -f mongod
ExecStart=/usr/bin/docker run \
--name mongod \
-p 27017:27017 \
-v /opt/mongoconfigdb:/data/configdb \
-v /opt/mongodata:/data/db \
-v /opt/mongoconfig/mongod.conf:/etc/mongod.conf \
mongo:4.2 mongod -f /etc/mongod.conf
ExecStop=/usr/bin/docker stop mongod
LimitNOFILE=65535
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
############################################################
# systemctl daemon-reload
# systemctl start mongod.service
# systemctl enable mongod.service
# systemctl status mongod.service
五、参考
CentOS7.x上使用docker安装部署mongoDB
https://www.jianshu.com/p/cf73f9a6d2d8
mongodb 开启密码认证配置
https://wp.hellocode.name/?p=844
mongodb 开启认证方式
https://www.cnblogs.com/Knight7971/p/10149643.html
mongod.conf documentation
http://docs.mongodb.org/manual/reference/configuration-options
mongoDB Production Notes
https://docs.mongodb.com/manual/administration/production-notes