1.docker 安装mongo shell脚本
#!bin/bashecho '创建网络'docker network create mongodbecho '创建成功'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.png
3.给mongodb数据库添加账户修改安装mongo shell脚本
#!bin/bashecho '启动权限容器'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 adminMongoDB shell version v3.4.10connecting to: mongodb://127.0.0.1:27017/adminMongoDB server version: 3.4.10Welcome 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/adminMongoDB 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.000GBlocal 0.000GB >use logs > db.createUser({ user: 'root1', pwd: 'root1', roles: [ { role: "dbAdmin", db: "logs" } ] });
参考资料:创建建用户
作者:even_366
链接:https://www.jianshu.com/p/fa09b25b0a5c