X DevAPI supports the use of DNS SRV records for connecting to MySQL 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. This section focuses on use of DNS SRV within X DevAPI applications. For general information about DNS SRV support in MySQL, see Connecting to the Server Using DNS SRV Records.
MySQL Connectors that implement X DevAPI can request DNS SRV
record lookup by specifying mysqlx+srv
as the
scheme element of the URI-like connection string, along with the
DNS SRV name. For example:
mysqlx+srv://_mysqlx._tcp.example.com/db?options
A DNS SRV name consists of a service, protocol, and domain, with
the service and protocol each prefixed by an underscore. In the
example, mysqlx
indicates the X Protocol
service and tcp
indicates the TCP protocol.
The X DevAPI mysqlx.getSession()
method, and
the mysqlx.getClient()
method for connection
pooling, validate connection information with this protocol
scheme extension, and handle the resulting DNS SRV record as a
list of hosts for the purposes of failover behavior and
connection pooling. The priority and weighting specified in the
DNS SRV record is respected.
MySQL Connectors also have connector-specific options to request DNS SRV record lookup both for X Protocol connections and for classic MySQL protocol connections. For details, see the documentation for individual MySQL Connectors.
MySQL Shell does not currently support DNS SRV records.
When DNS SRV record lookup is used, clients generally must apply these rules for connection requests (there may be connector-specific exceptions):
-
The request must specify the full DNS SRV record name, with the service and protocol names prefixed by underscores. For example, this DNS SRV record relates to an X Protocol service implemented over TCP that can be provided by multiple servers in the installation:
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.
A client can specify that DNS SRV record using syntax like this:
var client = mysqlx.getClient("mysqlx+srv://_mysqlx._tcp.example.com")
The request must not specify multiple host names.
The request must not specify a port number.
Only TCP connections are supported. Unix socket files, Windows named pipes, and shared memory cannot be used.
Java Code
Session mySession = new
SessionFactory().getSession("mysqlx+srv://user:password@_mysql._tcp.example.com/db");
Node.js JavaScript Code
mysqlx.getSession({ host: '_mysqlx._tcp.example.com', resolveSrv: true })
C# Code
var session = MySQLX.GetSession("mysqlx+srv://user:password@_mysqlx._tcp.example.com.");
Connector/C++ Code using X DevAPI for C
mysqlx::Session sess(
SessionOption::HOST, "_mysqlx._tcp.example.com",
SessionOption::DNS_SRV, true,
SessionOption::USER, "user",
SessionOption::PWD, "password");
Python Code
session = mysqlx.get_session(host="tcp.example.com", dns_srv=True)