question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: how to connect to Azure MySQL / use SSL certificate

See original GitHub issue

The question

I’m trying to connect to Azure MySQL. There SSL certificate is must-have. So that’s what I’m doing. Connection string looks like this: server=imgeneus-test.mysql.database.azure.com;userid=aosyatnik;pwd=password;port=3306;database=whatever;SSL Mode=Required;SslCA=BaltimoreCyberTrustRoot.crt.pem

BaltimoreCyberTrustRoot.crt.pem is located in the same project directory.

I get next exception, when called DbContext.Migrate():

System.InvalidOperationException: "The collection already contains item with same key 'net.transport''"
   at System.Diagnostics.ActivityTagsCollection.Add(String key, Object value)
   at MySqlConnector.Core.ServerSession.<OpenTcpSocketAsync>d__95.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySqlConnector.Core.ServerSession.<ConnectAsync>d__79.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at MySqlConnector.Core.ConnectionPool.<ConnectSessionAsync>d__21.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at MySqlConnector.Core.ConnectionPool.<GetSessionAsync>d__10.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at MySqlConnector.Core.ConnectionPool.<GetSessionAsync>d__10.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at MySqlConnector.MySqlConnection.<CreateSessionAsync>d__126.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at MySqlConnector.MySqlConnection.<OpenAsync>d__27.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at MySqlConnector.MySqlConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlRelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__19_0(DbContext _, Enumerator enumerator)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass31_0`2.<Execute>b__0(DbContext context, TState state)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at Imgeneus.Login.Pages.Index.GetNumberOfRegisteredUsers() in C:\Projects\Shaiya\Imgeneus\src\Imgeneus.Login\Pages\Index.razor:line 25

The same code works fine on localhost.

Further technical details

MySQL version: 8.0 Operating system: Win 10 Pomelo.EntityFrameworkCore.MySql version: 6.0.0 Microsoft.AspNetCore.App version: .NET 6.0

P.S. sorry, if it’s wrong repo to ask.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
aosyatnikcommented, Jun 23, 2022

There’s a similar issue in the underlying repo at mysql-net/MySqlConnector#1074 which offers a solution.

You should be able to work around it by adding ; TLS Version = TLS 1.1 to your connection string

However, the Azure documentation at Connect to Azure Database for MySQL - Flexible Server with encrypted connections mentions the following.

Azure Database for MySQL Flexible Server supports encrypted connections using Transport Layer Security (TLS 1.2) by default and all incoming connections with TLS 1.0 and TLS 1.1 will be denied by default.

So try adding TlsVersion=TLS 1.2 to your connection string. @bgrainger Any thoughts/suggestions?

Adding TlsVersion=TLS 1.2 fixed issue. Thank you very much!

0reactions
mguinnesscommented, Jun 22, 2022

Try updating to Pomelo 6.0.1 which will bring in MySqlConnector 2.1.2 (instead of 2.0.0 currently). If that doesn’t work, you can manually add the latest MySqlConnector package in your csproj file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to connect MySQL with SSL certificate in Azure Data ...
To connect to MySQL, we have SSL Certifcate. But while creating the linked service, I am not getting any option to upload the...
Read more >
Use SSL to securely access MySQL Database on Azure
Step 1: Download the certificate locally · Step 2: Download and install OpenSSL · Step 3: Move the local certificate file to the...
Read more >
Client Certificate and Key for Azure Database for MySQL
I want to use an SSL connection for my MySQL databases in Azure, but I can't find out how to get a Client...
Read more >
Need help Connecting PowerBI desktop to Azure MySQL with ...
Need help Connecting PowerBI desktop to Azure MySQL with SSL · Get Data > More > Select MySQL Database · Input the Server...
Read more >
How to Create a SSL Key and a SSL Cert from a Given CA ...
I Need to create a encrypted db connection to the Azure Cloud MySQL Servers, They have given us a CA certificate called "BaltimoreCyberTrustRoot ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found