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

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:

Related

For MYSQL 5.7.31 - Which Timeout variable should be changed for a transport connection: Connection timed out

I'm using mysql 5.7.31 with C# on Linux ubuntu
When I programmatically delete a large table or drop a database I get this timeout
Stack=MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution.
---> MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered attempting to read the resultset.
---> MySql.Data.MySqlClient.MySqlException (0x80004005): Reading from the stream has failed.
---> System.IO.IOException: Unable to read data from the transport connection: Connection timed out.
---> System.Net.Sockets.SocketException (110): Connection timed out
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
To fix this I've attempted
I added this to my connection string
Connect Timeout=60
I added this to my mysqld.cnf, Restarted and checked it show variables. The log file shows it timed-out after 42 seconds.
connect_timeout=60000
interactive_timeout=60000
wait_timeout=60000
I think the issue is "Unable to read data from the transport connection: Connection timed out."
But not sure which variable needs to be changed to fix this?
Thanks
connect_timeout is used only when the client and server are performing a handshake, during the initial client connection. Increasing this helps if the client gets errors like Lost connection to MySQL server.... The default is 10 seconds.
I've never had a reason to tune this variable, because if the client can't connect within 10 seconds, it's probably blocked by a firewall, or routing, or else the MySQL Server is simply not running or is listening on another port. Increasing the timeout will not help in these cases.
wait_timeout helps the client is idle for a long time between queries. The client is not considered idle if the query executes for a long time. The idle time starts when a query finishes. The default is 8 hours (28800 seconds). If your app is timing out during an SQL statement execution, then this variable is not relevant.
interactive_timeout is just like wait_timeout, but for "interactive" clients. The only client that is in this category is the mysql command-line client. You can ignore this timeout variable, unless you're working on the mysql CLI.
You might need to tune net_read_timeout or net_write_timeout. But I wouldn't expect either of these to be necessary.
Tip: Use XFS as the filesystem for the MySQL datadir on Linux. It's much faster to drop tables or schemas, regardless of how large they are. XFS gives better performance for other work too, not just dropping tables.

Pomelo MySqlException: Unable to connect to any of the specified MySQL hosts

I am working on Blazor Server-Side project that needs to run in an Ubuntu Linux 18.04LTS server. (NOTE: This project is not using Web Assembly.)
I am using C# and the Pomelo.EntityFrameWorkCode.MySql nuget package (v3.1.1) to connect to a MySQL database running on the Linux server.
When I run the app locally on my Windows development system, everything works correctly. When I publish the files to the Linux server, it works about 5% of the time - the remainder I get an error message
Pomelo MySqlException: Unable to connect to any of the specified MySQL hosts
I am connecting to the same MySQL database on both my Windows system and the Linux server. The MySQL is running on Linux, correct ports are open, etc.
More detailed error message is:
An unhandled exception occurred while processing the request.
MySqlException: Unable to connect to any of the specified MySQL hosts.
MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs, line 442
InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call.
Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute(TState state, Func operation, Func> verifySucceeded)
MySqlException: Unable to connect to any of the specified MySQL hosts.
MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in
C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs
MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in > C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs
MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in > C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs
System.Threading.Tasks.ValueTask.get_Result()
System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable+ConfiguredValueTaskAwaiter.GetResult()
MySql.Data.MySqlClient.MySqlConnection.CreateSessionAsync(ConnectionPool pool, Nullable ioBehavior, CancellationToken cancellationToken) in
C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs
System.Threading.Tasks.ValueTask.get_Result()
System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable+ConfiguredValueTaskAwaiter.GetResult()
MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Nullable ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs
MySql.Data.MySqlClient.MySqlConnection.Open() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs
Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(bool errorsExpected)
Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(bool errorsExpected)
Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlRelationalConnection.Open(bool errorsExpected)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable+Enumerator.InitializeReader(DbContext _, bool result)
Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute(TState state, Func operation, Func> verifySucceeded)
The Configuration in the Startup class looks like:
services.AddDbContext<LabDataDbContext>(options => {
options.UseMySql(Configuration.GetConnectionString("LabDbConnection"));
options.EnableDetailedErrors();
});
My Connection String is:
"server=server.grafton.internal;port=3306;database=labdb;user=******;password=******;charset=utf8"
The interesting thing is I can use MySQL Workbench and connect to the database from either my Windows dev system, or the Linux server using exactly the same credentials.
This application is designed to be run in house only and the server running the app is not visible on the Internet, so I am not too concerned about security issues (if someone wants to know what a bunch of autistic kids have as equipment and activities, they can have it!!)
While trying to isolate the error, I have completely removed and redeployed the app to a different linux server on the network with the same result, but as I have stated earlier, about 5% of the time, the app works as expected on the linux server.
Other pages in the app that do NOT use a database are working correctly, so I do not think it is an issue with the web server (NGinx as a reverse proxy to Kestrel).
I also want to stress that the app works about 5% of the time running on linux, and 100% of the time running on Windows - both connecting to the same MySQL server running on linux.
If more code samples are wanted, the app is on Github at https://github.com/SteveTeece/Lab.
This one has got me stumped. Any ideas on how to fix this and get it running on the Linux server is greatly appreciated.
Thanks
Thanks everyone for your feedback and comments.
After another few days of fiddling, I've resolved this issue by using
Server=localhost;
in the production connection string instead of the FQDN.

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?

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts

I am running a script which takes around 45 mins. The script interacts with MySQL multiple times in between but at the end the script got stopped and generated following error:
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> System.Net.Sockets.SocketException (0x80004005): An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full
at System.Net.Sockets.Socket.DoBeginConnect(EndPoint endPointSnapshot, SocketAddress socketAddress, LazyAsyncResult asyncResult)
at System.Net.Sockets.Socket.BeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)
at MySql.Data.Common.MyNetworkStream.CreateSocketStream(MySqlConnectionStringBuilder settings, IPAddress ip, Boolean unix)
at MySql.Data.Common.MyNetworkStream.CreateStream(MySqlConnectionStringBuilder settings, Boolean unix)
at MySql.Data.Common.StreamCreator.GetStream(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.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at Connection.ExecuteNonQuery(String str)
at BulkEmailSender.Program.Main(String[] args)
The script was able to connect multiple times than what could have happened?

Cannot connect to MySQL datebase [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
We have a clan MySQL database that tracks player stats in a game. There person in charge of our MySQL is away at the moment and we are getting this following Error
[18:17:18 23] Error in Tablebuilder:
[18:17:18 23] Message: Unable to connect to any of the specified MySQL hosts.
[18:17:18 23] Native: -2147467259
[18:17:18 24] Source: MySql.Data
[18:17:18 24] StackTrace: at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnecti onStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnecti on()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at PRoConEvents.CChatGUIDStatsLoggerBF3.tablebuilder( )
[18:17:18 24] InnerException: System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 50.63.244.184:3306
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at MySql.Data.Common.StreamCreator.CreateSocketStream (IPAddress ip, Boolean unix)
at MySql.Data.Common.StreamCreator.GetStreamFromHost( String pipeName, String hostName, UInt32 timeout)
at MySql.Data.Common.StreamCreator.GetStream(UInt32 timeout)
at MySql.Data.MySqlClient.NativeDriver.Open()
[18:17:18 24] Error: System.NullReferenceException: Object reference not set to an instance of an object.
at PRoConEvents.CChatGUIDStatsLoggerBF3.tablebuilder( )
Any ideas what the issue could be?
You should start by reading error messages. They look like gibberish but when you read closely, often there is a part it tells you what's going on.
[18:17:18 24] InnerException: System.Net.Sockets.SocketException:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 50.63.244.184:3306
You should now:
Execute telnet localhost 3306 from the database server and if it fails, it's an issue with the DB server
Execute telnet 50.63.244.184 3306 from the application server and if it fails, it's an issue with the network
You have two exceptions, them tested, but the meaning is clear:
"Unable to connect to any of the specified MySQL hosts."
and
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond"
So in short: your webserver cannot connect to you database server. Your database server (the MySQL server process mysqld.exe or the server itself, like its OS, hardware, anything) might be stopped. Or the firewall was reset to block connections on the MySQL port 3306. It could be anything.
I assume you've got a standard webhost - ping them and ask them why your database server isn't operational.
...but before you embarrass yourself, make sure that your application is connecting to the right server (i.e. production DB, not dev or staging DB). Take a look at your connection string.