> use admin switched to db admin > db.createUser({user:"root",pwd:"root",roles:[{role:"readWrite",db:"admin"},{role:"userAdminAnyDatabase",db:"admin"}]}) Successfully added user: { "user" : "root", "roles" : [ { "role" : "readWrite", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } >
配置文件添加
security:
authorization: enabled
[root@liruilong ~]# vim /etc/mongod.conf [root@liruilong ~]# systemctl restart mongod.service [root@liruilong ~]# mongo MongoDB shell version v4.0.20 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("4e5f7400-7fb5-4adf-8530-97906b118048") } MongoDB server version: 4.0.20