我有4个运行mongodb的实例(副本集),每个实例都有以下mongodb.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: /root/mongodata/log/mongod.log
# Where and how to store data.
storage:
dbPath: /root/mongodata/db1 # also have db2 and so on for rest of the instances
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /root/mongodata/db1/mongod.pid # location of pidfile, different for 4 instances
# network interfaces
net:
port: 30000 #port different for 4 different instances
bindIp: 12.123.321.432(example ip, same for all 4 .conf files)
# security
security:
KeyFile: /path to my keyfile location
# authorization: enabled
#operationProfiling:
replication:
replSetName: testReplica #have this same for all 4
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
我还为内部认证创建了一个密钥文件,如下所示:
openssl rand -base64 756 > <path-to-keyfile>
chmod 400 <path-to-keyfile>
在所有实例运行后,我按如下方式打开了mongoShell:
mongo --host 12.123.321.432 --port 30000
我可以打开shell但是当我尝试创建用户时,我得到以下异常:
2016-12-22T20:55:38.396-0500 E QUERY [thread1] Error: couldn't add user: not authorized on test to execute command { createUser: "root", pwd: "xxx", roles: [ { role: "root", db: "admin" } ], digestPassword: false, writeConcern: { w: "majority", wtimeout: 30000.0 } } :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
DB.prototype.createUser@src/mongo/shell/db.js:1230:11
@(shell):1:1
我尝试切换到admin db但仍然说未经授权,我也尝试运行rs.initiate()命令来定义主要和辅助dbs,未经授权说。即使我在禁用身份验证的情况下启动mongod
,我也会阅读keyfile
内部身份验证将强制进行基于角色的身份验证。我在这里错过了什么,我将如何解决它?提前谢谢。
答案 0 :(得分:1)
错误消息显示您没有执行命令的权限。
在 .conf 文件中设置密钥文件参数后,mongo需要您使用auth用户登录。
所以,如果你还没有root用户。
db.createUser({user:"root",pwd:"rootpassword",roles:[{role:"root",db:"admin"}]})
如果您有root用户。你应该使用db.auth()或使用root权限登录mongo来执行rs.conf()命令。
mongo --port portnumber -uroot -p --authenticationDatabase admin
或使用命令 mongo --port portnumber
登录后
db.auth("root", "rootpassword")
PS。 KeyFile用于复制集内部通信安全。