1. 下载
[root@host-10-23-110-128 mysql]# docker pull mongo:latest
latest: Pulling from library/mongo
7b1a6ab2e44d: Pull complete
90eb44ebc60b: Pull complete
5085b59f2efb: Pull complete
c7499923d022: Pull complete
019496b6c44a: Pull complete
c0df4f407f69: Pull complete
351daa315b6c: Pull complete
a233e6240acc: Pull complete
a3f57d6be64f: Pull complete
dd1b5b345323: Pull complete
Digest: sha256:5be752bc5f2ac4182252d0f15d74df080923aba39700905cb26d9f70f39e9702
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest
[root@host-10-23-110-128 mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 11 months ago 113MB
mysql latest 3218b38490ce 11 months ago 516MB
mongo latest dfda7a2cf273 11 months ago 693MB
[root@host-10-23-110-128 mysql]#
2. 配置
[root@host-10-23-110-128 mysql]# docker run --name mongo -p 27017:27017 -v /home/docker/data/mongo/data/:/data/db -v /home/docker/data/mongo/backup/:/data/backup -d mongo --auth
391a31a9bbe0b791c6d9ea2ae53fd431c8e38e27cbff5f528dbec67278367832
[root@host-10-23-110-128 mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
391a31a9bbe0 mongo "docker-entrypoint.s…" 4 seconds ago Up 4 seconds 0.0.0.0:27017->27017/tcp mongo
129f0779069d mysql "docker-entrypoint.s…" 24 minutes ago Up 24 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
a7aafa90cd99 redis "docker-entrypoint.s…" 2 hours ago Up 5 minutes 0.0.0.0:6379->6379/tcp redis
[root@host-10-23-110-128 mysql]# [root@host-10-23-110-128 mysql]# docker exec -it mongo mongo admin
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("46447b1d-2681-4aae-ab77-84202a2c0d1f") }
MongoDB server version: 5.0.5
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
> db.createUser({ user: 'hett', pwd: 'hett', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
Successfully added user: {
"user" : "hett",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> use admin
switched to db admin
> db.auth("hett","hett")
1
> use test01
switched to db test01
> db.createUser({user:"hett",pwd:"hett",roles:["readWrite"]});