在域名系统(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:
多个 MySQL 连接器实现了 DNS SRV 支持;特定连接器的选项可以启用对 X 协议连接和经典 MySQL 协议连接的 DNS SRV 记录查询。有关一般信息,请参见 使用 DNS SRV 记录的连接。有关详细信息,请参阅各个 MySQL 连接器的文档。Several MySQL Connectors implement DNS SRV support; connector-specific options enable requesting DNS SRV record lookup both for X Protocol connections and for classic MySQL protocol connections. For general information, see Connections Using DNS SRV Records. For details, see the documentation for individual MySQL Connectors.
C API 提供了一个
mysql_real_connect_dns_srv()函数,它类似于mysql_real_connect(),只是参数列表中不指定要连接的 MySQL 服务器的特定主机。相反,它指定了一个 DNS SRV 记录,该记录定义了一组服务器。请参见 mysql_real_connect_dns_srv()。The C API provides amysql_real_connect_dns_srv()function that is similar tomysql_real_connect(), except that the argument list does not specify the particular host of the MySQL server to connect to. Instead, it names a DNS SRV record that specifies a group of servers. See mysql_real_connect_dns_srv().mysql 客户端有一个
--dns-srv-name选项,用于指定一个定义了一组服务器的 DNS SRV 记录。参见 第 6.5.1 节,“mysql — MySQL 命令行客户端”。The mysql client has a--dns-srv-nameoption to indicate a DNS SRV record that specifies a group of servers. See Section 6.5.1, “mysql — The MySQL Command-Line Client”.
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.