Bug #109331 MySQL Connector fails to connect to MariaDB 10.10.2
Submitted: 11 Dec 2022 5:39 Modified: 22 Dec 2022 12:41
Reporter: Ivaylo Ivanov Email Updates:
Status: Unsupported Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:8.0.31 OS:Windows
Assigned to: CPU Architecture:Any
Tags: Connector/Net, MariaDB

[11 Dec 2022 5:39] Ivaylo Ivanov
Description:
When connecting to MariaDB 10.10.2 MySqlConnection.Open() throws exception

System.InvalidCastException: Object cannot be cast from DBNull to other types.
   at System.DBNull.System.IConvertible.ToInt32(IFormatProvider provider)
   at System.Convert.ToInt32(Object value, IFormatProvider provider)
   at MySql.Data.MySqlClient.Driver.LoadCharacterSets(MySqlConnection connection)
   at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at ...

Using the latest Connector/NET 8.0.31 and MariaDB 10.10.2
but affect all versions of Connector/NET, likely caused by MariaDB

Starting with MariaDB version 10.10.1 the Information_Schema.COLLATIONS
table includes records with null ID   
https://jira.mariadb.org/browse/MDEV-27009

MySqlConnection.Open() calls Driver.Configure()
Driver.Configure() calls Driver.LoadCharacterSets()
Driver.LoadCharacterSets() executes 'SHOW COLLATION' 
 expecting the ID to be an integer:

  MySqlCommand cmd = new MySqlCommand("SHOW COLLATION", connection);
  // now we load all the currently active collations
  try
  {
    using (MySqlDataReader reader = cmd.ExecuteReader())
    {
      CharacterSets = new Dictionary<int, string>();
      while (reader.Read())
      {
        CharacterSets[ Convert.ToInt32(reader["id"],
                       NumberFormatInfo.InvariantInfo)] =
          reader.GetString(reader.GetOrdinal("charset"));
      }
    }
    ...

How to repeat:

Try to open connection to MariaDB version 10.10.1 or later

Suggested fix:

Handle 'SHOW COLLATION' results that have null id
[11 Dec 2022 13:33] Frederic Descamps
Hi Ivaylo, 

MySQL and MariaDB are diverging products and compatibility is no longer guaranteed and is not a common objective of both projects. 
MariaDB is not a drop-in replacement of MySQL anymore.

Storage engines, protocol, etc.. all are going in different directions.

Therefore MySQL Connectors are developed with MySQL Server (Community, Enterprise) focus.

Regards,
[16 Dec 2022 9:30] VINCENT HELLIN
Hi,
Same issue for me.

I just replace the package MySql.Data by MySqlConnector (https://www.nuget.org/packages/MySqlConnector/). And the connection was back again.
[22 Dec 2022 12:41] MySQL Verification Team
Hello Ivaylo Ivanov,

Thank you for the bug report.
As Frederic explained, this is an unsupported issue. Our development is totally focused on MySQL Server.

Regards,
Ashwini Patil