<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="connecting-using-dns-srv">
      </a>
      6.2.6 Connecting to the Server Using DNS SRV Records
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045322411936">
  </a>
  <a class="indexterm" name="idm46045322410864">
  </a>
  <p>
   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
   <a class="ulink" href="https://tools.ietf.org/html/rfc2782" target="_blank">
    RFC 2782
   </a>
   .
  </p>
  <p>
   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.
  </p>
  <p>
   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:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      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.
     </p>
    </li>
    <li class="listitem">
     <p>
      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.
     </p>
    </li>
    <li class="listitem">
     <p>
      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.
     </p>
    </li>
   </ul>
  </div>
  <p>
   MySQL supports use of DNS SRV records to connect to servers in
      these contexts:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      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
      <a class="ulink" href="/doc/x-devapi-userguide/en/connecting-dns-srv.html" target="_top">
       Connections Using DNS SRV Records
      </a>
      . For details, see the
          documentation for individual MySQL Connectors.
     </p>
    </li>
    <li class="listitem">
     <p>
      The C API provides a
      <a class="ulink" href="/doc/c-api/8.4/en/mysql-real-connect-dns-srv.html" target="_top">
       <code class="literal">
        mysql_real_connect_dns_srv()
       </code>
      </a>
      function that is similar to
      <a class="ulink" href="/doc/c-api/8.4/en/mysql-real-connect.html" target="_top">
       <code class="literal">
        mysql_real_connect()
       </code>
      </a>
      , 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
      <a class="ulink" href="/doc/c-api/8.4/en/mysql-real-connect-dns-srv.html" target="_top">
       mysql_real_connect_dns_srv()
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      The
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client has a
      <a class="link" href="mysql-command-options.html#option_mysql_dns-srv-name">
       <code class="option">
        --dns-srv-name
       </code>
      </a>
      option to
          indicate a DNS SRV record that specifies a group of servers.
          See
      <a class="xref" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       Section 6.5.1, “mysql — The MySQL Command-Line Client”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
  <p>
   A DNS SRV name consists of a service, protocol, and domain, with
      the service and protocol each prefixed by an underscore:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-simple"><div class="docs-select-all right" id="sa3058022"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-simple">_<em class="replaceable">service</em><span class="token punctuation">.</span>_<em class="replaceable">protocol</em><span class="token punctuation">.</span><em class="replaceable">domain</em></code></pre>
  </div>
  <p>
   The following DNS SRV record identifies multiple candidate
      servers, such as might be used by clients for establishing
      X Protocol connections:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-simple"><div class="docs-select-all right" id="sa84951181"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-simple">Name                      TTL   Class  Priority Weight Port  Target
_mysqlx<span class="token punctuation">.</span>_tcp<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span> 86400 IN SRV 0        5      33060 server1<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span>
_mysqlx<span class="token punctuation">.</span>_tcp<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span> 86400 IN SRV 0        10     33060 server2<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span>
_mysqlx<span class="token punctuation">.</span>_tcp<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span> 86400 IN SRV 10       5      33060 server3<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span>
_mysqlx<span class="token punctuation">.</span>_tcp<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span> 86400 IN SRV 20       5      33060 server4<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span></code></pre>
  </div>
  <p>
   Here,
   <code class="literal">
    mysqlx
   </code>
   indicates the X Protocol service
      and
   <code class="literal">
    tcp
   </code>
   indicates the TCP protocol. A client
      can request this DNS SRV record using the name
   <code class="literal">
    _mysqlx._tcp.example.com
   </code>
   . 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.
  </p>
  <p>
   A DNS SRV record for classic protocol connections might look like
      this:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-simple"><div class="docs-select-all right" id="sa16059411"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-simple">Name                     TTL   Class  Priority Weight  Port Target
_mysql<span class="token punctuation">.</span>_tcp<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span> 86400 IN SRV 0        5       3306 server1<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span>
_mysql<span class="token punctuation">.</span>_tcp<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span> 86400 IN SRV 0        10      3306 server2<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span>
_mysql<span class="token punctuation">.</span>_tcp<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span> 86400 IN SRV 10       5       3306 server3<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span>
_mysql<span class="token punctuation">.</span>_tcp<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span> 86400 IN SRV 20       5       3306 server4<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span></code></pre>
  </div>
  <p>
   Here, the name
   <code class="literal">
    mysql
   </code>
   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).
  </p>
  <p>
   When DNS SRV record lookup is used, clients generally must apply
      these rules for connection requests (there may be client- or
      connector-specific exceptions):
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      The request must specify the full DNS SRV record name, with
          the service and protocol names prefixed by underscores.
     </p>
    </li>
    <li class="listitem">
     <p>
      The request must not specify multiple host names.
     </p>
    </li>
    <li class="listitem">
     <p>
      The request must not specify a port number.
     </p>
    </li>
    <li class="listitem">
     <p>
      Only TCP connections are supported. Unix socket files, Windows
          named pipes, and shared memory cannot be used.
     </p>
    </li>
   </ul>
  </div>
  <p>
   For more information on using DNS SRV based connections in
      X DevAPI, see
   <a class="ulink" href="/doc/x-devapi-userguide/en/connecting-dns-srv.html" target="_top">
    Connections Using DNS SRV Records
   </a>
   .
  </p>
 </div>
 <br/>
</div>
