mongodb刚安装完,
创建超级用户
$mongo #进入mongo控制台
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
>
> use admin #进入admin库
switched to db admin
> db #查看当前数据库
admin
> db.createUser({user:"master",pwd:"123456",roles:[{"role":"userAdminAnyDatabase","db":"admin"}]}) #创建超级用户
Successfully added user: {
"user" : "master",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
打开mongodb的验证
net:
port: 27017
bindIp : 0.0.0.0 #监听所有ip
security:
authorization: enabled #开启密码验证
用超级用户登录mongo
$mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
>
>use admin
switched to db admin
>db.auth("master","123456") #用超级用户登录了
去创建一个test库,然后创建test库的账户密码
$mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
>
>use admin
switched to db admin
>db.auth("master","123456") #用超级用户登录了
>use test
switched to db test
> db.createUser({user: "test",pwd: "test",roles: [{ role: "readWrite", db: "test" }] }) #创建对test库有读写权限的账户密码
Successfully added user: {
"user" : "test",
"roles" : [
{
"role" : "readWrite",
"db" : "timecash"
}
]
}
测试test库的账户密码是否能登录
$ mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
>
>use test
switched to db test
>db.auth("test","test")
1
>db.abc.insert({"a":1,"b":2}) #在test库的abc表插入数据
用test账户直接登录
$ mongo test -u test -p test
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017/timecash
MongoDB server version: 3.4.10
> db
test