码迷,mamicode.com
首页 > 数据库 > 详细

HAproxy负载均衡MYSQL双主

时间:2016-12-16 23:38:04      阅读:550      评论:0      收藏:0      [点我收藏+]

标签:haproxy 负载均衡   mysql   双主

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

技术分享

Service mysqld start

技术分享

 

安装mysql:192.168.26.212

Yum install –y mysql-server

 

技术分享

Service mysqld start

技术分享

 

配置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

技术分享

 

创建远程所有权账号:

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

技术分享

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;

技术分享

 

技术分享

Start slave;

技术分享

技术分享

 

技术分享

双主配置: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

 

技术分享

授权远程访问账号:

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

技术分享

 

 

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;

 

技术分享

Start slave;

技术分享

 

技术分享

配置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 /images /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 images, 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

 

 

 

技术分享

  

 

技术分享

技术分享


本文出自 “在路上” 博客,请务必保留此出处http://jdonghong.blog.51cto.com/3473478/1883376

HAproxy负载均衡MYSQL双主

标签:haproxy 负载均衡   mysql   双主

原文地址:http://jdonghong.blog.51cto.com/3473478/1883376

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!