Home > Database > Mysql Tutorial > 搭建MySQL负载均衡及高可用环境

搭建MySQL负载均衡及高可用环境

WBOY
Release: 2016-06-07 17:32:34
Original
909 people have browsed it

使用 rhel5.8,mysql,keepalived,haproxy 搭建一个集高可用、负载均衡的集群;也有使用到 mysql 的二进制复制来保持数据一致性;避

摘要: 使用 rhel5.8,mysql,keepalived,haproxy 搭建一个集高可用、负载均衡的集群;也有使用到 mysql 的二进制复制来保持数据一致性;避免了单节点故障,也提供设备的利用率;虽然这个实验只有两台设备,但可以此为例,理解概念后再放大到其它环境也可以;

目标:使用两台主机实现 Mysql 的负载均衡及冗余,,并做到双主互备;
环境:rhel5.8,mysql-5.0.77,keepalived-1.2.7,haproxy-1.4.20;
说明:
keepalived 实现通过 vrrp协议,通过使一个虚拟IP地址(或称浮动IP)在主备设备间的切换来达到主机冗余;
而客户机通过访问这个虚拟IP 来获取服务;
haproxy 是一款负载均衡软件,用于将请求按策略转发给不同主机,达到负载均衡的效果;
可以实现网络层或应用层上的判断分配;

mysql 开启二进制日志,做到数据库之间的双向复制,保持数据一致性;

#提醒一下,实际生产环境没人会这样搭建的,仅通过这个实验来理解这些概念,希望阅者能有所获;

server1_ip=192.168.5.11
server2_ip=192.168.5.12
server_vip=192.168.5.111
================================================
#此处关闭了防火墙,开启则另配置相应规则
service iptables stop
chkconfig iptables off

ls /opt/soft/ #提取准备软件到此处
haproxy-1.4.20.tar.gz  keepalived-1.2.7.tar.gz

mkdir /opt/keepalived
mkdir /opt/scripts/ #此实验用到脚本目录
mkdir /opt/log/ #此实验日志文件夹
=================================================

[install_mysql]
#在两台主机上安装 mysql ,此处使用 rpm 包安装,yum环境可以搭本地源;
yum install -y mysql-server
service mysqld start
=======================================================
[create_mysql_test_table]
#分别创建一张相同名称和字段的表,插入不同的值,方便中途测试 ha 和轮询是否成功;
#还有分别创建一个相同的用户

#server1
mysql
>use test;
>create table mywait(name char(9),phone char(14));
>insert into mywait(name,phone) values('wait',15000000000);

#server2
>use test;
>create table mywait(name char(9),phone char(14));
>insert into mywait(name,phone) values('chen',15611111111);

[new_mysql_test_user]
>mysql
>grant all on test.* to diaosi@'%' identified by '123456';
>flush privileges;
=======================================================
#从客户机上测试一下;
mysql -udiaosi -p123456 -h 192.168.5.11 -e "select * from test.mywait;"
mysql -udiaosi -p123456 -h 192.168.5.12 -e "select * from test.mywait;"

#至此,mysql 基础环境搭建完成;
=======================================================
[install_keepalived]

yum install -y libnl-devel #解决依赖关系

tar xf /opt/soft/keepalived-1.2.7.tar.gz -C /opt/soft
cd /opt/soft/keepalived-1.2.7/
#with-kernel 指定内核版本时,根据本机情况使用TAB键补全
./configure --prefix=/opt/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-308.el5-i686/
make && make install

[keepalived_config]
#因为没有安装在 / 目录下,所以这些启动和配置文件都需要再 copy 一下;

cp /opt/keepalived/sbin/keepalived /usr/sbin/
cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /opt/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

chkconfig keepalived on #设置开机启动
=======================================================
vim /etc/keepalived/keepalived.conf #keepalived 主配文件
#以下的配置适合 keepalived 本身便是服务提供者的情况;

! Configuration File for keepalived

#简单的头部,这里主要可以做邮件通知报警等的设置,此处就暂不配置了;
global_defs {
        notificationd LVS_DEVEL
}

#预先定义一个脚本,方便后面调用,也可以定义多个,方便选择;
vrrp_script mysql_chk {
        script "/opt/scripts/mysql_chke.sh"
        interval 2  #脚本循环运行间隔
        weight 2  #脚本的结果导致优先级变更,成功+2
}
#VRRP虚拟路由冗余协议配置
vrrp_instance VI_1 {  #VI_1 是自定义的名称;
        state MASTER  #表明这是一台主设备,备用设备为 BACKUP
        interface eth0  #指定VIP需要绑定的物理网卡
        virtual_router_id 11  #VRID虚拟路由标识,也叫做分组名称,该组内的设备需要相同
        priority 150  #定义这台设备的优先级 1-254;
        advert_int 1  #生存检测时的组播信息发送间隔,组内一致

        authentication {  #设置验证信息,组内一致
                auth_type PASS #有PASS 和 AH 两种,常用 PASS
                auth_pass 111 #密码
        }

        virtual_ipaddress {  #指定VIP地址,组内一致,可以设置多个IP
                192.168.5.111/24
        }

        track_script {  #使用在这个域中使用预先定义的脚本
                mysql_chk
        }

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template