Problem with mysql connection to local MariaDB MySQL Linux server via localhost/127.0.0.1 in c# mono app

0

I made a plugin to collect data from the players on my SCP:SL (game) server and saving it to MySQL database.

When connect to external databases, located outside my local VPS(like freemysqlhosting), it works perfectly.

My local VPS is running 10.1.37-MariaDB-0+deb9u1 Debian 9.6 MySQL Server with the newest mono version.

The problem starts when I am trying to connect to my MariaDB Linux MySQL database locally (via localhost/127.0.0.1). When it comes to connect to the database, I am getting two different errors:

  • one for localhost(the app isn't crashing),
  • one for 127.0.0.1(the app is crashing)

I am getting this error, when host is specified as "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 

The error, when I am specifying host as "127.0.0.1" looks like that (it is so long so i attach a pastebin of it)

https://pastebin.com/Xf1hn3in

This is the c# mysql connection code for my plugin, I am using MySQLConnector v 8.0.13 to connect to the database:

                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();

The expected result is to have a working connection to the local database, which is not working due to the errors.

Those errors are weird, because I can connect to the external databases with ease, the local connection seems to be a problem, and I do not know what to do.

c#
mysql
mysql-connector
asked on Stack Overflow Jan 30, 2019 by SandeQ • edited Jan 30, 2019 by Strom

1 Answer

0

The exception from using "localhost" is SocketException: An address incompatible with the requested protocol was used. The cause is that Connector/NET doesn't support IPv6: bug 83649.

The normal workaround would be to use an IPv4 address, e.g., "127.0.0.1". However, I'm not sure about the cause of the new exception you get when you try that. It may be that Connector/NET isn't fully compatible with Mono?

I suggest trying MySqlConnector, an OSS alternative MySQL library for .NET, to see if it resolves both these issues. It fixes a lot of other bugs in Connector/NET, and should be a drop-in replacement.

answered on Stack Overflow Jan 30, 2019 by Bradley Grainger • edited Jul 13, 2019 by Bradley Grainger

User contributions licensed under CC BY-SA 3.0