HAproxy负载均衡MYSQL双主

  

 

OS

IP

子网掩码

路由网关

Centos6.6

(DB:MYSQL)

Eth0:192.168.26.211

255.255.252.0

192.168.25.3

Centos6.6

(DB:MYSQL)

Eth0:192.168.26.212

255.255.252.0

192.168.25.3

Centos6.6

(HAproxy)

Eth0:192.168.26.210

255.255.252.0

192.168.25.3

   

安装mysql:192.168.26.211

Yum install –y mysql-server

HAproxy负载均衡MYSQL双主_第1张图片

Service mysqld start

HAproxy负载均衡MYSQL双主_第2张图片

 

安装mysql:192.168.26.212

Yum install –y mysql-server

 

HAproxy负载均衡MYSQL双主_第3张图片

Service mysqld start

HAproxy负载均衡MYSQL双主_第4张图片

 

配置MYSQL双主:192.168.26.211

Vim /etc/my.cnf

配置文件如下:

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

server-id=211

log-bin=mysq-bin

relay-log=relay-bin

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld

HAproxy负载均衡MYSQL双主_第5张图片

 

创建远程所有权账号:

grant replication slave,replication client on *.* to 'jerry'@'192.168.%.%' identified by 'jerrypass';

HAproxy负载均衡MYSQL双主_第6张图片

change master to master_host='192.168.26.212',master_user='jerrys',master_password='jerrypass',master_log_file='mysql-bin.000001',master_log_pos=282;

HAproxy负载均衡MYSQL双主_第7张图片

 

HAproxy负载均衡MYSQL双主_第8张图片

Start slave;

HAproxy负载均衡MYSQL双主_第9张图片

HAproxy负载均衡MYSQL双主_第10张图片

 

HAproxy负载均衡MYSQL双主_第11张图片

双主配置:192.168.26.212

Vim /etc/my.cnf

配置文件如下:

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

server-id=212

log-bin=mysql-bin

relay-log=relay-bin

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

HAproxy负载均衡MYSQL双主_第12张图片

授权远程访问账号:

grant replication slave,replication client on *.* to 'jerry'@'192.168.%.%' identified by 'jerrypass';

HAproxy负载均衡MYSQL双主_第13张图片

 

 

change master to master_host='192.168.26.211',master_user='jerry',master_password='jerrypass',master_log_file='mysql-bin.000001',master_log_pos=281;

 

HAproxy负载均衡MYSQL双主_第14张图片

Start slave;

HAproxy负载均衡MYSQL双主_第15张图片

 

HAproxy负载均衡MYSQL双主_第16张图片

配置HAproxy:192.168.26.210

安装:yum install –y haproxy

编辑配置文件:vim /etc/haproxy/haproxy.cfg

配置文件如下:

#---------------------------------------------------------------------

# Example configuration for a possible web application.  See the

# full configuration options online.

#

#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt

#

#---------------------------------------------------------------------

 

#---------------------------------------------------------------------

# Global settings

#---------------------------------------------------------------------

global

    # to have these messages end up in /var/log/haproxy.log you will

    # need to:

    #

    # 1) configure syslog to accept network log events.  This is done

    #    by adding the '-r' option to the SYSLOGD_OPTIONS in

    #    /etc/sysconfig/syslog

    #

    # 2) configure local2 events to go to the /var/log/haproxy.log

    #   file. A line like the following can be added to

    #   /etc/sysconfig/syslog

    #

    #    local2.*                       /var/log/haproxy.log

    #

    log         127.0.0.1 local2

 

    chroot      /var/lib/haproxy

    pidfile     /var/run/haproxy.pid

    maxconn     4000

    user        haproxy

    group       haproxy

    daemon

 

    # turn on stats unix socket

    stats socket /var/lib/haproxy/stats

 

#---------------------------------------------------------------------

# common defaults that all the 'listen' and 'backend' sections will

# use if not designated in their block

#---------------------------------------------------------------------

defaults

    mode                    tcp

    log                     global

    option                  httplog

    option                  dontlognull

    option http-server-close

    option forwardfor       except 127.0.0.0/8

    option                  redispatch

    retries                 3

    timeout http-request    10s

    timeout queue           1m

    timeout connect         10s

    timeout client          1m

    timeout server          1m

    timeout http-keep-alive 10s

    timeout check           10s

    maxconn                 3000

 

#---------------------------------------------------------------------

# main frontend which proxys to the backends

#---------------------------------------------------------------------

#frontend  main *:5000

#    acl url_static       path_beg       -i /static /p_w_picpaths /javascript /stylesheets

#    acl url_static       path_end       -i .jpg .gif .png .css .js

#

#    use_backend static          if url_static

#    default_backend             app

#

##---------------------------------------------------------------------

## static backend for serving up p_w_picpaths, stylesheets and such

##---------------------------------------------------------------------

#backend static

#    balance     roundrobin

#    server      static 127.0.0.1:4331 check

frontend websrv *:3306

   default_backend mysqlservers

backend mysqlservers

     balance  leastconn

     server m1 192.168.26.211:3306  check port 3306 maxconn 300

     server m2 192.168.26.212:3306  check port 3306 maxconn 300

listen static

     mode  http

     bind *:8008

     stats enable

     stats uri /haadmin?stats

     stats auth admin:admin

     stats hide-version

     stats admin if TRUE

 

 

 

HAproxy负载均衡MYSQL双主_第17张图片

  

 

HAproxy负载均衡MYSQL双主_第18张图片