.net MVC Web 应用程序中的 MySqlClient 尝试作为请求主机进行连接

MySqlClient within .net MVC web app trying to connect as requesting host

我对网络应用程序非常陌生,所以如果问题是由于我的天真造成的,我深表歉意,但是,我完全不知所措。

我在 Apache2 后面的 ubuntu 20.4 服务器上有一个基本的 ASP.net 核心 MVC 应用程序 运行,每次应用程序收到请求时,Mysqlclient returns 抛出一个例外情况:

MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host '' for user '' using method 'sha256_password' failed with message: Access denied for user ''@'{the requesting IP}' (using password: NO)

我期望传递给客户端的连接字符串实际上传递了所有正确的参数,这已经在我的本地机器 (Win10) 上进行了测试。通过 运行 MySQL 的本地实例和远程连接到 ubuntu 服务器上的 MySQL 服务器,两个实例都正常运行。

我唯一的假设是 MysqlClient 以某种方式尝试使用 ''@'{My IP}' 的网页请求者凭据,这显然会导致访问被拒绝,但我不确定如何或为什么会这样正在发生。

我的连接字符串是:

"SERVER=127.0.0.1; DATABASE=somedb; UID=someuser; PASSWORD=SomePassword;"

然后我的连接请求是用下面的,没有对默认的 MySqlConnection 对象进行任何修改:

private static MySqlConnection dbConnection;
dbConnection = new MySqlConnection(connectionString);

我从应用程序中得到的错误是:

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
      An unhandled exception has occurred while executing the request.
MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host '' for user '' using method 'sha256_password' failed with message: Access denied for user ''@'{my IP}' (using password: NO)
 ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user ''@'{my IP}' (using password: NO)
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(Exception ex)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ContinueAuthentication(Byte[] data)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.HandleAuthChange(MySqlPacket packet)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset)
   at MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset)
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()

我认为您的连接字符串可能不正确,我不是 MySql 专家,但根据一些 Google 结果 'PASSWORD' 应该是 'Pwd',并且字段是 PascalCase。 您可能还需要指定一个 TCP 端口。

示例连接字符串:

Server=127.0.0.1;Port=1234;Database=somedb;Uid=someuser;Pwd=somePassword;