iTranslated by AI
MySQL 8.0.22 DNS SRV Record Support
I tested the new DNS SRV record support feature introduced in MySQL 8.0.22.
I used docker-compose to set up four containers: three MySQL servers (a.example.com, b.example.com, c.example.com) and one client to connect to them.
Dockerfile
FROM ubuntu
RUN apt update
RUN apt install -y mysql-client libmysqlclient-dev gcc unbound bind9-dnsutils
RUN rm -f /etc/unbound/unbound.conf.d/root-auto-trust-anchor-file.conf
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT /entrypoint.sh
docker-compose.yml
services:
client:
build: .
hostname: client
volumes:
- ./resolv.conf:/etc/resolv.conf
- ./unbound-example.conf:/etc/unbound/unbound.conf.d/example.conf
- .:/work
networks:
test:
ipv4_address: 192.168.100.100
a:
image: mysql:8.0.22
hostname: a
networks:
test:
ipv4_address: 192.168.100.101
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=1
entrypoint: bash -c '/entrypoint.sh mysqld & sleep infinity'
b:
image: mysql:8.0.22
hostname: b
networks:
test:
ipv4_address: 192.168.100.102
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=1
entrypoint: bash -c '/entrypoint.sh mysqld & sleep infinity'
c:
image: mysql:8.0.22
hostname: c
networks:
test:
ipv4_address: 192.168.100.103
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=1
entrypoint: bash -c '/entrypoint.sh mysqld & sleep infinity'
networks:
test:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.100.0/24
unbound-example.conf
server:
interface: 127.0.0.1
local-zone: "example.com." static
local-data: "a.example.com. IN A 192.168.100.101"
local-data: "b.example.com. IN A 192.168.100.102"
local-data: "c.example.com. IN A 192.168.100.103"
local-data: "_mysql._tcp.example.com. IN SRV 1 0 3306 a.example.com"
local-data: "_mysql._tcp.example.com. IN SRV 2 0 3306 b.example.com"
local-data: "_mysql._tcp.example.com. IN SRV 3 0 3306 c.example.com"
resolv.conf
nameserver 127.0.0.1
entrypoint.sh
#!/bin/bash
unbound
sleep infinity
Place these files in the same directory and execute docker-compose.
% docker-compose up -d
Creating network "m_test" with driver "bridge"
Creating m_c_1 ... done
Creating m_a_1 ... done
Creating m_client_1 ... done
Creating m_b_1 ... done
% docker-compose exec client bash
Here is the current status. The SRV record values for _mysql._tcp.example.com are prioritized in order: a.example.com, b.example.com, and c.example.com.
root@client:/# dig _mysql._tcp.example.com srv +short
1 0 3306 a.example.com.
2 0 3306 b.example.com.
3 0 3306 c.example.com.
No matter how many times I run it, it connects to 'a', which has the highest priority.
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
a
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
a
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
a
If I shut down the 'a' mysqld, it connects to 'b'.
root@client:/# mysql -h a.example.com -e shutdown
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
b
If I shut down 'b', it connects to 'c', and if I shut down 'c' as well, it results in an error.
root@client:/# mysql -h b.example.com -e shutdown
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
c
root@client:/# mysql -h c.example.com -e shutdown
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
ERROR 2003 (HY000): Can't connect to MySQL server on 'c.example.com' (111)
Next, I'll set the priorities to be equal and change the Weights (load balancing distribution).
Change unbound-example.conf as follows:
server:
interface: 127.0.0.1
local-zone: "example.com." static
local-data: "a.example.com. IN A 192.168.100.101"
local-data: "b.example.com. IN A 192.168.100.102"
local-data: "c.example.com. IN A 192.168.100.103"
local-data: "_mysql._tcp.example.com. IN SRV 1 10 3306 a.example.com"
local-data: "_mysql._tcp.example.com. IN SRV 1 20 3306 b.example.com"
local-data: "_mysql._tcp.example.com. IN SRV 1 30 3306 c.example.com"
% docker-compose down
Stopping m_client_1 ... done
Stopping m_a_1 ... done
Stopping m_b_1 ... done
Stopping m_c_1 ... done
Removing m_client_1 ... done
Removing m_a_1 ... done
Removing m_b_1 ... done
Removing m_c_1 ... done
Removing network m_test
% docker-compose up -d
Creating network "m_test" with driver "bridge"
Creating m_client_1 ... done
Creating m_b_1 ... done
Creating m_c_1 ... done
Creating m_a_1 ... done
% docker-compose exec client bash
root@client:/# dig _mysql._tcp.example.com srv +short
1 10 3306 a.example.com.
1 20 3306 b.example.com.
1 30 3306 c.example.com.
No matter how many times I run it, it connects to 'c'.
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
c
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
c
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
c
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
c
root@client:/# mysql -s -N --dns-srv-name _mysql._tcp.example.com -e 'select @@hostname'
c
Although the program controls which SRV record to connect to, the mysql command launches a new process each time and has no memory of what it connected to previously. Therefore, it likely just selects 'c', which has the highest weight, every time.
So, I wrote a simple program using the C API.
#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>
int main(int argc, char *argv[])
{
MYSQL my;
mysql_init(&my);
for (int i = 0; i < 30; i++) {
if (mysql_real_connect_dns_srv(&my, "_mysql._tcp.example.com", "root", NULL, NULL, 0) == NULL)
goto error;
if (mysql_real_query(&my, "select @@hostname", 17) != 0)
goto error;
MYSQL_RES *res;
if ((res = mysql_store_result(&my)) == NULL)
goto error;
MYSQL_ROW row;
while ((row = mysql_fetch_row(res))) {
unsigned long *lengths;
lengths = mysql_fetch_lengths(res);
printf("%.*s\n", (int)lengths[0], row[0]);
}
mysql_close(&my);
}
exit(0);
error:
puts(mysql_error(&my));
exit(1);
}
It connects 30 times within the same process.
root@client:/work# gcc test.c -lmysqlclient
root@client:/work# ./a.out
c
b
c
c
c
b
b
c
b
c
b
c
b
b
c
c
c
c
a
c
a
b
a
c
a
b
a
a
c
b
root@client:/work# ./a.out | sort | uniq -c
6 a
10 b
14 c
It seems the connection targets were chosen in proportions corresponding to their weights.
A bit about SRV records.
SRV records are like generalized MX records, where the beginning of the record name is the service and protocol (TCP, UDP).
RFC6186 shows the following examples:
_submission._tcp SRV 0 1 587 mail.example.com.
_imap._tcp SRV 0 1 143 imap.example.com.
_imaps._tcp SRV 0 1 993 imap.example.com.
_pop3._tcp SRV 0 1 110 pop3.example.com.
_pop3s._tcp SRV 0 1 995 pop3.example.com.
Email apps that support SRV records can obtain SMTP/POP/IMAP connection destinations via DNS if they only know the domain name example.com.
Given this, since the beginning of the record name for MySQL is fixed as _mysql._tcp, there shouldn't be a need to specify it manually. I wonder why they require it to be specified. It seems like the mysql_real_connect_dns_srv() function could have simply prepended _mysql._tcp...
Discussion