gpt4 book ai didi

c# - 在c# mono应用程序中通过localhost/127.0.0.1 mysql连接到本地MariaDB MySQL Linux服务器的问题

转载 作者:行者123 更新时间:2023-11-29 16:23:00 26 4
gpt4 key购买 nike

我制作了一个插件来收集 SCP:SL(游戏)服务器上玩家的数据并将其保存到 MySQL 数据库。

当连接到位于我的本地 VPS 之外的外部数据库(如 freemysqlhosting)时,它工作得很好。

我的本​​地 VPS 正在运行具有最新单声道版本的 10.1.37-MariaDB-0+deb9u1 Debian 9.6 MySQL Server

当我尝试本地连接到我的 MariaDB Linux MySQL 数据库(通过 localhost/127.0.0.1)时,问题就出现了。当连接到数据库时,我收到两个不同的错误:

  • 一个用于本地主机(应用程序不会崩溃),
  • 一个用于 127.0.0.1(应用崩溃)

当主机指定为“localhost”时,我收到此错误

Unhandled Exception:
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> System.AggregateException: One or more errors occurred. ---> System.Net.Sockets.SocketException: An address incompatible with the requested protocol was used
at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x00014] in <ac210d81537245bc838518cc8e845861>:0
at System.Net.Sockets.Socket.EndConnect (System.IAsyncResult asyncResult) [0x0002c] in <ac210d81537245bc838518cc8e845861>:0
at System.Net.Sockets.TcpClient.EndConnect (System.IAsyncResult asyncResult) [0x0000c] in <ac210d81537245bc838518cc8e845861>:0
at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00019] in <f2e6809acb14476a81f399aeb800f8f2>:0
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout) [0x00000] in <f2e6809acb14476a81f399aeb800f8f2>:0
at MySql.Data.Common.StreamCreator.GetTcpStream (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) [0x0001f] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at MySql.Data.Common.StreamCreator.GetStream (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) [0x00021] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at MySql.Data.MySqlClient.NativeDriver.Open () [0x00007] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
---> (Inner Exception #0) System.Net.Sockets.SocketException (0x80004005): An address incompatible with the requested protocol was used
at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x00014] in <ac210d81537245bc838518cc8e845861>:0
at System.Net.Sockets.Socket.EndConnect (System.IAsyncResult asyncResult) [0x0002c] in <ac210d81537245bc838518cc8e845861>:0
at System.Net.Sockets.TcpClient.EndConnect (System.IAsyncResult asyncResult) [0x0000c] in <ac210d81537245bc838518cc8e845861>:0
at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00019] in <f2e6809acb14476a81f399aeb800f8f2>:0 <---

at MySql.Data.MySqlClient.NativeDriver.Open () [0x00041] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at MySql.Data.MySqlClient.Driver.Open () [0x0000e] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) [0x0004e] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection () [0x00006] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection () [0x0008a] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () [0x0003f] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at MySql.Data.MySqlClient.MySqlPool.GetConnection () [0x0001c] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at MySql.Data.MySqlClient.MySqlConnection.Open () [0x0016d] in <ffa6b95770be4daf8d8ae25ca059b16a>:0
at DataCollector.RoundEventHandler.<OnWaitingForPlayers>g__CheckIfTableExists|3_0 () [0x000db] in <8dda65d2174e4f2a8af95550bb525546>:0
at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00014] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) [0x0002b] in <f2e6809acb14476a81f399aeb800f8f2>:0
at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <f2e6809acb14476a81f399aeb800f8f2>:0

当我将主机指定为“127.0.0.1”时,错误看起来像这样(它太长了,所以我附上了它的pastebin)

https://pastebin.com/Xf1hn3in

这是我的插件的 C# mysql 连接代码,我使用 MySQLConnector v 8.0.13 连接到数据库:

                MySqlConnectionStringBuilder connstr = new MySqlConnectionStringBuilder();
DatabaseConnInfo.dbhost = _plugin.GetConfigString("dc_dbhost");
DatabaseConnInfo.dbname = _plugin.GetConfigString("dc_dbname");
DatabaseConnInfo.dbuser = _plugin.GetConfigString("dc_dbuser");
DatabaseConnInfo.dbpass = _plugin.GetConfigString("dc_dbpass");
DatabaseConnInfo.dbport = (uint)_plugin.GetConfigInt("dc_dbport");

connstr.Server = DatabaseConnInfo.dbhost;
connstr.Database = DatabaseConnInfo.dbname;
connstr.UserID = DatabaseConnInfo.dbuser;
connstr.Password = DatabaseConnInfo.dbpass;
connstr.Port = DatabaseConnInfo.dbport;
_plugin.Info(connstr.ToString());
MySqlConnection conn = new MySqlConnection(connstr.ToString());
conn.Open();
string sql = "some sql statement";
MySqlCommand comm = new MySqlCommand(sql, conn);
conn.Close();

预期结果是与本地数据库建立有效连接,但由于错误而无法正常工作。

这些错误很奇怪,因为我可以轻松连接到外部数据库,本地连接似乎有问题,我不知道该怎么办。

最佳答案

使用“localhost”的异常是SocketException:使用了与请求的协议(protocol)不兼容的地址。原因是 Connector/NET 不支持 IPv6:bug 83649 .

正常的解决方法是使用 IPv4 地址,例如“127.0.0.1”。但是,我不确定您尝试时遇到的新异常的原因。可能是 Connector/NET 与 Mono 不完全兼容?

我建议尝试MySqlConnector ,一个用于 .NET 的 OSS 替代 MySQL 库,看看它是否可以解决这两个问题。它fixes a lot of other bugs Connector/NET 中,并且应该是直接替代品。

关于c# - 在c# mono应用程序中通过localhost/127.0.0.1 mysql连接到本地MariaDB MySQL Linux服务器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54431850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com