HA代理MySQL – 仅限故障转移

我有兴趣在我们的MySQL环境中使用HA代理作为故障转移/负载均衡器。 我们有一个主 – 主+ 2从设置。

我想为主/主设置,即使他们设置它,只写入一个主。 这是为了避免裂脑的问题。

我打算在HA代理上有一个单独的端口进行读取操作,并且要负载平衡。

是否有可能使用HA代理作为一个失败,如果是的话,你将如何设置?

是不是像roundrobin平衡,但给服务器之一的权重1和另一个权重为0? 有了这个想法,如果第一台服务器下线了,第二台服务器不pipe它的重量如何?

是否有可能使用HA代理作为一个失败,如果是的话,你将如何设置?

是的,通过将backup选项添加到server行末端是可能的,如下所示:

 frontend FE_mysql_writer bind VIPA:3306 default_backend BE_mysql_writer backend BE_mysql_writer mode tcp balance roundrobin option tcpka option httpchk server mysql1 ip1:3306 weight 1 check port 9199 inter 12000 rise 3 fall 3 server mysql2 ip2:3306 weight 1 check port 9199 inter 12000 rise 3 fall 3 backup 

端口9199用于监视MySQL状态:

/etc/services

 mysqlchk 9199/tcp # mysqlchk 

/etc/xinetd.d/mysqlchk

 # /etc/xinetd.d/mysqlchk # default: on # description: mysqlchk service mysqlchk { flags = REUSE socket_type = stream port = 9199 wait = no user = nobody server = /opt/mysqlchk log_on_failure += USERID disable = no only_from = 0.0.0.0/0 # recommended to put the IPs that need # to connect exclusively (security purposes) per_source = UNLIMITED # Recently added (May 20, 2010) # Prevents the system from complaining # about having too many connections open from # the same IP. More info: # http://www.linuxfocus.org/English/November2000/article175.shtml } 

/opt/mysqlchk

 #!/bin/bash # /opt/mysqlchk # This script checks if a mysql server is healthy running on localhost. It will # return: # # "HTTP/1.x 200 OK\r" (if mysql is running smoothly) # # - OR - # # "HTTP/1.x 500 Internal Server Error\r" (else) # # The purpose of this script is make haproxy capable of monitoring mysql properly # # Author: Unai Rodriguez # # It is recommended that a low-privileged-mysql user is created to be used by # this script. Something like this: # # mysql> GRANT SELECT on mysql.* TO 'mysqlchkusr'@'localhost' \ # -> IDENTIFIED BY '257retfg2uysg218' WITH GRANT OPTION; # mysql> flush privileges; MYSQL_HOST="localhost" MYSQL_PORT="3306" MYSQL_USERNAME="mysqlchkusr" MYSQL_PASSWORD="pa$$w0rd" TMP_FILE="/tmp/mysqlchk.out" ERR_FILE="/tmp/mysqlchk.err" # # We perform a simple query that should return a few results :-p # /usr/bin/mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --user=$MYSQL_USERNAME \ --password=$MYSQL_PASSWORD -e"show databases;" > $TMP_FILE 2> $ERR_FILE # # Check the output. If it is not empty then everything is fine and we return # something. Else, we just do not return anything. # if [ "$(/bin/cat $TMP_FILE)" != "" ] then # mysql is fine, return http 200 /bin/echo -e "HTTP/1.1 200 OK\r\n" /bin/echo -e "Content-Type: Content-Type: text/plain\r\n" /bin/echo -e "\r\n" /bin/echo -e "MySQL is running.\r\n" /bin/echo -e "\r\n" else # mysql is fine, return http 503 /bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n" /bin/echo -e "Content-Type: Content-Type: text/plain\r\n" /bin/echo -e "\r\n" /bin/echo -e "MySQL is *down*.\r\n" /bin/echo -e "\r\n" fi 

资料来源: http : //sysbible.org/2008/12/04/having-haproxy-check-mysql-status-through-a-xinetd-script/


但等一下,当一个主人失败时,你怎么把奴隶指向新的主人? 您最好使用HAProxy来对读取操作进行负载均衡,并让写入操作(包括故障转移)到Percona Pacemaker Resource Agents处理。

请注意,某些mysql的客户端实现(例如官方JDBC连接器 )不支持使用负载均衡器软件。 如果你碰巧有那么幸运(或不幸),你有这样一个连接器和控制它的configuration,有几个好处,即:

  • 客户端故障转移。 less了一些可能出错的例子。

  • 通过使用故障转移节点进行读取,提高了读取性能。

  • 较低的延迟(主要是挑选,但在一些设置中非常重要)

查看JDBC的示例 。

而且还有一个mysqlproxy ,它和ha-proxy的解决方法差不多。