Mongodb设置超级用户和普通用户

1.修改dbPath(为后期普通用户做铺垫)

[hadoop@hadoop01 ~]$ sudo vi /etc/mongod.conf 

# mongod.conf
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
  dbPath: /mon
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:
# how the process runs
processManagement:
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
# network interfaces
net:
  port: 27017
#  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.
security:
   authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:

2.创建目录

[hadoop@hadoop01 packages]$ sudo mkdir /mon
[hadoop@hadoop01 packages]$ cd /mon
[hadoop@hadoop01 mon]$ ll
total 0

3.修改mon的权限

[hadoop@hadoop01 /]$ ll
drwxr-xr-x   2 root root  4096 Jan  9 16:12 mon

[hadoop@hadoop01 /]$ sudo chown -R mongod:mongod mon

4.重启mongodb

[hadoop@hadoop01 ~]$ sudo service mongod restart
Stopping mongod:                                           [  OK  ]
Starting mongod:                                           [  OK  ]

5.再次进入mongo

[hadoop@hadoop01 ~]$ mongo
> show dbs;
admin  0.000GB
local  0.000GB

6.设置超级用户

> db.createUser({
... user:"admin",
... pwd:"123456",
... roles:[{role:"root",db:"admin"}]
... });
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

#############使用超级用户############
> use admin;
switched to db admin
> db.auth("admin","123456")
1

7.设置普通用户

> show tables;
system.users
system.version
> use pt
switched to db pt
> db.createUser({
... user:"pt",
... pwd:"123456",
... roles:["readWrite"]
... })
Successfully added user: { "user" : "pt", "roles" : [ "readWrite" ] }

你可能感兴趣的:(Mongodb)