Keepalived+mysql实现高可用


项目环境:
VIP 192.168.122.100
mysql1 192.168.122.10
mysql2 192.168.122.20


一、mysql 主主同步        (不使用共享存储,数据保存本地存储)

1. mysql1 192.168.122.10:
   [root@mysql1 ~]# vim /etc/my.cnf
   log-bin=mysql-bin
   server-id  = 4
   [root@mysql1 ~]# service mysqld start
   [root@mysql1 ~]# mysql
   mysql> reset master;
   mysql> grant replication slave, replication client on *.*
       -> to admin@'%' identified by '123';
   mysql> flush privileges;
   mysql> change master to
       -> master_host='192.168.122.20',
       -> master_user='admin',
       -> master_password='123',
       -> master_log_file='mysql-bin.000001',         //根据个人实际修改
       -> master_log_pos=0;                           //根据个人
   Query OK, 0 rows affected (0.02 sec)
   mysql> show slave status\G

2. mysql2 192.168.122.20:
   [root@mysql2 ~]# vim /etc/my.cnf
   log-bin=mysql-bin
   server-id  = 251
   [root@mysql2 ~]# service mysqld start
   [root@mysql2 ~]# mysql
   mysql> reset master;
   mysql> grant replication slave, replication client on *.*
       -> to admin@'%' identified by '123';
   mysql> flush privileges;
   mysql> change master to
       -> master_host='192.168.122.10',
       -> master_user='admin',
       -> master_password='123',
       -> master_log_file='mysql-bin.000001',
       -> master_log_pos=0;
   Query OK, 0 rows affected (0.02 sec)
   mysql> show slave status\G

二、安装keepalived 

[root@root ~]# yum -y install ipvsadm kernel-headers kernel-devel openssl-devel popt-devel
方法一:
源码安装keepalived
[root@root ~]# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
[root@root ~]# tar zxvf keepalived-1.2.2.tar.gz
[root@root ~]# cd keepalived-1.2.2
[root@root ~]# ./configure --prefix=/                //安装到根下,防止目录太深
[root@root ~]# make
[root@root ~]# make install
方法二:
[root@root ~]# yum -y install keepalived

三、keepalived 主备配置文件

192.168.122.10 Master配置
[root@root ~]# vim /etc/keepalived/keepalived.conf
=====================================================================
! Configuration File for keepalived

global_defs {
  router_id mysql1
}

vrrp_script check_run {
  script "/root/keepalived_check_mysql.sh"
  interval 5
}

vrrp_instance VI_1 {
   state MASTER
   interface eth0
   virtual_router_id 88
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass root
   }

   track_script {
       check_run
   }

   virtual_ipaddress {
       192.168.122.100
   }
}


192.168.122.20 Slave配置
[root@root ~]# vim /etc/keepalived/keepalived.conf
=====================================================================
! Configuration File for keepalived

global_defs {
  router_id mysql2
}

vrrp_script check_run {
  script "/root/keepalived_check_mysql.sh"
  interval 5
}

vrrp_instance VI_1 {
   state BACKUP
   interface eth0
   virtual_router_id 88
   priority 50
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass root
   }

   track_script {
       check_run
   }

   virtual_ipaddress {
       192.168.122.100
   }
}

1. 注意空格
2. 日志查看脚本是否被执行

[root@xen2 ~]# tail -f /var/log/messages
Jun 19 15:20:19 xen1 Keepalived_vrrp[6341]: Using LinkWatch kernel netlink reflector...
Jun 19 15:20:19 xen1 Keepalived_vrrp[6341]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Jun 19 15:20:19 xen1 Keepalived_vrrp[6341]: VRRP_Script(check_run) succeeded


四、mysql状态检测脚本/root/bin/keepalived_check_mysql.sh

[root@root ~]#  vim  /root/keepalived_check_mysql.sh
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root                      //连接mysql的用户名
MYSQL_PASSWORD=root                  //连接mysql的密码
CHECK_TIME=3

#mysql  is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1

check_mysql_helth (){
   $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" &>/dev/null
   if [ $? -eq 0 ] ;then
    MYSQL_OK=1
   else
    MYSQL_OK=0
   fi
   return $MYSQL_OK
}

while [ $CHECK_TIME -ne 0 ]
do
    check_mysql_helth
if [ $MYSQL_OK -eq 1 ] ; then
    exit 0
fi

let CHECK_TIME--
sleep 1
done
/etc/init.d/keepalived stop
exit 1


[root@root ~]# chmod 755 /root/keepalived_check_mysql.sh

两边均启动keepalived
[root@root ~]# /etc/init.d/keepalived start
[root@root ~]# /etc/init.d/keepalived start
[root@root ~]# chkconfig --add keepalived
[root@root ~]# chkconfig keepalived on


五、测试及诊断
keepalived之间使用vrrp组播方式通信使用的IP地址是224.0.0.18

你可能感兴趣的:(mysql,change,master,admin,identified)