Keepalived的工作原理就是VRRP,请你描述VRRP!!!
环境安装部署
Mysql—A:192.168.152.130 安装mysql和keepalived
Mysql—B:192.168.152.136 安装mysql和keepalived
一 、 先实现mysql互为主从环境
Mysql–A上操作:
[root]# yum -y install keepalived mysql mysql-server
[root]# service iptables stop
[root]# setenforce 0
修改mysql配置文件:
[root]# vi /etc/my.cnf
添加在【mysqld】
server-id = 1
log-bin = mysql-bin
之后保存退出,切记重启mysql
[root]# service mysqld restart
登录mysql给数据同步授权:
[root]# mysql
mysql> grant all on . to tongbu@’%’ identified by ‘tongbu’;
mysql> flush privileges;
查看log-bin日志和和pos值位置:
mysql> show master status;
下面在mysql-B上进行操作:
[root]# yum -y install keepalived mysql mysql-server
[root]# service iptables stop
[root]# setenforce 0
修改mysql配置文件:
[root]# vi /etc/my.cnf
添加在【mysqld】
server-id = 2
log-bin = mysql-bin
之后保存退出,切记重启mysql
[root]# service mysqld restart
登录mysql给数据同步授权:
[root]# mysql
mysql> grant all on . to tongbu@’%’ identified by ‘tongbu’;
mysql> flush privileges;
查看log-bin日志和和pos值位置:
mysql> show master status;
mysql> show master status;
之后接着当mysql-B作为从时:
mysql> change master to master_host=‘192.168.152.130’,master_user=‘tongbu’,master_password=‘tongbu’,master_log_file=‘mysql-bin.000003’,master_log_pos=309;
mysql> start slave;
mysql> show slave status \G;
这样mysql-B做为从,mysql—A作为主结束,实现主从同步。
现在在mysql—A上操作:
现在mysql-A作为从,mysql-B将作为主:
mysql> change master to master_host=‘192.168.152.136’,master_user=‘tongbu’,master_password=‘tongbu’,master_log_file=‘mysql-bin.000004’,master_log_pos=106;
mysql> start slave;
mysql> show slave status \G;
现在主主成功完成,需要互相新建数据验证!!!
下面开始配置mysql+keepalived高可用环境
在mysql—A上操作:
安装keepalived服务:
[root]# yum -y install keepalived
修改配置文件(修改配置文件之前记得先备份):
[root]# vi /etc/keepalived/keepalived.conf
[root]# service keepalived restart
[root]# vi /etc/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
/sbin/ifdown eth0 && /sbin/ifup eth0
[root]# chmod +x mysql.sh
这是一个检测脚本,作用是当mysql停止工作时自动关闭本机的keepalived,当MySQL正常启动之后,要手动启动keepalived服务。
Mysql-B上操作:
安装keepalived服务:
[root]# yum -y install keepalived
修改配置文件(修改配置文件之前记得先备份):
[root]# vi /etc/keepalived/keepalived.conf 注意:相对于master上的操作,只需要修改state为BACKUP,修改优先级为小于master的优先级,修改real_server为本机地址即可。
[root]# service keepalived restart
[root]# vi /etc/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
/sbin/ifdown eth0 && /sbin/ifup eth0
[root]# chmod +x mysql.sh
这是一个检测脚本,作用是当mysql停止工作时自动关闭本机的keepalived,当MySQL正常启动之后,要手动启动keepalived服务。
测试:
在mysql-A和mysql-B上分别执行ip addr命令查看mysql-B和mysql-A主机对VIP(群集虚拟ip)的控制权。
客户端通过vip登录mysql服务:
[root]# mysql -h192.168.152.100 -utongbu –ptongbu
查看现在的server-id:
mysql> show variables like ‘%server_id%’;
之后停掉现在存在vip的mysql,之后继续查看,mysql并不中断,但是id已经变了:
在mysql—A上:
[root]# service mysqld stop
[root]# service keepalived stop
之后vip自动切换到mysql-B上去了,再次在客户端查看id: