当前位置:Gxlcms > mysql > MySQL从库集群方案之HAProxy篇_MySQL

MySQL从库集群方案之HAProxy篇_MySQL

时间:2021-07-01 10:21:17 帮助过:150人阅读

Mysql集群Proxy

HAProxy反向代理服务器支持双机热备支持虚拟主机,其配置简单,拥有非常不错的服务器健康检查功能。当其代理的后端服务器出现故障,HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入。

这里有两台HAProxy机器,分别安装keepalived,组成热备形式。作用:当一台有问题,另一台可以在1秒内接管。

xinetd服务的作用是检测端口,本文中使用8890端口。HAProxy用http协议检测这个端口是否正常。

MySQL同步状态脚本,是放在从库本地,由xinetd服务来激活脚本,正常就会输出200状态码给HAProxy,证明从库正常;否则,就剔除。(这里就可以加上短信报警了)

系统架构图

HAProxy系统架构图

使用软件

  • HAProxy 1.4.16
  • Keepalived 1.1.20
  • Xinetd 2.3.14
  • MySQL 同步状态脚本 0.2

一、系统约定

系统环境

  • OS:CentOS 5.6 x86_64
  • MASTER:192.168.1.65
  • BACKUP:192.168.1.66
  • VIP:192.168.1.67
  • serivce Port:3306

工作流程

准备工作:应用配置好slave的VIP 192.168.1.67 端口3306

(1)应用服务器

(2)连接HAProxy的vip 192.168.1.67:3306,根据算法,分配到一台slave。

(3)检测slave的8890端口是否返回http 200状态码。

(4)返回200 状态码,HAProxy 返回正常,继续服务。

(5)返回503,剔除该slave,并将mysql请求转发到另外一台slave。

(6)有问题的slave,发送短信报警,相关人员检查。

二、Keepalived 1.1.20的安装于配置

  1. #cd /var/tmp/
  2. #wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
  3. #tar zxvf keepalived-1.1.20.tar.gz
  4. #cd keepalived-1.1.20
  5. #./configure –prefix=/usr
  6. #make && make install
  7. #cp /usr/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
  8. #cp /usr/etc/sysconfig/keepalived /etc/sysconfig/
  9. #mkdir /etc/keepalived
  10. vim /etc/keepalived/keepalived.conf
  11. ! Configuration File for keepalived
  12. global_defs {
  13. notification_email {
  14. coralzd@gmail.com
  15. }
  16. notification_email_from coralzd@gmail.com
  17. smtp_server 192.168.1.1
  18. smtp_connect_timeout 30
  19. router_id LVS_DEVEL
  20. }
  21. vrrp_script chk_HAProxy {
  22. script "killall -0 HAProxy"
  23. interval 2
  24. weight 2
  25. }
  26. vrrp_instance VI_1 {
  27. state MASTER
  28. interface eth0
  29. virtual_router_id 50
  30. priority 150
  31. advert_int 1
  32. authentication {
  33. auth_type PASS
  34. auth_pass 1111
  35. }
  36. track_interface {
  37. eth0
  38. }
  39. virtual_ipaddress {
  40. 192.168.1.67
  41. }
  42. track_script {
  43. chk_HAProxy
  44. }
  45. }<br>

三、HAProxy 1.4.16的安装与配置

  1. #cd /var/tmp/
  2. #wget http://HAProxy.1wt.eu/download/1.4/src/HAProxy-1.4.16.tar.gz
  3. #tar -zxvf HAProxy-1.4.16.tar.gz
  4. #cd HAProxy-1.4.16
  5. #make install
  6. #mkdir -p /usr/local/HAProxy/etc
  7. #mkdir -p /usr/local/HAProxy/sbin
  8. #cp examples/HAProxy.cfg /usr/local/HAProxy/etc
  9. #ln -s /usr/local/sbin/HAProxy /usr/local/HAProxy/sbin/HAProxy
  10. #mkdir /usr/share/HAProxy
  11. /etc/HAProxy/HAProxy.cfg
  12. global
  13. log 127.0.0.1 local1 notice
  14. maxconn 4096
  15. chroot /usr/share/HAProxy
  16. uid 99
  17. gid 99
  18. daemon
  19. #debug
  20. #quiet
  21. defaults
  22. log global
  23. mode http
  24. #option httplog
  25. option dontlognull
  26. retries 3
  27. option redispatch
  28. maxconn 2000
  29. contimeout 5000
  30. clitimeout 50000
  31. srvtimeout 50000
  32. listen DZW_MYSQL_SLAVE 192.168.1.67:3306
  33. #cookie SERVERID rewrite
  34. mode tcp
  35. maxconn 200
  36. balance roundrobin
  37. option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
  38. server mysql_192_168_1_23 192.168.1.23:3306 check port 8890 inter 5s rise 2 fall 3
  39. server mysql_192_168_1_24 192.168.1.24:3306 check port 8890 inter 5s rise 2 fall 3
  40. srvtimeout 20000
  41. listen admin_status
  42. mode http
  43. bind 192.168.1.65:8899
  44. option httplog
  45. log global
  46. stats enable
  47. stats refresh 10s
  48. stats hide-version
  49. stats realm Haproxy\ Statistics
  50. stats uri /admin-status
  51. stats auth admin:123456
  52. stats admin if TRUE<br>

HAProxy 启动脚本

  1. /etc/init.d/HAProxy
  2. #!/bin/sh
  3. #
  4. # chkconfig: - 85 15
  5. # description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited \
  6. # for high availability environments.
  7. # processname: HAProxy
  8. # config: /etc/HAProxy/HAProxy.cfg
  9. # pidfile: /var/run/HAProxy.pid
  10. # Script Author: Simon Matter <simon.matter@invoca.ch>
  11. # Version: 2004060600
  12. # Source function library.
  13. if [ -f /etc/init.d/functions ]; then
  14. . /etc/init.d/functions
  15. elif [ -f /etc/rc.d/init.d/functions ] ; then
  16. . /etc/rc.d/init.d/functions
  17. else
  18. exit 0
  19. fi
  20. # Source networking configuration.
  21. . /etc/sysconfig/network
  22. # Check that networking is up.
  23. [ ${NETWORKING} = "no" ] && exit 0
  24. # This is our service name
  25. BASENAME=HAProxy
  26. if [ -L ___FCKpd___2 ]; then
  27. BASENAME=`find ___FCKpd___2 -name $BASENAME -printf %l`
  28. BASENAME=`basename $BASENAME`
  29. fi
  30. [ -f /etc/$BASENAME/$BASENAME.cfg ] || exit 1
  31. RETVAL=0
  32. start() {
  33. /usr/sbin/$BASENAME -c -q -f /etc/$BASENAME/$BASENAME.cfg
  34. if [ $? -ne 0 ]; then
  35. echo "Errors found in configuration file, check it with '$BASENAME check'."
  36. return 1
  37. fi
  38. echo -n "Starting $BASENAME: "
  39. daemon /usr/sbin/$BASENAME -D -f /etc/$BASENAME/$BASENAME.cfg -p /var/run/$BASENAME.pid
  40. RETVAL=$?
  41. echo
  42. [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$BASENAME
  43. return $RETVAL
  44. }
  45. stop() {
  46. echo -n "Shutting down $BASENAME: "
  47. killproc $BASENAME -USR1
  48. RETVAL=$?
  49. echo
  50. [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$BASENAME
  51. [ $RETVAL -eq 0 ] && rm -f /var/run/$BASENAME.pid
  52. return $RETVAL
  53. }
  54. restart() {
  55. /usr/sbin/$BASENAME -c -q -f /etc/$BASENAME/$BASENAME.cfg
  56. if [ $? -ne 0 ]; then
  57. echo "Errors found in configuration file, check it with '$BASENAME check'."
  58. return 1
  59. fi
  60. stop
  61. start
  62. }
  63. reload() {
  64. /usr/sbin/$BASENAME -c -q -f /etc/$BASENAME/$BASENAME.cfg
  65. if [ $? -ne 0 ]; then
  66. echo "Errors found in configuration file, check it with '$BASENAME check'."
  67. return 1
  68. fi
  69. /usr/sbin/$BASENAME -D -f /etc/$BASENAME/$BASENAME.cfg -p /var/run/$BASENAME.pid -sf $(cat /var/run/$BASENAME.pid)
  70. }
  71. check() {
  72. /usr/sbin/$BASENAME -c -q -V -f /etc/$BASENAME/$BASENAME.cfg
  73. }
  74. rhstatus() {
  75. status $BASENAME
  76. }
  77. condrestart() {
  78. [ -e /var/lock/subsys/$BASENAME ] && restart || :
  79. }
  80. # See how we were called.
  81. case "$1" in
  82. start)
  83. start
  84. ;;
  85. stop)
  86. stop
  87. ;;
  88. restart)
  89. restart
  90. ;;
  91. reload)
  92. reload
  93. ;;
  94. condrestart)
  95. condrestart
  96. ;;
  97. status)
  98. rhstatus
  99. ;;
  100. check)
  101. check
  102. ;;
  103. *)
  104. echo ___FCKpd___2quot;Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}"
  105. exit 1
  106. esac
  107. exit $?
  108. chkconfig –add HAProxy
  109. chkconfig HAProxy on
  110. service HAProxy start<br></simon.matter@invoca.ch>

四、xinetd安装和配置

  1. yum install -y xinetd
  2. vim /etc/xinetd.d/mysql_status.sh
  3. service mysqlrep_status
  4. {
  5. flags = REUSE
  6. socket_type = stream
  7. port = 8890
  8. wait = no
  9. user = nobody
  10. server = /usr/local/bin/mysqlrep_status.sh
  11. log_on_failure += USERID
  12. disable = no
  13. }<br>

重启xinetd

  1. service xinetd restart

MySQL同步检测脚本(脚本检测同步sql和IO进程是否都为真,以及select是否达到20个进程以上)

  1. #!/bin/bash
  2. #
  3. # /usr/local/bin/mysqlchk_status.sh
  4. #
  5. # This script checks if a mysql server is healthy running on localhost. It will
  6. # return:
  7. #
  8. # "HTTP/1.x 200 OK\r" (if mysql is running smoothly)
  9. #
  10. # – OR –
  11. #
  12. # "HTTP/1.x 503 Internal Server Error\r" (else)
  13. #
  14. MYSQL_HOST="localhost"
  15. MYSQL_PORT="3306"
  16. MYSQL_USERNAME="repdb63"
  17. MYSQL_PASSWORD="mylqs9eyex7s"
  18. #
  19. # We perform a simple query that should return a few results
  20. #/usr/local/mysql/bin/mysql -hlocalhost –urepdb63 –pmylqs9eyex7s -e "show slave status\G;" > /tmp/rep.txt
  21. mysql -urepdb63 -pmylqs9eyex7s -e "show full processlist;" >/tmp/processlist.txt
  22. mysql -urepdb63 -pmylqs9eyex7s -e "show slave status\G;" >/tmp/rep.txt
  23. iostat=`grep "Slave_IO_Running" /tmp/rep.txt |awk '{print $2}'`
  24. sqlstat=`grep "Slave_SQL_Running" /tmp/rep.txt |awk '{print $2}'`
  25. result=$(cat /tmp/processlist.txt|wc -l)
  26. #echo iostat:$iostat and sqlstat:$sqlstat
  27. # if slave_IO_Running and Slave_sql_Running ok,then return 200 code
  28. if [ "$result" -lt "20" ] && [ "$iostat" = "Yes" ] && [ "$sqlstat" = "Yes" ];
  29. then
  30. # mysql is fine, return http 200
  31. /bin/echo -e "HTTP/1.1 200 OK\r\n"
  32. else
  33. # mysql is down, return http 503
  34. /bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
  35. fi<br>

注意:在mysql slave另行建立一个具有process和slave_client权限的账号。

作者简介:崔晓辉,网名coralzd,大众网系统管理员,精通网站系统架构、Unix技术。gtalk:coralzd@gmail.com

人气教程排行