X DevAPI User Guide

2.2.3 Connections Using DNS SRV Records

X DevAPI supports the use of DNS SRV records for connections. In the Domain Name System (DNS), SRV records (service location records) are a type of resource record that enables a client to specify a protocol and domain for a connection, and get a reply with the names of multiple available servers in the domain that provide the required service. The client then attempts to connect to the listed hosts in order of preference, based on the priority and weighting that was assigned to each of them by the DNS administrator in the SRV records.

Where multiple MySQL instances, such as a cluster of servers, can provide the same service for your applications, DNS SRV records can be used to assist with failover, load balancing, and replication services. They remove the need for clients to identify each possible host in the URI-like connection string, or for connections to be handled by an additional software component. They can also be updated centrally by administrators when servers are added or removed from the configuration or when their host names are changed. 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 SRV records are removed from the pool when they become idle.

MySQL Connectors which implement X DevAPI can request SRV record lookup by specifying mysqlx+srv as the scheme element of the URI-like connection string, for example:

mysqlx+srv://example.com/db?options

X DevAPI's mysqlx.getSession() method, and the mysqlx.getClient() method for connection pooling, validate connection information with this protocol scheme extension, and handle the resulting SRV records as a list of hosts for the purposes of failover behavior and connection pooling. The priority and weighting specified in the SRV records are respected.

MySQL Connectors also have connector-specific options to request SRV record lookup both for X Protocol connections and for classic MySQL protocol connections. For details, see the documentation for the individual MySQL Connector.

Note

DNS SRV records are not currently supported by MySQL Shell.

When SRV record lookup is used, clients must apply these rules for the connection:

  1. The client must specify the full name for the service as given in the DNS SRV records. In SRV records, the name of the service and of the protocol are prepended with underscores. For example, these SRV records relate to a service using X Protocol that can be provided by four servers in the installation:

    Record                    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 request this service using syntax like this:

    var client = mysqlx.getClient("mysqlx+srv://_mysqlx._tcp.example.com")
  2. The client must not specify a port number in the URI-like connection string or in the connection options.

  3. Unix socket files and Windows named pipes cannot be used.

  4. Multiple host names cannot be specified in the connection request (with some connector-specific exceptions).

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, "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)