搭建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 本身便是服务提供者的情况;01
!
Configuration File
for
keepalived
02
03
#简单的头部,这里主要可以做邮件通知报警等的设置,此处就暂不配置了;
04
global_defs
{
05
notificationd
LVS_DEVEL
06
}
07
08
#预先定义一个脚本,方便后面调用,也可以定义多个,方便选择;
09
vrrp_script
mysql_chk {
10
script
"/opt/scripts/mysql_chke.sh"
11
interval
2
#脚本循环运行间隔
12
weight
2
#脚本的结果导致优先级变更,成功+2
13
}
14
#VRRP虚拟路由冗余协议配置
15
vrrp_instance
VI_1 {
#VI_1
是自定义的名称;
16
state
MASTER
#表明这是一台主设备,备用设备为
BACKUP
17
interface
eth0
<code class="comments" style="word-break:break-all; padding:0px!important; margin:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; hei