docker 应用 —— docker 安装 MongoDB ,配置用户名和密码
文章目录1、查看 mongo镜像2、运行安装命令:3、 查看 mongo 进程4、 创建管理员admin 用户和密码4.1、以 admin 用户身份进入mongo :4.2、创建一个 admin 管理员账号:4.3、退出:4.4、完整 shell 如下:5、创建普通用户、密码和数据库5.1、 以 admin 用户身份进入mongo :5.2、对 admin 进行身份认证:5.3、创建 用户、密码和
·
文章目录
1、查看 mongo镜像
执行 docker search mongo
命令:
[root@iz2zei0x4t16rv0e5buzvhz tmp]# docker search mongo
NAME DESCRIPTION STARS
mongo MongoDB document databases provide high avai… 5406
mongo-express Web-based MongoDB admin interface, written w… 335
tutum/mongodb MongoDB Docker image – listens in port 27017… 224
mvertes/alpine-mongo light MongoDB container 88
bitnami/mongodb Bitnami MongoDB Docker Image 76
mongoclient/mongoclient Official docker image for Mongoclient, featu… 59
mongooseim/mongooseim Small docker image for MongooseIM - robust a… 17
frodenas/mongodb A Docker Image for MongoDB 17
cvallance/mongo-k8s-sidecar Kubernetes side car to setup and maintain a … 8
istepanov/mongodump Docker image with mongodump running as a cro… 5
2、运行安装命令:
docker run \
--name mongodb_server \
-p 27017:27017 \
-v /mysoft/mongodb/configdb:/data/configdb/ \
-v /mysoft/mongodb/db/:/data/db/ \
-d mongo --auth
3、 查看 mongo 进程
[root@iz2zei0x4t16rv0e5buzvhz tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
a7e5d4e4ca69 mongo "docker-entrypoint.s…" 6 seconds ago Up 4 seconddb_server
4、 创建管理员admin 用户和密码
4.1、以 admin 用户身份进入mongo :
docker exec -it a7e5d4e4ca69 mongo admin
4.2、创建一个 admin 管理员账号:
db.createUser({ user: 'admin', pwd: 'admin123456', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
4.3、退出:
exit
4.4、上述操作记录:
root@iz2zei0x4t16rv0e5buzvhz tmp]# docker exec -it a7e5d4e4ca69 mongo admin
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017/admin
Implicit session: session { "id" : UUID("5bd17aa0-f8f6-454e-a1f3-6917d98336bc") }
MongoDB server version: 4.0.4
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
> use admin
switched to db admin
> db.createUser({ user: 'admin', pwd: 'admin123456', roles: [ { role: "userAdminAnyDatabase", db
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> exit
5、创建普通用户、密码和数据库
5.1、 以 admin 用户身份进入mongo :
docker exec -it a7e5d4e4ca69 mongo admin
5.2、对 admin 用户 进行身份认证:
db.auth("admin","admin123456");
5.3、创建 用户、密码和数据库:
db.createUser({ user: 'swen', pwd: 'swen123456', roles: [ { role: "readWrite", db: "app" } ] });
5.4、 退出:
exit
5.5、上述操作记录:
[root@iz2zei0x4t16rv0e5buzvhz tmp]# docker exec -it a7e5d4e4ca69 mongo admin
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017/admin
Implicit session: session { "id" : UUID("592da935-824f-4593-b056-a381915c7205") }
MongoDB server version: 4.0.4
> db.auth("admin","admin123456");
1
> db.createUser({ user: 'swen', pwd: 'swen123456', roles: [ { role: "readWrite", db: "app" } ] }
Successfully added user: {
"user" : "swen",
"roles" : [
{
"role" : "readWrite",
"db" : "app"
}
]
}
> exit
bye
[root@iz2zei0x4t16rv0e5buzvhz tmp]
6、登录 APP 数据库
6.1、 以 admin 用户身份进入mongo :
docker exec -it a7e5d4e4ca69 mongo admin
6.2、对 swen 进行身份认证:
db.auth("swen","swen123456");
6.3、切换数据库:
use app
6.4、添加数据:
db.test.save({name:"zhangsan"});
6.5、上述操作记录:
[root@iz2zei0x4t16rv0e5buzvhz tmp]# docker exec -it a7e5d4e4ca69 mongo admin
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017/admin
Implicit session: session { "id" : UUID("13f9c7bc-fc90-42b0-bfd8-a1a7a27aaee9") }
MongoDB server version: 4.0.4
> db.auth("swen","swen123456");
1
> use app
switched to db app
> db.test.save({name:"zhangsan"});
WriteResult({ "nInserted" : 1 })
> ^C
bye
[root@iz2zei0x4t16rv0e5buzvhz tmp]#
7、NoSQL Manager for MongoDB 登录
插入数据:
查看数据:
更多推荐
已为社区贡献14条内容
所有评论(0)