weixin_39805924
weixin_39805924
2021-01-11 05:13

MySqlException: Access denied for user 'root'@'192.168.1.84' (using password: NO)

1.stack trace:

MySqlException: Access denied for user 'root'@'192.168.1.84' (using password: NO) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable+ConfiguredValueTaskAwaiter.GetResult() MySqlConnector.Core.ServerSession+<ConnectAsync>d__56.MoveNext() in ServerSession.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) MySqlConnector.Core.ConnectionPool+<GetSessionAsync>d__3.MoveNext() in ConnectionPool.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) MySql.Data.MySqlClient.MySqlConnection+<CreateSessionAsync>d__81.MoveNext() in MySqlConnection.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) MySql.Data.MySqlClient.MySqlConnection+<OpenAsync>d__23.MoveNext() in MySqlConnection.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) MySql.Data.MySqlClient.MySqlConnection.Open() in MySqlConnection.cs DotNetCore.CAP.MySql.MySqlStorage.CreateAndOpenConnection() DotNetCore.CAP.MySql.MySqlStorage.UseConnection<T>(Func<IDbConnection, T> func) DotNetCore.CAP.Dashboard.Pages.HomePage.Execute() DotNetCore.CAP.Dashboard.RazorPage.TransformText(string body) DotNetCore.CAP.Dashboard.RazorPageDispatcher.Dispatch(DashboardContext context) DotNetCore.CAP.DashboardMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()

2.关键代码:

`services.AddDbContextPool(options => options.UseMySql(GetConnection()));

        services.AddCap(x =>
        {
            x.UseDashboard();
            x.UseEntityFramework<StudyDBContext>();
            //x.UseMySql(Configuration["Conn"]);
            x.UseRabbitMQ(d => { d.HostName = "rabbit"; d.UserName = "guest"; d.Password = "guest"; });
        });

//使用CoreProfiler来封装connection private DbConnection GetConnection() { return new ProfiledDbConnection(new MySqlConnection(Configuration["Conn"]), () => { if (ProfilingSession.Current == null) return null;

            return new DbProfiler(ProfilingSession.Current.Profiler);
        });
    }`

3.组合使用中,可能会影响到的类库: (1)CoreProfiler.Web https://www.nuget.org/packages/CoreProfiler.Web/ (2)AspectCore.Core https://www.nuget.org/packages/AspectCore.Core/

4.移除无关代码后的完整startup.cs的代码

`public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }

    public IConfiguration Configuration { get; }

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {

        services.AddMvc();
        services.AddDbContextPool<StudyDBContext>(options => options.UseMySql(GetConnection()));

        services.AddCap(x =>
        {
            //访问地址为/cap,来访问控制面板
            x.UseDashboard();
            x.UseEntityFramework<StudyDBContext>();
            //x.UseMySql(Configuration["Conn"]);
            x.UseRabbitMQ(d => { d.HostName = "rabbit"; d.UserName = "guest"; d.Password = "guest"; });
        });
        var container = services.ToServiceContainer();
        container.AddType<DbContext, StudyDBContext>(Lifetime.Scoped);
        container.AddType<IUserRepository, UserRepository>(Lifetime.Transient);
        container.AddType<IDistributedCache, RedisCache>(Lifetime.Transient);
        return container.Build();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseDeveloperExceptionPage();
        app.UseStaticFiles();
        app.UseCap();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
        app.UseCoreProfiler(true);
    }

    private DbConnection GetConnection()
    {
        return new ProfiledDbConnection(new MySqlConnection(Configuration["Conn"]), () =>
        {
            if (ProfilingSession.Current == null)
                return null;

            return new DbProfiler(ProfilingSession.Current.Profiler);
        });
    }

}`

5.已经尝试过情形: (1)不使用coreprofiler,正常工作 (2)在cap中直接传入连接字符,x.UseMySql(Configuration["Conn"]),有时候能工作,有时会因为检测是否有使用ef而报错 (3)当在aspectcore中使用全局拦截器时,项目会无法启动到首页,不使用cap时能工作,拦截器示例代码如下: //services.AddDynamicProxy(config => //{ // config.Interceptors.AddTyped<CachingInterceptor>(); //}); 如果指定拦截器的类型,则能正常工作 //services.AddDynamicProxy(config => //{ // config.Interceptors.AddTyped<CachingInterceptor>(method => // method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) as CachingAttribute != null); //});

6.不工作的版本 2.1.4 2.2.0-preview-38490295

该提问来源于开源项目:dotnetcore/CAP

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39601642 weixin_39601642 4月前

    这个需要你自己来排查,我没有太多时间来帮助你...

    点赞 评论 复制链接分享
  • weixin_39805924 weixin_39805924 4月前

    那提个建议吧 我不是很清楚cap为啥需要检测ef,这种设计让我觉得有点难受 因为此类库使用到的表也就3个,直接用dapper都可以满足使用需求了吧 在这种略微复杂的情形下,提供一个直接使用连接字符串就可以使用途径是不是更好。 x.UseMySql(Configuration["Conn"]); 毕竟存在对ef做自己的封装的可能性

    点赞 评论 复制链接分享
  • weixin_39601642 weixin_39601642 4月前

    CAP是一个库,她的客户可能是ADO.NET使用者,也可能是EntityFramework使用者,作为框架必须要考虑受众人群。 如果用户是EF,我们为EF用户提供了更加方便的API,这对于使用者来说来更加的友好。

    CAP 作为一个MIT协议开源项目,没有限制任何用户对其进行修改和扩展,针对于特殊需求的客户,完全可以Fork分支来定制,这就是开源项目的好处。

    点赞 评论 复制链接分享
  • weixin_39805924 weixin_39805924 4月前

    我的意思是 哪怕我使用了ef,我也应该可以用 x.UseMySql(Configuration["Conn"]); 来使用cap 不应该因为我用了ef,就不能用这句来操作 我实际测试过,会有影响的

    点赞 评论 复制链接分享