1、拉取镜像
[centos8@localhost software]$ sudo docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
171857c49d0f: Downloading [===> ] 1.952MB/26.7MB
419640447d26: Download complete
61e52f862619: Download complete
892787ca4521: Download complete
06e2d54757a5: Downloading [===============================================> ] 2.829MB/2.975MB
e2f7d90822f3: Downloading [===============> ] 1.76MB/5.827MB
f518d3776320: Waiting
feb8e9d469d8: Waiting
69705b632494: Waiting
c7daea26376d: Waiting
13d1f9e1fc77: Waiting
f87e65fe7ffd: Waiting
2、运行容器
[root@localhost ~]# docker run -itd --name mongo -p 27017:27017 mongo
WARNING: IPv4 forwarding is disabled. Networking will not work.
c46ebd447cc090732bba4998870e21323264d68c2c69d22ae9826f7c3b36a47f
3、进入mongodb容器
[root@localhost ~]# docker exec -it mongo mongo admin MongoDB shell version v4.2.6 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("606ee7d9-6b05-4093-b497-92faa0a473b0") } MongoDB server version: 4.2.6 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
4、创建user
db.createUser({user:'admin',pwd:'123456',roles:[{role:'userAdminAnyDatabase',db:'admin'}]});
Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
5、尝试使用刚刚创建的用户登陆mongodb
> db.auth('admin','123456') 1 >
进入mongo shell操作
docker exec -it mongo admin