Docker安装mogodb给数据库添加用户账号和密码
2018-02-02 本文已影响40人
even_366
1.docker 安装mongo shell脚本
#!bin/bash
echo '创建网络'
docker network create mongodb
echo '创建成功'
docker run --name mongodb1 -p 27017:27017 -v /home/mongo/db:/data/db -d --network mongodb mongo:3.4
由于防火墙没有关闭27017端口导致服务器被攻击。
2. docker启动mongodb数据库没有给数据库加密导致数据库数据丢失。
TIM图片20180202110709.png3.给mongodb数据库添加账户修改安装mongo shell脚本
#!bin/bash
echo '启动权限容器'
docker run --name mongodb1 -p 33601:27017 -v /home/mongo/db1:/data/db -d --network mongodb mongo:3.4 --auth
4.进入容器使用admin账户 docker exec -it mongodb2 mongo admin
[root@localhost mongo]# docker exec -it mongodb2 mongo admin
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017/admin
MongoDB server version: 3.4.10
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
>
5创建root账户授权root
> db.createUser({ user: 'root', pwd: 'root', roles: [ { role: "root", db: "admin" } ] });
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
#退出容器
>exit
6用root用户登录 (创建数据库 logs 用户名root1 密码root1)
[root@localhost mongo]# docker exec -it mongodb2 mongo -u root -p root 127.0.0.1:27017/admin
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017/admin
MongoDB server version: 3.4.10
Server has startup warnings:
2018-02-02T03:22:54.307+0000 I CONTROL [initandlisten]
2018-02-02T03:22:54.307+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-02-02T03:22:54.307+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-02-02T03:22:54.307+0000 I CONTROL [initandlisten]
2018-02-02T03:22:54.307+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-02-02T03:22:54.307+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-02-02T03:22:54.307+0000 I CONTROL [initandlisten]
> show dbs
admin 0.000GB
local 0.000GB
>use logs
> db.createUser({ user: 'root1', pwd: 'root1', roles: [ { role: "dbAdmin", db: "logs" } ] });
参考资料:创建建用户
Docker:https://hub.docker.com/_/mongo/
Dockerfile:https://github.com/dockerlibrary/mongo/blob/8a439d6df6a95f424c41dab451f640cd08ba68c7/3.4/Dockerfile