I'm trying to connect to MySql server through Unix Socket instead of TCP for maximizing connection and reducing processing load.
My Asp Net Core app is running in Docker Swarm and I'm running Percona docker
I'm sharing mysqld.sock using volume sharing and on Asp Net core container I am able to login to console and make connection to the server using mysql client
/var/run/mysqld/mysqld.sock
However when Asp Net Core application is trying to connect to server it is switching from localhost to 127.0.0.1:3306 even though I set no port and SslMode=none
Any help is much
Exception message:
Stack trace:
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
---> System.AggregateException: One or more errors occurred. (Connection refused 127.0.0.1:3306)
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (111): Connection refused 127.0.0.1:3306
MySQL version: 5.7
Operating system: ubuntu:latest
Pomelo.EntityFrameworkCore.MySql version: 3.1.1
Microsoft.AspNetCore.App version: 3.1
This question is targeted at MySqlConnector, the underlying library used by Pomelo for MySQL communication.
To use Unix sockets, you need to set the connection protocol
to Unix
. The Server
option then becomes the path to the socket file. The following shows a sample connection string:
server=/var/run/mysqld/mysqld.sock;protocol=unix;database=mydatabase
See MySQL .NET Connection String Options for further details.
You are really awesome Lauxjpn, I really wish this was in Pomelo document and samples to save a lot of times, I was not aware that Pomelo is using MySqlConnector underneath @lauxjpn
I really wish this was in Pomelo document and samples to save a lot of times
Unfortunately, both documentation and samples are lacking at the moment. We really need to improve upon that before the .NET 5 release.
I provided a lot of sample code inside of issues, but we should consolidate them into multiple samples and make them part of the repo.
I was not aware that Pomelo is using MySqlConnector underneath
It is stated on our main repo page, but there is a lot of stuff there and its easy to overlook.
I really wish this was in Pomelo document and samples to save a lot of times
@mason-chase This is a community project, so if you wish to contribute your PR would be welcomed.
Most helpful comment
This question is targeted at MySqlConnector, the underlying library used by Pomelo for MySQL communication.
To use Unix sockets, you need to set the connection
protocol
toUnix
. TheServer
option then becomes the path to the socket file. The following shows a sample connection string:See MySQL .NET Connection String Options for further details.