mongodb - Mongodb Docker 镜像与 Asp dotnet core API 连接问题
问题描述
我一步一步地按照文章,首先我使用以下 docker 文件运行了一个 mongo 和 mong-express 图像的实例:
version: '3.8'
services:
mongo:
image: mongo
container_name: mongo
restart: always
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- "8081:8081"
depends_on:
- mongo
volumes:
mongo-data:
driver: local
错误信息
https://localhost:5001/api/messages
An unhandled exception occurred while processing the request.
MongoCommandException: Command saslContinue failed: Authentication failed..
MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ProcessReply(ConnectionId connectionId, ReplyMessage<RawBsonDocument> reply)
MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-256.
MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
Stack Query Cookies Headers Routing
MongoCommandException: Command saslContinue failed: Authentication failed..
MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ProcessReply(ConnectionId connectionId, ReplyMessage<RawBsonDocument> reply)
MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
MongoDB.Driver.Core.Authentication.SaslAuthenticator.AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
Show raw exception details
MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-256.
MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
MongoDB.Driver.Core.Servers.Server.GetChannelAsync(CancellationToken cancellationToken)
MongoDB.Driver.Core.Operations.RetryableReadContext.InitializeAsync(CancellationToken cancellationToken)
MongoDB.Driver.Core.Operations.RetryableReadContext.CreateAsync(IReadBinding binding, bool retryRequested, CancellationToken cancellationToken)
MongoDB.Driver.Core.Operations.FindOperation<TDocument>.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync<TResult>(IReadBinding binding, IReadOperation<TResult> operation, CancellationToken cancellationToken)
MongoDB.Driver.MongoCollectionImpl<TDocument>.ExecuteReadOperationAsync<TResult>(IClientSessionHandle session, IReadOperation<TResult> operation, ReadPreference readPreference, CancellationToken cancellationToken)
MongoDB.Driver.MongoCollectionImpl<TDocument>.UsingImplicitSessionAsync<TResult>(Func<IClientSessionHandle, Task<TResult>> funcAsync, CancellationToken cancellationToken)
MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync<TDocument>(IAsyncCursorSource<TDocument> source, CancellationToken cancellationToken)
ErrorMessagesAPI.Models.MessageRepository.GetAllMessages() in MessageRepository.cs
+
return await _context
ErrorMessagesAPI.Controllers.MessagesController.Get() in MessagesController.cs
+
return new ObjectResult(await _repo.GetAllMessages());
lambda_method(Closure , object )
Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable+Awaiter.GetResult()
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask<IActionResult> actionResultValueTask)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
在 Monog DB 中,我创建了一个名为 messagesdb 的数据库,其中包含一个名为 messages 的空集合。
集合语法是这样的:
{
"text": "hello",
"createdAt": "2015-11-08T13:15:15.363Z",
"updatedAt": "2015-11-08T13:15:15.363Z",
"id": "5638b363c5cd0825511690bd"
}
然后可以在这里找到完整的实现ErrorMessageAPI GitHub Repo
任何有 Docker 和 .NET 核心专家的人都可以帮助我吗?
解决方案
如果您需要对 MongoDB 进行身份验证,则必须在 docker 文件中提供用户名和密码:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root //This
MONGO_INITDB_ROOT_PASSWORD: example //This
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root //This
ME_CONFIG_MONGODB_ADMINPASSWORD: example //This
depends_on:
- mongo
您尚未创建登录,但尝试使用用户名和密码访问 MongoDB。
推荐阅读
- javascript - Ajax 没有获取最新版本的 JSON 文件
- python - 在 python sklearn 中对一维数组使用高斯混合
- perl - HTTP::Request 在标头中用连字符替换下划线
- graphql - GitHub GraphQL api 说还有另一个页面,但是使用游标时找不到结果
- r - 比较数据框中的 xy 坐标
- python - 不使用 async-await 的 Python 代码
- javascript - 操作图标未显示 Krajee 文件输入
- ruby-on-rails - 为什么我在 Rails 网站上的链接使用“GET”动词?
- python - 使用 Google Cloud AutoML 模型预测存储在 Firebase 功能中的 Google Cloud 存储中的图像
- functional-programming - 为什么在 ramda.js 中使用“补码”作为函数的名称