Error: not authorized on database to execute command { listCollections: 1.0, filter: { type: "collection" }, nameOnly: false, authorizedCollections: false.
docker 安装mongodb:docker pull mongo:latest
docker ps 查看CONTAINER ID
进入容器:
docker exec -it 容器id mongo admin
创建用户:
db.createUser({ user: ‘admin‘, pwd: ‘123‘, roles: [ { role: "userAdminAnyDatabase", db: "demo" } ] });
db.auth(‘admin‘,‘123‘)
新创建数据库:
db.grantRolesToUser(‘admin‘,[{ role: "dbAdmin", db: "demo" }])