拉取MongoDB镜像

  1. docker pull mongo:latest

启动MongoDB

  1. docker run --restart=always -itd --name mongo -v /opt/mongodb:/data/db -p 27017:27017 mongo --auth

进入容器

  1. docker exec -it mongo mongo admin

创建用户名

  1. db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});

连接

  1. db.auth('admin', '123456')