8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 6 个 MySQL 程序 / 6.2 使用 MySQL 程序 / 6.2.6 使用 DNS SRV 记录连接到服务器
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

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

在域名系统(DNS)中,SRV 记录(服务位置记录)是一种资源记录类型,使客户端能够指定一个名称,该名称指示服务、协议和域。对该名称进行 DNS 查询会返回一个回复,其中包含提供所需服务的域中多个可用服务器的名称。有关 DNS SRV 的信息,包括记录如何定义列出服务器的优先顺序,请参见 RFC 2782。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. A DNS lookup on the name returns a reply containing the names of multiple available servers in the domain that provide the required service. For information about DNS SRV, including how a record defines the preference order of the listed servers, see RFC 2782.

MySQL 支持使用 DNS SRV 记录来连接服务器。客户端在收到 DNS SRV 查询结果后,会根据 DNS 管理员为每个主机分配的优先级和权重,按优先顺序尝试连接列表中的每个 MySQL 服务器。只有在客户端无法连接到任何服务器时,才会发生连接失败。MySQL supports the use of DNS SRV records for connecting to servers. 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. A failure to connect occurs only if the client cannot connect to any of the servers.

当多个 MySQL 实例(例如服务器集群)为您的应用程序提供相同的服务时,可以使用 DNS SRV 记录来协助实现故障切换、负载均衡和复制服务。应用程序直接管理连接尝试的候选服务器集合是繁琐的,而 DNS SRV 记录提供了一种替代方案: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. 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 记录使 DNS 管理员能够将单个 DNS 域映射到多个服务器。当服务器从配置中添加或移除,或者其主机名发生更改时,管理员也可以集中更新 DNS SRV 记录。DNS SRV records enable a DNS administrator to map a single DNS domain to multiple servers. DNS SRV records also can be updated centrally by administrators when servers are added or removed from the configuration or when their host names are changed.

  • 对 DNS SRV 记录的集中管理消除了各个客户端在连接请求中识别每个可能主机的需要,也无需通过额外的软件组件来处理连接。应用程序可以使用 DNS SRV 记录来获取候选 MySQL 服务器的信息,而无需自行管理服务器信息。Central management of DNS SRV records eliminates the need for individual clients to identify each possible host in connection requests, or for connections to be handled by an additional software component. An application can use the DNS SRV record to obtain information about candidate MySQL servers, instead of managing the server information itself.

  • DNS SRV 记录可以与连接池结合使用,在这种情况下,当主机不再位于当前 DNS SRV 记录列表中时,其连接在空闲时会从池中移除。DNS SRV records can be used in combination with connection pooling, in which case connections to hosts that are no longer in the current list of DNS SRV records are removed from the pool when they become idle.

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

DNS SRV 名称由服务、协议和域组成,其中服务和协议前都加有下划线:A DNS SRV name consists of a service, protocol, and domain, with the service and protocol each prefixed by an underscore:

_service._protocol.domain

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

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.

这里,mysqlx 表示 X 协议服务,tcp 表示 TCP 协议。客户端可以使用名称 _mysqlx._tcp.example.com 请求这个 DNS SRV 记录。连接请求中指定名称的具体语法取决于客户端类型。例如,客户端可能支持在类似 URI 的连接字符串中指定名称,或者以键值对的形式指定。Here, mysqlx indicates the X Protocol service and tcp indicates the TCP protocol. A client can request this DNS SRV record using the name _mysqlx._tcp.example.com. 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.

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

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.

这里,名称 mysql 指的是经典的 MySQL 协议服务,端口是 3306(默认经典 MySQL 协议端口),而不是 33060(默认 X 协议端口)。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).

当使用 DNS SRV 记录查询时,客户端通常必须对连接请求应用这些规则(可能存在客户端或连接器特定的例外情况):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 记录名称,服务和协议名称前面必须加下划线。The request must specify the full DNS SRV record name, with the service and protocol names prefixed by underscores.

  • 请求不得指定多个主机名。The request must not specify multiple host names.

  • 请求不得指定端口号。The request must not specify a port number.

  • 只支持 TCP 连接。不能使用 Unix 套接字文件、Windows 命名管道和共享内存。Only TCP connections are supported. Unix socket files, Windows named pipes, and shared memory cannot be used.

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