MongoDB Docker 身份验证问题“错误:身份验证失败”

时间:2021-03-30 18:45:24

标签: database mongodb docker authentication environment-variables

我正在尝试设置一个 MongoDB Docker 容器以用作本地数据库进行测试,但我遇到了问题。

为了运行容器,我使用了以下命令:

docker run -d --name mongodb -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME="root" -e MONGO_INITDB_ROOT_PASSWORD="password" -v C:\projects\docker\volumes\mongotmp:/data/db mongo:4.4.4

我使用-e传递root用户名和密码环境变量,但是我无法连接到数据库,我尝试使用这个连接字符串:

mongodb://root:password@localhost:27017/?authSource=admin

当我在容器内执行 shell 并尝试使用 db.getUsers() 获取用户时,我收到身份验证错误。

uncaught exception: Error: command usersInfo requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1659:15

然后,我尝试使用 db.auth(root, password) 进行身份验证,但没有成功。

uncaught exception: ReferenceError: root is not defined

正如评论中所建议的,我不得不在 ("root", "password") 周围加上引号以进行授权,但仍然没有用。

Error: Authentication failed.

网上有人遇到这个问题,问题是这个人在使用环境变量运行容器之前创建并填充了卷,这样 MongoDB 无法找到 root 用户。这应该不是我的问题,因为我在旋转容器之前删除了音量。

这是容器日志的一部分:

{"t":{"$date":"2021-03-31T07:30:58.665+00:00"},"s":"I", "c":"COMMAND", "id":51803, "ctx":"conn2","msg":"Slow query","attr":{"type":"command","ns":"admin.$cmd","appName":"MongoDB Shell","command":{"createUser":"root","pwd":"xxx","roles":[{"role":"root","db":"admin"}],"digestPassword":true,"writeConcern":{"w":"majority","wtimeout":600000.0},"lsid":{"id":{"$uuid":"d9cfc623-2db5-4798-bdfe-8fca9d9d5b76"}},"$db":"admin"},"numYields":0,"reslen":38,"locks":{"ParallelBatchWriterMode":{"acquireCount":{"r":6}},"ReplicationStateTransition":{"acquireCount":{"w":6}},"Global":{"acquireCount":{"r":2,"w":4}},"Database":{"acquireCount":{"r":2,"W":4}},"Collection":{"acquireCount":{"r":1,"w":4}},"Mutex":{"acquireCount":{"r":6}}},"flowControl":{"acquireCount":4,"timeAcquiringMicros":5},"writeConcern":{"w":"majority","wtimeout":600000,"provenance":"clientSupplied"},"storage":{},"protocol":"op_msg","durationMillis":143}}

Successfully added user: {

"user" : "root",

"roles" : [

{

"role" : "root",

"db" : "admin"

}

]

}

Error saving history file: FileOpenFailed Unable to open() file /home/mongodb/.dbshell: No such file or directory

{"t":{"$date":"2021-03-31T07:30:58.669+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn2","msg":"Connection ended","attr":{"remote":"127.0.0.1:43118","connectionId":2,"connectionCount":0}}


/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/



{"t":{"$date":"2021-03-31T07:30:58.697+00:00"},"s":"I", "c":"CONTROL", "id":20698, "ctx":"main","msg":"***** SERVER RESTARTED *****"}

{"t":{"$date":"2021-03-31T07:30:58.700+00:00"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}

{"t":{"$date":"2021-03-31T07:30:58.702+00:00"},"s":"W", "c":"ASIO", "id":22601, "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}

{"t":{"$date":"2021-03-31T07:30:58.702+00:00"},"s":"I", "c":"NETWORK", "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}

killing process with pid: 30

{"t":{"$date":"2021-03-31T07:30:58.705+00:00"},"s":"I", "c":"CONTROL", "id":23377, "ctx":"SignalHandler","msg":"Received signal","attr":{"signal":15,"error":"Terminated"}}

{"t":{"$date":"2021-03-31T07:30:58.705+00:00"},"s":"I", "c":"CONTROL", "id":23378, "ctx":"SignalHandler","msg":"Signal was sent by kill(2)","attr":{"pid":84,"uid":999}}

{"t":{"$date":"2021-03-31T07:30:58.705+00:00"},"s":"I", "c":"CONTROL", "id":23381, "ctx":"SignalHandler","msg":"will terminate after current cmd ends"}

{"t":{"$date":"2021-03-31T07:30:58.705+00:00"},"s":"I", "c":"REPL", "id":4784900, "ctx":"SignalHandler","msg":"Stepping down the ReplicationCoordinator for shutdown","attr":{"waitTimeMillis":10000}}

{"t":{"$date":"2021-03-31T07:30:58.706+00:00"},"s":"I", "c":"COMMAND", "id":4784901, "ctx":"SignalHandler","msg":"Shutting down the MirrorMaestro"}

{"t":{"$date":"2021-03-31T07:30:58.706+00:00"},"s":"I", "c":"SHARDING", "id":4784902, "ctx":"SignalHandler","msg":"Shutting down the WaitForMajorityService"}

{"t":{"$date":"2021-03-31T07:30:58.706+00:00"},"s":"I", "c":"CONTROL", "id":4784903, "ctx":"SignalHandler","msg":"Shutting down the LogicalSessionCache"}

{"t":{"$date":"2021-03-31T07:30:58.706+00:00"},"s":"I", "c":"NETWORK", "id":20562, "ctx":"SignalHandler","msg":"Shutdown: going to close listening sockets"}

{"t":{"$date":"2021-03-31T07:30:58.706+00:00"},"s":"I", "c":"NETWORK", "id":23017, "ctx":"listener","msg":"removing socket file","attr":{"path":"/tmp/mongodb-27017.sock"}}

{"t":{"$date":"2021-03-31T07:30:58.707+00:00"},"s":"I", "c":"NETWORK", "id":4784905, "ctx":"SignalHandler","msg":"Shutting down the global connection pool"}

{"t":{"$date":"2021-03-31T07:30:58.707+00:00"},"s":"I", "c":"STORAGE", "id":4784906, "ctx":"SignalHandler","msg":"Shutting down the FlowControlTicketholder"}

{"t":{"$date":"2021-03-31T07:30:58.707+00:00"},"s":"I", "c":"-", "id":20520, "ctx":"SignalHandler","msg":"Stopping further Flow Control ticket acquisitions."}

{"t":{"$date":"2021-03-31T07:30:58.707+00:00"},"s":"I", "c":"STORAGE", "id":4784908, "ctx":"SignalHandler","msg":"Shutting down the PeriodicThreadToAbortExpiredTransactions"}

{"t":{"$date":"2021-03-31T07:30:58.707+00:00"},"s":"I", "c":"STORAGE", "id":4784934, "ctx":"SignalHandler","msg":"Shutting down the PeriodicThreadToDecreaseSnapshotHistoryCachePressure"}

{"t":{"$date":"2021-03-31T07:30:58.707+00:00"},"s":"I", "c":"REPL", "id":4784909, "ctx":"SignalHandler","msg":"Shutting down the ReplicationCoordinator"}

{"t":{"$date":"2021-03-31T07:30:58.707+00:00"},"s":"I", "c":"SHARDING", "id":4784910, "ctx":"SignalHandler","msg":"Shutting down the ShardingInitializationMongoD"}

{"t":{"$date":"2021-03-31T07:30:58.707+00:00"},"s":"I", "c":"REPL", "id":4784911, "ctx":"SignalHandler","msg":"Enqueuing the ReplicationStateTransitionLock for shutdown"}

{"t":{"$date":"2021-03-31T07:30:58.707+00:00"},"s":"I", "c":"-", "id":4784912, "ctx":"SignalHandler","msg":"Killing all operations for shutdown"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"-", "id":4695300, "ctx":"SignalHandler","msg":"Interrupted all currently running operations","attr":{"opsKilled":3}}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"COMMAND", "id":4784913, "ctx":"SignalHandler","msg":"Shutting down all open transactions"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"REPL", "id":4784914, "ctx":"SignalHandler","msg":"Acquiring the ReplicationStateTransitionLock for shutdown"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"INDEX", "id":4784915, "ctx":"SignalHandler","msg":"Shutting down the IndexBuildsCoordinator"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"REPL", "id":4784916, "ctx":"SignalHandler","msg":"Reacquiring the ReplicationStateTransitionLock for shutdown"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"REPL", "id":4784917, "ctx":"SignalHandler","msg":"Attempting to mark clean shutdown"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"NETWORK", "id":4784918, "ctx":"SignalHandler","msg":"Shutting down the ReplicaSetMonitor"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"SHARDING", "id":4784921, "ctx":"SignalHandler","msg":"Shutting down the MigrationUtilExecutor"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"CONTROL", "id":4784925, "ctx":"SignalHandler","msg":"Shutting down free monitoring"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"CONTROL", "id":20609, "ctx":"SignalHandler","msg":"Shutting down free monitoring"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"STORAGE", "id":4784927, "ctx":"SignalHandler","msg":"Shutting down the HealthLog"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"STORAGE", "id":4784929, "ctx":"SignalHandler","msg":"Acquiring the global lock for shutdown"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"STORAGE", "id":4784930, "ctx":"SignalHandler","msg":"Shutting down the storage engine"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"STORAGE", "id":20282, "ctx":"SignalHandler","msg":"Deregistering all the collections"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"STORAGE", "id":22261, "ctx":"SignalHandler","msg":"Timestamp monitor shutting down"}

{"t":{"$date":"2021-03-31T07:30:58.708+00:00"},"s":"I", "c":"STORAGE", "id":22317, "ctx":"SignalHandler","msg":"WiredTigerKVEngine shutting down"}

{"t":{"$date":"2021-03-31T07:30:58.710+00:00"},"s":"I", "c":"STORAGE", "id":22318, "ctx":"SignalHandler","msg":"Shutting down session sweeper thread"}

{"t":{"$date":"2021-03-31T07:30:58.711+00:00"},"s":"I", "c":"STORAGE", "id":22319, "ctx":"SignalHandler","msg":"Finished shutting down session sweeper thread"}

{"t":{"$date":"2021-03-31T07:30:58.711+00:00"},"s":"I", "c":"STORAGE", "id":22320, "ctx":"SignalHandler","msg":"Shutting down journal flusher thread"}

{"t":{"$date":"2021-03-31T07:30:58.711+00:00"},"s":"I", "c":"STORAGE", "id":22321, "ctx":"SignalHandler","msg":"Finished shutting down journal flusher thread"}

{"t":{"$date":"2021-03-31T07:30:58.711+00:00"},"s":"I", "c":"STORAGE", "id":22322, "ctx":"SignalHandler","msg":"Shutting down checkpoint thread"}

{"t":{"$date":"2021-03-31T07:30:58.711+00:00"},"s":"I", "c":"STORAGE", "id":22323, "ctx":"SignalHandler","msg":"Finished shutting down checkpoint thread"}

{"t":{"$date":"2021-03-31T07:30:58.711+00:00"},"s":"I", "c":"STORAGE", "id":4795902, "ctx":"SignalHandler","msg":"Closing WiredTiger","attr":{"closeConfig":"leak_memory=true,"}}

{"t":{"$date":"2021-03-31T07:31:02.898+00:00"},"s":"E", "c":"STORAGE", "id":22435, "ctx":"thread1","msg":"WiredTiger error","attr":{"error":95,"message":"[1617175862:898369][30:0x7f2660703700], log-server: __posix_std_fallocate, 58: /data/db/journal/WiredTigerTmplog.0000000003: fallocate:: Operation not supported"}}

{"t":{"$date":"2021-03-31T07:31:02.898+00:00"},"s":"E", "c":"STORAGE", "id":22435, "ctx":"thread1","msg":"WiredTiger error","attr":{"error":95,"message":"[1617175862:898447][30:0x7f2660703700], log-server: __posix_sys_fallocate, 75: /data/db/journal/WiredTigerTmplog.0000000003: fallocate:: Operation not supported"}}

{"t":{"$date":"2021-03-31T07:31:07.244+00:00"},"s":"I", "c":"STORAGE", "id":4795901, "ctx":"SignalHandler","msg":"WiredTiger closed","attr":{"durationMillis":8533}}

{"t":{"$date":"2021-03-31T07:31:07.244+00:00"},"s":"I", "c":"STORAGE", "id":22279, "ctx":"SignalHandler","msg":"shutdown: removing fs lock..."}

{"t":{"$date":"2021-03-31T07:31:07.245+00:00"},"s":"I", "c":"-", "id":4784931, "ctx":"SignalHandler","msg":"Dropping the scope cache for shutdown"}

{"t":{"$date":"2021-03-31T07:31:07.245+00:00"},"s":"I", "c":"FTDC", "id":4784926, "ctx":"SignalHandler","msg":"Shutting down full-time data capture"}

{"t":{"$date":"2021-03-31T07:31:07.245+00:00"},"s":"I", "c":"FTDC", "id":20626, "ctx":"SignalHandler","msg":"Shutting down full-time diagnostic data capture"}

{"t":{"$date":"2021-03-31T07:31:07.256+00:00"},"s":"I", "c":"CONTROL", "id":20565, "ctx":"SignalHandler","msg":"Now exiting"}

{"t":{"$date":"2021-03-31T07:31:07.256+00:00"},"s":"I", "c":"CONTROL", "id":23138, "ctx":"SignalHandler","msg":"Shutting down","attr":{"exitCode":0}}


MongoDB init process complete; ready for start up.


{"t":{"$date":"2021-03-31T07:31:07.726+00:00"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}

{"t":{"$date":"2021-03-31T07:31:07.729+00:00"},"s":"W", "c":"ASIO", "id":22601, "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}

{"t":{"$date":"2021-03-31T07:31:07.729+00:00"},"s":"I", "c":"NETWORK", "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}

{"t":{"$date":"2021-03-31T07:31:07.730+00:00"},"s":"I", "c":"STORAGE", "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"ca04a1bdd684"}}

{"t":{"$date":"2021-03-31T07:31:07.730+00:00"},"s":"I", "c":"CONTROL", "id":23403, "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.4","gitVersion":"8db30a63db1a9d84bdcad0c83369623f708e0397","openSSLVersion":"OpenSSL 1.1.1 11 Sep 2018","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu1804","distarch":"x86_64","target_arch":"x86_64"}}}}

{"t":{"$date":"2021-03-31T07:31:07.730+00:00"},"s":"I", "c":"CONTROL", "id":51765, "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"18.04"}}}

{"t":{"$date":"2021-03-31T07:31:07.730+00:00"},"s":"I", "c":"CONTROL", "id":21951, "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"*"},"security":{"authorization":"enabled"}}}}

{"t":{"$date":"2021-03-31T07:31:07.740+00:00"},"s":"I", "c":"STORAGE", "id":22270, "ctx":"initandlisten","msg":"Storage engine to use detected by data files","attr":{"dbpath":"/data/db","storageEngine":"wiredTiger"}}

{"t":{"$date":"2021-03-31T07:31:07.746+00:00"},"s":"I", "c":"STORAGE", "id":22315, "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=4220M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],"}}

{"t":{"$date":"2021-03-31T07:31:08.290+00:00"},"s":"E", "c":"STORAGE", "id":22435, "ctx":"initandlisten","msg":"WiredTiger error","attr":{"error":95,"message":"[1617175868:290197][1:0x7fc31420dac0], connection: __posix_std_fallocate, 58: /data/db/journal/WiredTigerTmplog.0000000001: fallocate:: Operation not supported"}}

{"t":{"$date":"2021-03-31T07:31:08.290+00:00"},"s":"E", "c":"STORAGE", "id":22435, "ctx":"initandlisten","msg":"WiredTiger error","attr":{"error":95,"message":"[1617175868:290294][1:0x7fc31420dac0], connection: __posix_sys_fallocate, 75: /data/db/journal/WiredTigerTmplog.0000000001: fallocate:: Operation not supported"}}

{"t":{"$date":"2021-03-31T07:31:12.592+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1617175872:592855][1:0x7fc31420dac0], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 1 through 2"}}

{"t":{"$date":"2021-03-31T07:31:13.221+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1617175873:221071][1:0x7fc31420dac0], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 2 through 2"}}

{"t":{"$date":"2021-03-31T07:31:13.841+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1617175873:841697][1:0x7fc31420dac0], txn-recover: [WT_VERB_RECOVERY | WT_VERB_RECOVERY_PROGRESS] Main recovery loop: starting at 1/29952 to 2/256"}}

{"t":{"$date":"2021-03-31T07:31:15.188+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1617175875:188227][1:0x7fc31420dac0], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 1 through 2"}}

{"t":{"$date":"2021-03-31T07:31:16.064+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1617175876:64206][1:0x7fc31420dac0], txn-recover: [WT_VERB_RECOVERY_PROGRESS] Recovering log 2 through 2"}}

{"t":{"$date":"2021-03-31T07:31:16.694+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1617175876:694002][1:0x7fc31420dac0], txn-recover: [WT_VERB_RECOVERY | WT_VERB_RECOVERY_PROGRESS] Set global recovery timestamp: (0, 0)"}}

{"t":{"$date":"2021-03-31T07:31:16.694+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1617175876:694073][1:0x7fc31420dac0], txn-recover: [WT_VERB_RECOVERY | WT_VERB_RECOVERY_PROGRESS] Set global oldest timestamp: (0, 0)"}}

{"t":{"$date":"2021-03-31T07:31:16.901+00:00"},"s":"I", "c":"STORAGE", "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":9155}}

{"t":{"$date":"2021-03-31T07:31:16.901+00:00"},"s":"I", "c":"RECOVERY", "id":23987, "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}

{"t":{"$date":"2021-03-31T07:31:16.903+00:00"},"s":"I", "c":"STORAGE", "id":4366408, "ctx":"initandlisten","msg":"No table logging settings modifications are required for existing WiredTiger tables","attr":{"loggingEnabled":true}}

{"t":{"$date":"2021-03-31T07:31:16.905+00:00"},"s":"I", "c":"STORAGE", "id":22262, "ctx":"initandlisten","msg":"Timestamp monitor starting"}

{"t":{"$date":"2021-03-31T07:31:16.909+00:00"},"s":"E", "c":"STORAGE", "id":22435, "ctx":"thread1","msg":"WiredTiger error","attr":{"error":95,"message":"[1617175876:909152][1:0x7fc30a503700], log-server: __posix_std_fallocate, 58: /data/db/journal/WiredTigerTmplog.0000000002: fallocate:: Operation not supported"}}

{"t":{"$date":"2021-03-31T07:31:16.909+00:00"},"s":"E", "c":"STORAGE", "id":22435, "ctx":"thread1","msg":"WiredTiger error","attr":{"error":95,"message":"[1617175876:909306][1:0x7fc30a503700], log-server: __posix_sys_fallocate, 75: /data/db/journal/WiredTigerTmplog.0000000002: fallocate:: Operation not supported"}}

{"t":{"$date":"2021-03-31T07:31:16.911+00:00"},"s":"W", "c":"CONTROL", "id":22178, "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}

{"t":{"$date":"2021-03-31T07:31:16.918+00:00"},"s":"I", "c":"STORAGE", "id":20536, "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}

{"t":{"$date":"2021-03-31T07:31:16.921+00:00"},"s":"I", "c":"FTDC", "id":20625, "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"/data/db/diagnostic.data"}}

{"t":{"$date":"2021-03-31T07:31:16.924+00:00"},"s":"I", "c":"NETWORK", "id":23015, "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}

{"t":{"$date":"2021-03-31T07:31:16.924+00:00"},"s":"I", "c":"NETWORK", "id":23015, "ctx":"listener","msg":"Listening on","attr":{"address":"0.0.0.0"}}

{"t":{"$date":"2021-03-31T07:31:16.924+00:00"},"s":"I", "c":"NETWORK", "id":23016, "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}

{"t":{"$date":"2021-03-31T07:31:21.738+00:00"},"s":"E", "c":"STORAGE", "id":22435, "ctx":"thread1","msg":"WiredTiger error","attr":{"error":95,"message":"[1617175881:738153][1:0x7fc30a503700], log-server: __posix_std_fallocate, 58: /data/db/journal/WiredTigerTmplog.0000000003: fallocate:: Operation not supported"}}

{"t":{"$date":"2021-03-31T07:31:21.738+00:00"},"s":"E", "c":"STORAGE", "id":22435, "ctx":"thread1","msg":"WiredTiger error","attr":{"error":95,"message":"[1617175881:738233][1:0x7fc30a503700], log-server: __posix_sys_fallocate, 75: /data/db/journal/WiredTigerTmplog.0000000003: fallocate:: Operation not supported"}}

{"t":{"$date":"2021-03-31T07:32:14.341+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:43120","connectionId":1,"connectionCount":1}}

{"t":{"$date":"2021-03-31T07:32:14.342+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn1","msg":"client metadata","attr":{"remote":"127.0.0.1:43120","client":"conn1","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.4"},"os":{"type":"Linux","name":"Ubuntu","architecture":"x86_64","version":"18.04"}}}}

{"t":{"$date":"2021-03-31T07:32:33.098+00:00"},"s":"I", "c":"ACCESS", "id":20251, "ctx":"conn1","msg":"Supported SASL mechanisms requested for unknown user","attr":{"user":"root@test"}}

{"t":{"$date":"2021-03-31T07:32:33.099+00:00"},"s":"I", "c":"ACCESS", "id":20249, "ctx":"conn1","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-1","principalName":"root","authenticationDatabase":"test","client":"127.0.0.1:43120","result":"UserNotFound: Could not find user \"root\" for db \"test\""}}

2 个答案:

答案 0 :(得分:1)

在 MongoDB 中,用户存储在数据库中,但用户有权访问的一个(或多个)数据库不需要与该用户所在的数据库相同存储

存储用户的数据库称为身份验证数据库。这是通过 authSource URI 选项和各种特定于语言的驱动程序选项以及 --authenticationDatabase mongo shell 选项进行配置的。

错误消息表明您正在针对 test 数据库进行身份验证。您之前的 shell 命令显示了对 admin 数据库进行身份验证的尝试。

检查用户是在哪个数据库中创建的,并确保在身份验证期间使用相同的数据库。

答案 1 :(得分:0)

我的问题是我忘记了 MongoDB 已经在我的本地机器上运行,所以我不得不将 27017 映射到不同的端口。通常这是行不通的,但不知何故它不会在启动时引起问题,这使我无法使用用户名和密码进行连接。

我现在正在使用这个命令:

docker run -d --name mongodb -p 8081:27017 -e MONGO_INITDB_ROOT_USERNAME="root" -e MONGO_INITDB_ROOT_PASSWORD="password" -v C:\projects\docker\volumes\mongotmp:/data/db mongo:4.4.4

使用此连接字符串:

mongodb://root:password@localhost:8081/?authSource=admin