MySQL c# 连接字符串故障转移
MySQL c# Connection String failover
我知道我可以用逗号分隔连接字符串中的主机,它将使用不同的服务器:https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/multiple-servers/
例如:Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase;
Uid=我的用户名;Pwd=我的密码;
但我需要一些关于它具体如何选择服务器的信息。例如,它是轮询吗?还是按顺序进行,直到找到一个可以工作的?
如果第一个失败,它会移动到第二个,它会在多长时间后尝试使用第二个?
我愿意接受有关故障转移连接字符串的其他建议
TIA
- 乔
MySQL 文档是您的好帮手。它指出
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
所以在下面的连接字符串中,第一个主机是主要的,将首先被选中进行连接。只有当这不可用时,才会选择辅助主机。它还将尝试尽快故障回复到主要主机,但可以配置其工作方式。
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
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. Additionally, it has a Load Balance
connection string option 让你指定您想要的确切负载平衡类型:RoundRobin
、FailOver
、Random
、LeastConnections
.
我知道我可以用逗号分隔连接字符串中的主机,它将使用不同的服务器:https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/multiple-servers/
例如:Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase; Uid=我的用户名;Pwd=我的密码;
但我需要一些关于它具体如何选择服务器的信息。例如,它是轮询吗?还是按顺序进行,直到找到一个可以工作的?
如果第一个失败,它会移动到第二个,它会在多长时间后尝试使用第二个?
我愿意接受有关故障转移连接字符串的其他建议
TIA - 乔
MySQL 文档是您的好帮手。它指出
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
所以在下面的连接字符串中,第一个主机是主要的,将首先被选中进行连接。只有当这不可用时,才会选择辅助主机。它还将尝试尽快故障回复到主要主机,但可以配置其工作方式。
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
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. Additionally, it has a Load Balance
connection string option 让你指定您想要的确切负载平衡类型:RoundRobin
、FailOver
、Random
、LeastConnections
.