4.2.6 Connecting to the Server Using DNS SRV Records使用DNS SRV记录连接到服务器

In the Domain Name System (DNS), a SRV record (service location record) is a type of resource record that enables a client to specify a name that indicates a service, protocol, and domain. 在域名系统(DNS)中,SRV记录(服务位置记录)是一种资源记录,它使客户端能够指定一个表示服务、协议和域的名称。A DNS lookup on the name returns a reply containing the names of multiple available servers in the domain that provide the required service. 对名称的DNS查找会返回一个回复,其中包含域中提供所需服务的多个可用服务器的名称。For information about DNS SRV, including how a record defines the preference order of the listed servers, see RFC 2782.有关DNS SRV的信息,包括记录如何定义所列服务器的首选项顺序,请参阅RFC 2782

MySQL supports the use of DNS SRV records for connecting to servers. MySQL支持使用DNS SRV记录连接到服务器。A client that receives a DNS SRV lookup result attempts to connect to the MySQL server on each of the listed hosts in order of preference, based on the priority and weighting assigned to each host by the DNS administrator. 接收DNS SRV查找结果的客户端尝试根据DNS管理员分配给每个主机的优先级和权重,按优先顺序连接到每个列出的主机上的MySQL服务器。A failure to connect occurs only if the client cannot connect to any of the servers.只有当客户端无法连接到任何服务器时,才会发生连接失败。

When multiple MySQL instances, such as a cluster of servers, provide the same service for your applications, DNS SRV records can be used to assist with failover, load balancing, and replication services. 当多个MySQL实例(如服务器集群)为您的应用程序提供相同的服务时,DNS SRV记录可用于协助故障转移、负载平衡和复一致性务。It is cumbersome for applications to directly manage the set of candidate servers for connection attempts, and DNS SRV records provide an alternative:应用程序直接管理连接尝试的候选服务器集很麻烦,DNS SRV记录提供了一种替代方案:

MySQL supports use of DNS SRV records to connect to servers in these contexts:MySQL支持在以下情况下使用DNS SRV记录连接到服务器:

A DNS SRV name consists of a service, protocol, and domain, with the service and protocol each prefixed by an underscore:DNS SRV名称由服务、协议和域组成,服务和协议均以下划线作为前缀:

_service._protocol.domain

The following DNS SRV record identifies multiple candidate servers, such as might be used by clients for establishing X Protocol connections:以下DNS SRV记录标识了多个候选服务器,例如客户端可能用于建立X协议连接的服务器:

Name                      TTL   Class  Priority Weight Port  Target
_mysqlx._tcp.example.com. 86400 IN SRV 0        5      33060 server1.example.com.
_mysqlx._tcp.example.com. 86400 IN SRV 0        10     33060 server2.example.com.
_mysqlx._tcp.example.com. 86400 IN SRV 10       5      33060 server3.example.com.
_mysqlx._tcp.example.com. 86400 IN SRV 20       5      33060 server4.example.com.

Here, mysqlx indicates the X Protocol service and tcp indicates the TCP protocol. 这里,mysqlx表示X协议服务,tcp表示tcp协议。A client can request this DNS SRV record using the name _mysqlx._tcp.example.com. 客户端可以使用名称_mysqlx._tcp.example.com请求此DNS SRV记录。。The particular syntax for specifying the name in the connection request depends on the type of client. For example, a client might support specifying the name within a URI-like connection string or as a key-value pair.在连接请求中指定名称的特定语法取决于客户端的类型。例如,客户端可能支持在类似URI的连接字符串中或作为键值对指定名称。

A DNS SRV record for classic protocol connections might look like this:经典协议连接的DNS SRV记录可能如下:

Name                     TTL   Class  Priority Weight  Port Target
_mysql._tcp.example.com. 86400 IN SRV 0        5       3306 server1.example.com.
_mysql._tcp.example.com. 86400 IN SRV 0        10      3306 server2.example.com.
_mysql._tcp.example.com. 86400 IN SRV 10       5       3306 server3.example.com.
_mysql._tcp.example.com. 86400 IN SRV 20       5       3306 server4.example.com.

Here, the name mysql designates the classic MySQL protocol service, and the port is 3306 (the default classic MySQL protocol port) rather than 33060 (the default X Protocol port).在这里,mysql这个名字表示经典mysql协议服务,端口是3306(默认的经典mysql协议端口),而不是33060(默认的X协议端口)。

When DNS SRV record lookup is used, clients generally must apply these rules for connection requests (there may be client- or connector-specific exceptions):当使用DNS SRV记录查找时,客户端通常必须对连接请求应用这些规则(可能存在客户端或连接器特定的例外):

For more information on using DNS SRV based connections in X DevAPI, see Connections Using DNS SRV Records.有关在X DevAPI中使用基于DNS SRV的连接的更多信息,请参阅使用DNS SRV记录的连接