实验环境:
master:192.0.2.115 (redhat7.2)
slave:192.0.2.119 (redhat7.2)
VIP:192.0.2.116
mysql:5.7.11
keepalived:1.2.13(系统盘自带版本)
一、MySQL主主互备模式配置
①master和slave两个数据库节点环境准备
分别在master和slave两节点服务器上安装mysql软件,参考rpm包方式安装mysql,此处不做详述。安装完成后,注意修改root密码和开通root用户远程登陆权限。
②master的参数文件/etc/my.cnf配置
shell>vim /etc/my.cnf
###在[mysqld]标签下增加如下内容:
[mysqld]
……
server_id = 1
log_bin = master-bin
relay_log = master-relay-bin
……
|
③slave的参数文件/etc/my.cnf配置
shell>vim /etc/my.cnf
###在[mysqld]标签下增加如下内容:
[mysqld]
……
server_id = 2
log_bin = slave-bin
relay_log = slave-relay-bin
……
|
④分别在两个mysql节点上,创建主从同步复制账户repl:
###登陆master节点的mysql服务:
shell>mysql -uroot -p
mysql>grant replication slave,replication client on *.* to ’repl’@’192.0.2.119’ identified by ’Myreplpwd4!’ with grant options;
mysql>flush privileges;
###登陆slave节点的mysql服务:
shell>mysql -uroot -p
mysql>grant replication slave,replication client on *.* to ’repl’@’192.0.2.115’ identified by ’Myreplpwd4!’ with grant options;
mysql>flush privileges;
|
⑤重启两节点的mysql服务:115和119上都要执行。
shell>systemctl restart mysqld.service
|
⑥配置主主replication机制:
###登陆master节点192.0.2.115的mysql服务,将slave节点配置成master的主库,即master去获取slave上的二进制日志。
shell>mysql -uroot -p
mysql>reset master;
mysql>stop slave;
mysql>change master to
->master_host=’192.0.2.119’,
->master_user=’repl’,
->master_password=’ Myreplpwd4!’,
->master_log_file=’slave-bin.000001’,
->master_log_pos=0;
###启动slave服务,并查看115到119的相关复制线程状态。
mysql>start slave;
mysql>show slave status\G;
###登陆slave节点192.0.2.119的mysql服务,将master节点配置成slave的主库,即slave去获取master上的二进制日志。
shell>mysql -uroot -p
mysql>reset master;
mysql>stop slave;
mysql>change master to
->master_host=’192.0.2.115’,
->master_user=’repl’,
->master_password=’ Myreplpwd4!’,
->master_log_file=’master-bin.000001’,
->master_log_pos=0;
###启动slave服务,并查看119到115的相关复制线程状态。
mysql>start slave;
mysql>show slave status\G;
|
二、安装和配置keepalived,实现mysql的高可用
①配置本地YUM源,配置方法简述如下:
【第一步】使用安装光盘作为本地YUM源,插入光盘,执行如下命令
shell>mkdir /mnt/cdrom
shell>mount /dev/cdrom /mnt/cdrom
|
【第二步】进入/etc/yum.repos.d目录,编辑或创建server.repo配置文件,内容如下
shell> vim /etc/yum.repos.d/server.repo
###添加如下内容
[server]
name=server
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
|
【第三步】执行如下命令
shell>yum clean all
shell>yum makecache
|
②安装keepalived
shell>yum -y install keepalived
|
③设置keepalived的开机自启动
shell>chkconfig --add keepalived
shell>chkconfig --level 3 5 keepalived on
|
注:以上安装keepalived操作在master和slave两个节点上都需要执行。
④配置master节点的keepalived的配置文件
###master节点的keepalived配置文件
shell>vim /etc/keepalived/keepalived.conf
##修改如下:
……
vrrp_instance VI_1 {
BACKUP #master和slave都配置成backup
enp2s0 #指定HA检测的网络接口
51 #虚拟路由标识 两节点相同均51
定义优先级 slave节点设为90
设定同步检查的时间间隔
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
设置虚拟IP,即对外服务的VIP
192.0.2.116
}
}
virtual_server 192.0.2.116 3306 { #VIP和对外服务端口
2 #监测real server状态的时间间隔
lb_algo wrr
lb_kinf DR
persistence_timeout 50
protocol TCP
192.0.2.115 3306 { #实际服务IP和端口
weight 1
服务down的后续动作
TCP_CHECK {
connect_timeout 10
192.0.2.116
nb_get_regry 3
delay_before_retry 3
connect_port 3306
}
}
}
###创建keepalived监测脚本,当本地mysql服务挂掉时,关闭本地的keepalived服务,实现VIP”漂移“到另一节点的故障切换功能
shell>mkdir -p /usr/local/keepalived/bin/
shell>vim /usr/local/keepalived/bin/mysql.sh
#!/bin/bash
pkill keepalived
###修改该脚本权限
shell>chmod 755 /usr/local/keepalived/bin/mysql.sh
|
⑤配置slave节点的keepalived的配置文件
###slave节点的keepalived配置文件
shell>vim /etc/keepalived/keepalived.conf
##修改如下:
……
vrrp_instance VI_1 {
BACKUP #master和slave都配置成backup
enp2s0 #指定HA检测的网络接口
51 #虚拟路由标识 两节点相同均51
定义优先级 master节点设为100
设定同步检查的时间间隔
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
设置虚拟IP,即对外服务的VIP
192.0.2.116
}
}
virtual_server 192.0.2.116 3306 { #VIP和对外服务端口
2 #监测real server状态的时间间隔
lb_algo wrr
lb_kinf DR
persistence_timeout 50
protocol TCP
192.0.2.119 3306 { #实际服务IP和端口
weight 1
服务down的后续动作
TCP_CHECK {
connect_timeout 10
192.0.2.116
nb_get_regry 3
delay_before_retry 3
connect_port 3306
}
}
}
###创建keepalived监测脚本,当本地mysql服务挂掉时,关闭本地的keepalived服务,实现VIP”漂移“到另一节点的故障切换功能
shell>mkdir -p /usr/local/keepalived/bin/
shell>vim /usr/local/keepalived/bin/mysql.sh
#!/bin/bash
pkill keepalived
###修改该脚本权限
shell>chmod 755 /usr/local/keepalived/bin/mysql.sh
|
⑥启动两个节点的keepalived服务
shell> systemctl start keepalived.service
shell> systemctl status keepalived.service
|
⑦查看vip是否只在其中一个节点上
如上图所示,vip漂移在slave节点119上。
三、mysql故障切换测试
①在两节点的keepalived服务都正常运行的情况,查看VIP漂移在哪个节点上
②用客户端从vip上登陆mysql,查看mysql服务的机器名
shell>mysql -h192.0.2.116 -uroot -p
mysql>show variables like ’hostname%’;
|
③关闭①步骤中确认的节点上的mysql服务,模拟该节点的mysql服务故障,确认此时VIP是否已经’漂移’到另一个节点上
###关闭当前提供服务的mysql实例
shell> mysqladmin -uroot -p shutdown;
###确认VIP的漂移情况
shell>ip addr
|
④继续在客户端上查看mysql服务的机器名
mysql> show variables like ’hostname%’;
|
四、注意事项:
①关闭linux系统的防火墙
②关闭linux系统的SELINUX服务
附录:
一、keepalived的源码编译安装方式
1、官网下载最新版keepalived
http://www.keepalived.org/download.html
shell> wget http://www.keepalived.org/software/keepalived-1.2.22.tar.gz
|
2、安装keepalived依赖软件包
shell>yum install pcre-devel openssl-devel popt-devel libnl-devel
|
3、解压并安装keepalived
shell>tar zxf keepalived-1.2.22.tar.gz
shell>cd keepalived-1.2.22
shell>./config --prefix=/usr/local/keepalived --sysconf=/etc
shell>make
shell>make install
###链接keepalived到/sbin目录
shell>ln -s /usr/local/keepalived/sbin/keepalived /sbin/
###设置keepalived的启动级别
shell>chkconfig --add keepalived
shell>chkconfig --level 3 5 keepalived on
|