Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MySQL SSL Connection Error when trying to use conn.Open()

Tags:

c#

mysql

For the past 3 days now I have been struggling with an issue with MySQL connector in C#. Basically, I follow the MySQLConnector tutorial to open my connection in order to send data, but when I get to the MySQLConnection.Open() method, my code throws a SSL Connection error. Here is the code:

public async void SQLInsert()
        {
            string connStr = "Server=echstreme.de;User=c1Look;Database=c1Look;Port=3306;Password=redacted;";
            MySqlConnection conn = new MySqlConnection(connStr);

            try
            {
                await SendEmbedMessage("MySQL", "Attempting to connect to database");
                conn.Open();
                await SendEmbedMessage("MySQL", "Connection to Database Successful");
                MySqlCommand cmd = new MySqlCommand(sqlQuery, conn);

                cmd.ExecuteNonQuery();


                conn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

I have tried using both the Domain "echstreme.de" and the IP address, however both throw similar errors.

EDIT: THIS HAS BEEN FIXED Fix => "Server=echstreme.de;Port=3306;Database=c1Look;Uid=c1Look;Pwd=;SSL Mode =None";

Here is the complete error message for anyone needing it

MySql.Data.MySqlClient.MySqlException (0x80004005): SSL Connection error.
 ---> System.AggregateException: One or more errors occurred. (The handshake failed due to an unexpected packet format.)
 ---> System.IO.IOException: The handshake failed due to an unexpected packet format.
   at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslStream.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Security.SslStream.ThrowIfExceptional()
   at System.Net.Security.SslStream.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
   at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__64_2(IAsyncResult iar)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at MySql.Data.Common.Ssl.StartSSL(Stream& baseStream, Encoding encoding, String connectionString)
   at MySql.Data.Common.Ssl.StartSSL(Stream& baseStream, Encoding encoding, String connectionString)
   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()
   at Bot.Admins.AdminCommands.SQLInsert() in C:\Users\Owner\source\repos\Bot\Bot\Admins\AdminCommands.cs:line 71
like image 730
Luke Avatar asked Dec 07 '22 09:12

Luke


1 Answers

If your host doesn't support SSL you can use SSL Mode options, None or Required.

Connection string must be like this

"Server=echstreme.de;User=c1Look;Database=c1Look;Port=3306;Password=redacted;SSL Mode=None"
like image 195
Batuhan Avatar answered Dec 09 '22 22:12

Batuhan