1 拉取镜像
docker pull mongo
2 运行容器
docker run -itd --name mongo --restart=always -p 27017:27017 -v /mydata/mongo/data:/data/db mongo:latest
docker run -itd --name mongo --restart=always -p 27017:27017 -v /mydata/mongo/data:/data/db mongo:latest --auth
3 进入容器设置账户(无密码可不设置)
docker exec -it mongo mongosh admin
db.createUser({ user:'admin',pwd:'123456',roles:[{ role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]})
db.auth('admin', '123456')
---------------------------------------------------------------
db.createUser(
{
user:"root",
pwd:"123456",
roles:[{role:"root",db:"admin"}]
}
);
4 重启mongo(需退出容器exit)
docker restart mongo
docker update --restart=no <CONTAINER ID>