MySQL Connector/Net with SSL from WCF Service fails - mysql

I'm trying to achieve the following with no success so far:
Certificate based SSL connection to an external MySQL database (v5.0) from my WCF service (hostet in IIS 7.5 on Server 2008 R2) using Connector/Net.
The guys that host the mysql db have sent me three .pem files, which I combined to a single .pfx using OpenSSL, as described in Using SSL with MySQL Connector/Net.
But when I tried to connect using the file-based approach, I got an exception that told me that mysql 5.0 doesn't support file-based mode.
So I was left with but one option: store-based certificate.
Therefore I installed the certificate on the server (hosting my wcf service) for "Local Computer", adding it to "Personal Certificates". Then after adjusting the connection string to contain
"SSL Mode=Required;Certificate Store Location=LocalMachine;"
I tested again, with some weired authentication/credential errors.
After google'ing some more, I found that there might be a problem with insufficient access rights to the certificate. So I followed the steps explained here, and used the tool WinHttpCertCfg to grant access for the account "NetworkService", which I believe is used for hosting my service.
Unfortunately I still get the following exception:
Message: Out of sync with server
Source: MySql.Data
Type: MySql.Data.MySqlClient.MySqlException
StackTrace:
bei MySql.Data.MySqlClient.NativeDriver.ReadOk(Boolean read)
bei MySql.Data.MySqlClient.NativeDriver.AuthenticateNew(Boolean reset)
bei MySql.Data.MySqlClient.NativeDriver.Authenticate(Boolean reset)
bei MySql.Data.MySqlClient.NativeDriver.Open()
bei MySql.Data.MySqlClient.Driver.Open()
bei MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
bei MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
bei MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
bei MySql.Data.MySqlClient.MySqlPool.GetConnection()
bei MySql.Data.MySqlClient.MySqlConnection.Open()
bei System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
bei System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
bei System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
bei AtlasService.AtlasService.getChildNodes(UInt32 nid, Language lang, Boolean withEmpty, DateFormats format)
Does anyone know what the problem is here, or what I'm doing wrong?
Any help would be greatly apreciated.
Thanks in advance
/EDIT: I should add that I successfully tested the ssl connection using mysql commandline tool and workbench (with the separate .pem files)

Ok after hours of frustration I found a solution.
It seems that there is a problem with the latest version (v6.4.3) of the connector/net lib or the combination of ssl with the versions of connector/net lib and mysql server in use.
After downgrading the connector/net lib to v6.3.7 everything works fine.

Related

Getting error "A network-related or instance-specific error occurred while establishing a connection to SQL Server" when using MySQL and Authorization

I have an ASP.NET project running on .NET Framework 4.8 and using MySQL for the database. Everything was working fine, till I decided to user Authorization. I have the following:
[Authorize(Roles = "Admin")]
public class ApproveActionsController : DaysOffBaseController
{
}
The moment I try to access the URL: https://myApp/DayOffModule/ApproveActions/ApproveDaysOffList
I'm getting the following error message and the app is throwing SqlException
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I have read a lot and it looks like it's thinking that it thinks I'm using SQL Server or trying to connect to it. I have tried looking everywhere in the app for System.Data.SqlClient and changed it to MySql.Data.MySqlClient but it's still throwing the same Exception.
When I remove the [Authorize(Roles = "Admin")] from the controller above - I'm not having this issue.
Here is the entire stack:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
SQLExpress database file auto-creation error:
The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:
If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.com/fwlink/?LinkId=160102. If the application's App_Data directory does not already exist, the web server account must have read and write access to the application's directory. This is necessary because the web server account will automatically create the App_Data directory if it does not already exist.
If the application's App_Data directory already exists, the web server account only requires read and write access to the application's App_Data directory. This is necessary because the web server account will attempt to verify that the Sql Server Express database already exists within the application's App_Data directory. Revoking read access on the App_Data directory from the web server account will prevent the provider from correctly determining if the Sql Server Express database already exists. This will cause an error when the provider attempts to create a duplicate of an already existing database. Write access is required because the web server account's credentials are used when creating the new database.
Sql Server Express must be installed on the machine.
The process identity for the web server account must have a local user profile. See the readme document for details on how to create a local user profile for both machine and domain accounts.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)]
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager) +1524
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +706
System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +57
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +1186
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +315
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +128
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +265
System.Data.SqlClient.SqlConnection.Open() +133
System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +82
[HttpException (0x80004005): Unable to connect to SQL Server database.]
System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +175
System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) +121
System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) +57
System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) +475

LOAD DATA LOCAL INFILE file request rejected due to restrictions on access (ODBC)

Oh my, how do I even start asking this question.... I have a feeling this has been answered more than ones but I'm too dumb to understand the answers.... So I'm sure I'll be roasted for lots of reasons, but I'm feeling brave today.
I'm trying to automatically update my Database with a .csv file I receive daily.
Biggest issue I have is that I have no coding experience and my SQL is extremely limited so when I search for help regarding this issue I .... don't really know what to do with the information.
In order to get Power Automate (desktop) to connect to the server I had to install ODBC Data Sources to create a connection string. Then I tried a simple query to make sure i have connection, and this works fine.
Now, for the actual query I want to run;
"LOAD DATA LOCAL INFILE 'C:\Automation\Lagerførte Artikkler.csv' REPLACE INTO TABLE ......
I get this error;
Error in SQL statement ERROR [HY000] [MySQL][ODBC 8.0(w) Driver][mysqld-5.5.5-10.5.18-MariaDB-1:10.5.18+maria~ubu2004-log
LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.
Detailed Error;
ERROR [HY000] [MySQL][ODBC 8.0(w) Driver][mysqld-5.5.5-10.5.18-MariaDB-1:10.5.18+maria~ubu2004-log]LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.: Microsoft.Flow.RPA.Desktop.Modules.SDK.ActionException: Error in SQL statement ERROR [HY000] [MySQL][ODBC 8.0(w) Driver][mysqld-5.5.5-10.5.18-MariaDB-1:10.5.18+maria~ubu2004-log]LOAD DATA LOCAL INFILE file request rejected due to restrictions on access. ---> System.Data.Odbc.OdbcException: ERROR [HY000] [MySQL][ODBC 8.0(w) Driver][mysqld-5.5.5-10.5.18-MariaDB-1:10.5.18+maria~ubu2004-log]LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.
at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)
at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
at Microsoft.Flow.RPA.Desktop.Modules.Database.Actions.DatabaseActions.ExecuteSQLStatement(Variant connectionString, Variant sqlConnectionVariable, Variant sqlCommand, Variant& result, Int32 timeout, Int32 getConnection)
--- End of inner exception stack trace ---
at Microsoft.Flow.RPA.Desktop.Modules.Database.Actions.ExecuteSqlStatement.Execute(ActionContext context)
at Microsoft.Flow.RPA.Desktop.Robin.Engine.Execution.ActionRunner.Run(IActionStatement statement, Dictionary`2 inputArguments, Dictionary`2 outputArguments)
If I change the path to something non existing I get same error.
If I run this query in HeidiSQL it works fine. Also tried to run it through SQL plugin for VS, and this also works fine. (The pathing then is C:\Users\UNRAID~1\DOCUME~1\AUTOMA~1\LAGERF~1.CSV)
If I delete the LOCAL part in the query I get this error;
Access denied for user 'cncportalenno01'#'%' (using password: YES)
What I don't understand is how this user has no access when it clearly has access when going through heidi or vs ? (This might be a dumb take, but as I said, I'm .... lost)
Any help, or guidance wound be greatly appreciated, spent 20h on this f... issue already.
Also tried to put the csv file in Public (Users) folder and just on C:
There is also this checkbox in ODBC Data source config which states "Enabled LOAD DATA LOCAL INFILE statements" but this is an option which do not save. How to overwrite this setting I have no clue.
ODBC Settings
I managed to get this bloddy thing fixed by using "MariaDB ODBC 3.1" Driver rather than "MySQL ODBC 8.0 Driver".....
Relief and anger at same time sure feels wierd.
The server needs local_infile=1 enabled. Given HeidiSQL worked we can assume this is set.
The second requirement is the client connection needs to connect with a local_infile flag set.
In ODBC, this is the OPTIONS with bit 7 set aka 128 (2^7).

AWS RDS - Unable to connect to any of the specified MySQL hosts

I am working on a web application that is using AWS RDS (MySQL). Everything works fine until I send 10/20 concurrent requests. When I start sending more concurrent requests (say 100/200), then RDS does not respond in timely manner and application throws errors -
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
---> MySql.Data.MySqlClient.MySqlException (0x80004005): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
at MySql.Data.Common.StreamCreator.GetTcpStream(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.NativeDriver.Open()
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 Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities[TOut,TIn](IEnumerable`1 results, QueryContext queryContext, IList`1 entityTrackingInfos, IList`1 entityAccessors)+MoveNext()
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
At the same time, if I go and check the RDS cluster or database monitoring, I do not see any spikes in CPU usage or abnormal connections.
Can you provide any hint as to what should I investigate further to identify the root cause?
[Update 31/10/2022 12:30 PM]
In the performance insights of the database, I can see a following graph that interests me:

Using an AWS Elastic Load Balancer for MySQL Cluster

I have a web server which connects to a MySQL cluster (it's Galera cluster)... the cluster works fine (if I update a record on the first instance of MySQL, it is automatically updated on the second instance). The problem is I need a load balancer between the web server and MySQL instances...
So What I have done is to setup an AWS Network Load Balancer (NLB works on Network Layer, TCP in my case)... I have added the 2 instances of MySQL to the NLB's Target group (their private IPs).
Now I go ahead an update the connection string in my Web Server to connect to the NLB:
<add name="MyDB" connectionString="Server=my-nlb-ap-southeast-2.amazonaws.com; Port=3306; Database=mydb; Uid=user1; Pwd=mypassword; Protocol=TCP" providerName="MySql.Data.MySqlClient"/>
But I get the following error:
[MySqlException (0x80004005): Unable to connect to any of the
specified MySQL hosts.] MySql.Data.MySqlClient.NativeDriver.Open()
+1734 MySql.Data.MySqlClient.Driver.Open() +60 MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder
settings) +309
MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() +18
MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() +290
MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() +151
MySql.Data.MySqlClient.MySqlPool.GetConnection() +60
MySql.Data.MySqlClient.MySqlConnection.Open() +1588
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection
connection) +182
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection
connection)
When I try to connect to MySQL from my web server, I get the following:
This indicates that the web server is able to reach the MySQL instance through the NLB, but I don't know why the connection string is not working.
I have opened all the IPs on port 3306 , and I can connect to MySQL through NLB from my local computer which is not in the VPC:
But the web server connection string is not working...
I am not entirely sure if it's even possible to use an NLB for this purpose?

Deploy .NET app to Azure using another server database

Is possible to deploy my .NET application to azure using as connection string another server MySQL database?
I created an application in .NET MVC. It connects to my private server MySQL Database. The application works fine. However, when I deployed it to Azure server, it doesn't working.
When I try to publish, I don't have MySQL as datasource:
When I run it in Azure server, I get the error:
[MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.]
MySql.Data.MySqlClient.NativeDriver.Open() +1228
MySql.Data.MySqlClient.Driver.Open() +22
MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) +218
MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() +287
MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() +93
MySql.Data.MySqlClient.MySqlPool.GetConnection() +65
MySql.Data.MySqlClient.MySqlConnection.Open() +566
System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) +41
My connectionstring:
<connectionStrings>
<add name="myEntities" connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=myServer;user id=userName;password=Password;database=myDB"" providerName="System.Data.EntityClient" />
</connectionStrings>
Does anyone know how to fix it?
Thanks
You can certainly connect to a MySQL server from an MVC web app. Your MySQL server can be hosted in Azure, AWS, or any other hosting provider that lets you connect to your database remotely. However, making database calls outside of your Azure data enter will cause a delay due to network latency.