操作系统: Ubuntu 18.04。
MongoDB版本: v4.4.14版。
我已经在AWS EC2实例上配置了一个3节点mongo副本集。为了复制相同的环境,我创建了AMI并在另一个帐户中启动了ec2实例。我能够运行单神服务,但无法验证和连接到mongo。我曾经使用下面的命令进行连接:
mongo -u "adminuser" -p "adminpass" --authenticationDatabase "admin"
这在源计算机中有效,但在新的情况下,我得到了以下错误:
MongoDB shell version v4.4.14
connecting to: mongodb://localhost:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1
Mongo日志没有显示任何特定的错误:
{"t":{"$date":"2022-06-30T20:30:02.506+00:00"},"s":"I", "c":"ACCESS", "id":20249, "ctx":"conn6","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"srrclusteradmin","authenticationDatabase":"admin","remote":"127.0.0.1:60426","extraInfo":{},"error":"UserNotFound: Could not find user \"srrclusteradmin\" for db \"admin\""}}
{"t":{"$date":"2022-06-30T20:30:02.506+00:00"},"s":"I", "c":"ACCESS", "id":20249, "ctx":"conn6","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-1","speculative":false,"principalName":"srrclusteradmin","authenticationDatabase":"admin","remote":"127.0.0.1:60426","extraInfo":{},"error":"UserNotFound: Could not find user \"srrclusteradmin\" for db \"admin\""}}
{"t":{"$date":"2022-06-30T20:30:02.507+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn6","msg":"Connection ended","attr":{"remote":"127.0.0.1:60426","connectionId":6,"connectionCount":0}}
{"t":{"$date":"2022-06-30T20:30:54.948+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"WTCheckpointThread","msg":"WiredTiger message","attr":{"message":"[1656621054:948266][2295:0x7f81a1103700], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 254, snapshot max: 254 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 190"}}
{"t":{"$date":"2022-06-30T20:31:54.956+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"WTCheckpointThread","msg":"WiredTiger message","attr":{"message":"[1656621114:955999][2295:0x7f81a1103700], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 256, snapshot max: 256 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 190"}}
{"t":{"$date":"2022-06-30T20:32:54.965+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"WTCheckpointThread","msg":"WiredTiger message","attr":{"message":"[1656621174:965737][2295:0x7f81a1103700], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 258, snapshot max: 258 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 190"}}
{"t":{"$date":"2022-06-30T20:33:54.973+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"WTCheckpointThread","msg":"WiredTiger message","attr":{"message":"[1656621234:973751][2295:0x7f81a1103700], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 260, snapshot max: 260 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 190"}}
{"t":{"$date":"2022-06-30T20:34:53.767+00:00"},"s":"I", "c":"CONTROL", "id":20712, "ctx":"LogicalSessionCacheReap","msg":"Sessions collection is not setup; waiting until next sessions reap interval","attr":{"error":"NamespaceNotFound: config.system.sessions does not exist"}}
{"t":{"$date":"2022-06-30T20:34:53.767+00:00"},"s":"I", "c":"CONTROL", "id":20714, "ctx":"LogicalSessionCacheRefresh","msg":"Failed to refresh session cache, will try again at the next refresh interval","attr":{"error":"NotYetInitialized: Replication has not yet been configured"}}
{"t":{"$date":"2022-06-30T20:34:54.981+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"WTCheckpointThread","msg":"WiredTiger message","attr":{"message":"[1656621294:981888][2295:0x7f81a1103700], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 262, snapshot max: 262 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 190"}}
mongo配置如下:
storage:
dbPath: /mongodata
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: localhost, 10.0.2.56
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
keyFile: /mongo-security/keyfile.txt
# transitionToAuth: true
#operationProfiling:
#replication:
replication:
replSetName: mongors
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
mongod.service (添加了--auth标志)
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target
[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf
PIDFile=/var/run/mongodb/mongod.pid
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings
[Install]
WantedBy=multi-user.target
注意:以下命令是成功的:
MongoDB shell version v4.4.14connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodbImplicit session: session { "id" : UUID("79d782b8-f2c4-464d-9537-00b561bc01ec") }
MongoDB server version: 4.4.14> use admin
switched to db admin
> db.auth("adminuser", passwordPrompt())
Enter password:
Error: Authentication failed.
0
>
不知何故,在这台新机器上,先前起作用的密码不能工作。请建议如何获得访问和身份验证,以检索数据。
很少有更多的输出有错误可供参考:
MongoDB shell version v4.4.14
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b680471c-cc8b-4ea5-a251-b30b6a528448") }
MongoDB server version: 4.4.14
> use admin
switched to db admin
> show collections
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> show dbs
uncaught exception: Error: listDatabases failed:{
"topologyVersion" : {
"processId" : ObjectId("62be0e1f90d1de371954e891"),
"counter" : NumberLong(0)
},
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotPrimaryNoSecondaryOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
发布于 2022-07-04 07:42:55
你的问题和评论中的许多陈述都是错误的:
Mongo没有显示任何特定的错误
它显示了几个错误:
"error":"UserNotFound: Could not find user \"srrclusteradmin\" for db \"admin\""}}
"error":"NotYetInitialized: Replication has not yet been configured"
注意:以下命令是成功的:
db.auth("adminuser",passwordPrompt())输入密码:错误:身份验证失败。
为什么认为“身份验证失败”为“成功”?
,除非我能够访问数据库,否则我无法启动副本集。
不,首先您需要启动副本集,然后您可以创建一个用户,然后您可以使用它进行授权。
启动您的副本集(请参阅https://www.mongodb.com/docs/manual/reference/method/rs.initiate/),然后它就能工作了。
基本命令是rs.initiate()
,您可以给它添加更多的参数。
net:端口: 27017 bindIp: localhost,10.0.2.56
https://stackoverflow.com/questions/72821802
复制相似问题