1.在无鉴权状态下登录数据库,执行use admin
db.createUser({user: "admin",pwd: "123456",roles: [ { role: "root", db: "admin" }]})
2.执行关闭数据库
db.shutdownServer();
3.加入配置文件mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/mongodb.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
maxIncomingConnections: 1000
# run in background
#processManagement:
# fork: true
security:
authorization: enabled
#operationProfiling:
4.执行admin登录
./mongo --host localhost -u "admin" --authenticationDatabase "admin" -p'123456'
5.切换要创建的用户,比如testbb
use testbb_db
db.createUser({user: "testbb",pwd:"111111",roles:[{role: "readWrite", db: "testbb_db" }]})