使用 HAproxy 为 mysql 集群拆分 read/write
Splitting read/write for mysql cluster with HAproxy
我在 ubuntu 16.04 上有一个 mysql 集群,想用 haproxy 拆分 read/write。
所有连接都将通过端口 80 完成。
如果有人使用 X.X.X.X/write 连接到服务器,我希望他被重定向到 1 个特定的服务器。
到目前为止,这是我的默认配置(除了 /write 之外的任何配置),但是当尝试连接 /write 时,我得到 "unknown MySql server host"。
我的配置如下:
global
log 127.0.0.1 local0 notice
maxconn 2000
user haproxy
group haproxy
defaults
mode tcp
log global
retries 2
timeout connect 3000
timeout server 5000
timeout client 5000
backend read
balance roundrobin
server mysql1 192.168.0.4:3306
server mysql2 192.168.0.5:3306
server mysql3 192.168.0.6:3306
backend write
server mysql1 192.168.0.4:3306
frontend local
bind *:80
acl write url_beg /write
use_backend write if write
default_backend read
感谢您的帮助!
MySQL 连接中没有 "URL",因此 url_beg
不可能匹配任何内容。连接到 MySQL 服务器(或代理)仅通过 IP 地址或主机名完成——而不是主机名和路径,因为没有路径。检测客户端使用的主机名是不可能的,因为它在建立连接时没有传递。
为了完成你想要的,你需要两个不同的主机名,指向 HAProxy 服务器上的两个不同的 IP 地址,并且这两个 IP 中的每一个都在其自己的 frontend
和 bind
语句中.
frontend read
mode tcp
bind 203.0.113.1:3306
default_backend read
frontend write
mode tcp
bind 203.0.113.2:3306
default_backend write
我在 ubuntu 16.04 上有一个 mysql 集群,想用 haproxy 拆分 read/write。
所有连接都将通过端口 80 完成。 如果有人使用 X.X.X.X/write 连接到服务器,我希望他被重定向到 1 个特定的服务器。 到目前为止,这是我的默认配置(除了 /write 之外的任何配置),但是当尝试连接 /write 时,我得到 "unknown MySql server host"。 我的配置如下:
global
log 127.0.0.1 local0 notice
maxconn 2000
user haproxy
group haproxy
defaults
mode tcp
log global
retries 2
timeout connect 3000
timeout server 5000
timeout client 5000
backend read
balance roundrobin
server mysql1 192.168.0.4:3306
server mysql2 192.168.0.5:3306
server mysql3 192.168.0.6:3306
backend write
server mysql1 192.168.0.4:3306
frontend local
bind *:80
acl write url_beg /write
use_backend write if write
default_backend read
感谢您的帮助!
MySQL 连接中没有 "URL",因此 url_beg
不可能匹配任何内容。连接到 MySQL 服务器(或代理)仅通过 IP 地址或主机名完成——而不是主机名和路径,因为没有路径。检测客户端使用的主机名是不可能的,因为它在建立连接时没有传递。
为了完成你想要的,你需要两个不同的主机名,指向 HAProxy 服务器上的两个不同的 IP 地址,并且这两个 IP 中的每一个都在其自己的 frontend
和 bind
语句中.
frontend read
mode tcp
bind 203.0.113.1:3306
default_backend read
frontend write
mode tcp
bind 203.0.113.2:3306
default_backend write