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

标签 c# mysql database-connection connection-string

我知道我可以用逗号分隔连接字符串中的主机,它将使用不同的服务器: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允许您指定所需的负载平衡类型:RoundRobinFailOverRandomLeastConnections

关于c# - MySQL C# 连接字符串故障转移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50274556/

相关文章:

c# - 如何获取有关 Control.Invoke 的目标引发的异常的信息

c# - 为什么 Select 返回一个 bool 值?

Mysql 无法正常工作

java - 带有 JDBC 的 Advantage 数据库本地服务器

c# - 显示两个字符串之间的交集

c# - 生成的表与 rowspan 导致额外的单元格

mysql - 在 belongsToMany 关系中使用 name 列而不是 id

mysql - 创建三个表之间的关系

python - 将数据库连接作为参数传递给方法是一种好习惯吗?

Qt 上未加载 MySQL 驱动程序