10.(附)一个创建超级管理员的实例,注意 db.createUser()里的格式一定不要错,成功后可用 show users 查看:
2.设置用户和用户密码
> root@iZwz9jb9ek3ckh79tmy891Z:~# mongo
MongoDB shell version: 2.6.10
connecting to: test> show dbs
admin 0.078GB
local 0.078GB
> use admin
switched to db admin
> db.createUser()
2019-01-11T10:33:01.536+0800 TypeError: Cannot read property 'user' of undefined at src/mongo/shell/db.js:1056
> db.createUser(... {user: "chenzhiyong",}...
... )
2019-01-11T10:33:55.588+0800 Error: couldn't add user: Must provide a 'pwd' field for all user documents, except those with '$external' as the user's source db at src/mongo/shell/db.js:1081
> db.createUser({
user: "654634863",
pwd: "54156756",
roles:[{
role: "userAdminAnyDatabase",
db: "admin"}]})
Successfully added user: {"user":"chenzhiyong",
"roles":[{"role":"userAdminAnyDatabase",
"db":"admin"}]}>> show users{"_id":"admin.root",
"user":"root",
"db":"admin",
"roles":[{"role":"root",
"db":"admin"}]}{"_id":"admin.chenzhiyong",
"user":"chenzhiyong",
"db":"admin",
"roles":[{"role":"userAdminAnyDatabase",
"db":"admin"}]}
3.修改配置文件
sudo vim /etc/mongodb.conf
# mongodb.conf
数据存储位置
# Where to store the data.
dbpath=/var/lib/mongodb
日志打印位置
#where to log
logpath=/var/log/mongodb/mongodb.log
开启日志追加
logappend=true
允许远程访问
bind_ip =0.0.0.0
设置端口
port = 27017
# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling
journal=true
# Enables periodic logging of CPU utilization and I/O wait#cpu = true# Turn on/off security. Off is currently the default#noauth = true
开启权限认证
auth =true# Verbose logging output.#verbose = true# Inspect all client data for validity on receipt (useful for# developing drivers)#objcheck = true# Enable db quota management#quota = true# Set diagnostic logging level where n is# 0=off (default)# 1=W# 2=R# 3=both# 7=W+some reads#diaglog = 0# Diagnostic/debugging option#nocursors = true