port=27017
dbpath=/mongodb/data
logpath=/mongodb/log/mongodb.log
pidfilepath=/mongodb/mongo.pid
# 以守护进程的方式运行MongoDB,创建服务器进程
fork=true
# 使用追加的方式写日志
logappend=true
# 设置每个数据库将被保存在一个单独的目录
directoryperdb=true
# 启用验证
#auth=true
httpinterface=true
master=true
# 启动
/mongodb/mongoapp/bin/mongod -f mongodb.conf --rest
# 停机
/mongodb/mongoapp/bin/mongod --shutdown -f mongodb.conf
>use admin
> db.createUser(
{
user: "hzero",
pwd: "hzero",
roles: [{role: "userAdminAnyDatabase",db:"admin"}]
}
)
#结果
> Successfully added user: {
"user" : "hzero",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> db.auth("hzero","hzero")
# 新建一个root账户
> db.createUser({
user: 'root', pwd: 'root123',
roles: [{role: 'root', db: 'admin'}]}
)
# 单独创建一个pms用户给hims数据库
> db.auth("root","root123")
> use hims
> db.createUser(
{
user: "pms",
pwd: "hzero",
roles: [{role: "readWrite",db:"hims"}]
}
}
# 连接
> mongo 127.0.0.1:27017/admin -u hzero -p "hzero"
#认证一下你刚才创建的用户
> db.auth("hzero","hzero")