简体   繁体   English

MySQL c#连接字符串故障转移

[英]MySQL c# Connection String failover

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/我知道我可以用逗号分隔连接字符串中的主机,它将使用不同的服务器: https : //www.connectionstrings.com/mysql-connector-net-mysqlconnection/multiple-servers/

for example: Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase;例如:Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase; Uid=myUsername;Pwd=myPassword; 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 TIA - 乔

The MySQL documentation says that multiple hosts can be separated by commas: MySQL 文档说多个主机可以用逗号分隔:

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.在为复制配置多个 MySQL 服务器并且您不关心要连接到的精确服务器的情况下,这可能很有用。

Unfortunately, this behaviour was broken in Connector/NET 8.0.18 and earlier (it was fixed in 8.0.19 ).不幸的是,此行为在 Connector/NET 8.0.18 及更早版本中被破坏(在 8.0.19 中修复)。

Connector/NET 8.0.19 will try multiple hosts at random unless you specify a priority attribute for each host. Connector/NET 8.0.19 将随机尝试多个主机除非您为每个主机指定一个priority属性。 For example:例如:

// hosts will be tried at random
host=10.10.10.10:3306,192.101.10.2:3305,localhost:3306;uid=test;password=xxxx;

// hosts will be tried in descending priority order
server=(address=192.10.1.52:3305,priority=60),(address=localhost:3306,priority=100);

If you can't update to 8.0.19, there is an alternative OSS MySQL ADO.NET provider that does support multiple comma-delimited hosts: MySqlConnector on GitHub , NuGet .如果您无法更新到 8.0.19,还有一个替代的 OSS MySQL ADO.NET 提供程序支持多个逗号分隔的主机: GitHub 上的 MySqlConnectorNuGet 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 .此外,它还具有Load Balance 连接字符串选项,可让您指定所需的确切负载平衡类型: RoundRobinFailOverRandomLeastConnections

The MySQL documentation is your friend here. MySQL 文档是您的朋友。 It states它指出

The host list in the connection URL comprises of two types of hosts, the primary and the secondary .连接 URL 中的主机列表包含两种类型的主机,主要次要主机 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]...] 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 https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-config-failover.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM