Is it possible to redirect MySQL (tcp) traffic to multiple servers based on domain name alone?

Our DNS is setup to point several sub-domain aliases to one proxy machine. (running haproxy and iptables). We would like to redirect all database traffic through this proxy server and route it to the appropriate db server, WITHOUT deviating from the standard MySQL port.

dev.domain1.com:3306 --> dbDevServer.domain1.com:3306
test.domain1.com:3306 --> dbTestServer.domain1.com:3306
prod.domain1.com:3306 --> dbProdServer.domain1.com:3306


  • You have active-active DB cluster? – jirib Oct 31 '13 at 15:54
  • HA offers plenty of ways to distribute traffic, most of it transparent to the end client, so dbDevServer.domain1.com could actually be a large cluster behind the proxy, but the client would see just one server.. – NickW Oct 31 '13 at 15:54
  • HAProxy doesn't seem to be able to route TCP traffic based on domain host info. If it were HTTP traffic it would be easy. – PNW-DBA Oct 31 '13 at 16:01
  • Yes, XtraDB cluster is involved in backend, but not necessarily for each domain redirection. Prod and Stage could run XtraDB cluster, Dev would run standard MySQL 5.6 But cluster is irrelevant to what I'm looking for, which is simply have all database connection domains routed via DNS to one proxy box, and then have that traffic routed to the appropriate server. – PNW-DBA Oct 31 '13 at 16:04
  • Multiple IPS on the proxy, each one corresponding to a domain? – NickW Oct 31 '13 at 16:13

No it is not possible, because MySQL protocol does allow client to provide expected hostname to server.There is no way to route arbitrary TCP traffic based on hostname, as client connects to IP address which was resolved for given hostname.
If protocol (for example HTTP or FTP) includes a requirement for client to include hostname in request (for example Host header which is mandatory for HTTP/1.1), protocol aware application can distinguish between resources based on hostname.
You can provide 3 virtual IP addresses to Haproxy and create real IP and test IP mapping in HAProxy.


If you want to load balance your mysql cluster use Galera

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.