MySQL c# Connection String failover

十年热恋 提交于 2019-12-02 10:55:59

问题


I am aware that I can seperate hosts in the connection string with a comma and it will use different servers: https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/multiple-servers/

for example: Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase; Uid=myUsername;Pwd=myPassword;

But I need some information on how it specifically chooses servers. For example, is it round robin? Or does it go in order, until it finds a working one?

If the first one fails, and it moves to the second one, how long would it be before it attempted to use the second one?

I am open to other suggestions for failover connection strings

TIA - Joe


回答1:


The MySQL documentation is your friend here. It states

The host list in the connection URL comprises of two types of hosts, the primary and the secondary. When starting a new connection, the driver always tries to connect to the primary host first and, if required, fails over to the secondary hosts on the list sequentially when communication problems are experienced. Even if the initial connection to the primary host fails and the driver gets connected to a secondary host, the primary host never loses its special status

So in the connection string below, the first host is the primary and will be selected first for connection. Only if this is not available will a secondary host be selected. It will also try to fail back to the primary host ASAP, but how this works can be configured.

jdbc:mysql://[primary host][:port],[secondary host 1][:port][,[secondary host 2][:port]]...[/[database]]» [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-config-failover.html




回答2:


The MySQL documentation says that multiple hosts can be separated by commas:

Multiple hosts can be specified separated by commas. This can be useful where multiple MySQL servers are configured for replication and you are not concerned about the precise server you are connecting to.

Unfortunately, this information is incorrect. There is a long-standing bug (MySQL bug 81650, MySQL bug 88962) reporting that this functionality does not work as described.

However, there is an alternative OSS MySQL ADO.NET provider that does support multiple comma-delimited hosts: MySqlConnector on GitHub, NuGet. Additionally, it has a Load Balance connection string option that lets you specify the exact kind of load balancing you want: RoundRobin, FailOver, Random, LeastConnections.



来源:https://stackoverflow.com/questions/50274556/mysql-c-sharp-connection-string-failover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!