我知道我可以用逗号分隔连接字符串中的主机,它将使用不同的服务器:https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/multiple-servers/
例如:服务器=服务器地址1、服务器地址2、服务器地址3;数据库=myDataBase; Uid=我的用户名;Pwd=我的密码;
但我需要一些有关它如何具体选择服务器的信息。例如,是循环赛吗?或者它是否按顺序进行,直到找到一个可用的?
如果第一个失败,并且转移到第二个,那么在尝试使用第二个之前需要多长时间?
我愿意接受有关故障转移连接字符串的其他建议
TIA - 乔
最佳答案
MySQL documentation表示多个主机可以用逗号分隔:
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.
不幸的是,这种行为在 Connector/NET 8.0.18 及更早版本中已被破坏(它是 fixed in 8.0.19 )。
Connector/NET 8.0.19 将尝试多个主机 at random除非您为每个主机指定priority
属性。例如:
// 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);
如果您无法更新到 8.0.19,还有一个替代 OSS MySQL ADO.NET 提供程序,它支持多个逗号分隔的主机:MySqlConnector on GitHub , NuGet 。此外,它还有一个负载平衡
connection string option允许您指定所需的负载平衡类型:RoundRobin
、FailOver
、Random
、LeastConnections
。
关于c# - MySQL C# 连接字符串故障转移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50274556/