Mysql +MHA+LVS+KEEPALIVED高可用,读写分离,负载均衡 搭建(学习笔记十八)

IP 主机名 角色 MHA 角色

172.16.54.226 MySQL-15.11 MySQL Master 主 Masterha-node

172.16.54.227 MySQL-15.12 MySQL Slave1从1 Masterha-node

172.16.54.228 MySQL-15.13 MySQL Slave2从2 Masterha-node

172.16.54.230 MySQL-15.14 MySQL Slave3从3 Masterha-node

172.16.54.231 Lvs-15.23 Lvs主 Masterha-manager

172.16.54.232 Lvs-15.24 Lvs 从 Masterha-manager


MySQL写的VIP:172.16.54.251

MySQL读的VIP:172.16.54.252


172.16.54.226-230: yum install -y cmake

wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz

tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz

mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql

groupadd mysql

useradd -g mysql mysql

chown -R mysql.mysql /usr/local/mysql

cd /usr/local/mysql/scripts

./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/data &

mkdir -p /data/log /data/tmp /data/data /var/run/mysql /usr/local/mysql

yum install -y gcc gcc++ git gcc-c++ ncurses-devel cmake libaio bison

echo "/usr/local/mysql/lib" >> /etc/ld.so.conf.d/mysql.conf

ldconfig


226,mysql主:vi /etc/my.cnf

[client]

socket=/var/run/mysql/mysql.sock

[mysqld]

user=mysql

pid-file=/var/run/mysql/mysqld.pid

socket=/var/run/mysql/mysql.sock

basedir=/usr/local/mysql

datadir=/data/data

tmpdir=/data/tmp

server-id=1 #每个MySQL server的此选项都必须唯一

###begin innodb settiong###

innodb_file_per_table=1

innodb_lock_wait_timeout=500

innodb_buffer_pool_size=512M

###end innodb setting###

###key buffer size set###

key-buffer-size=10M

sort_buffer_size=10M

###key buffer size set###

###begin bin log###

log-bin=/data/log/log

expire-logs-days=90

###end bin log###

###begin general log###

#general_log=1

#general_log_file=/data/log/record.log

###end general log###

###begin error log###

log_error=/data/log/error.log

###end error log###

###begin skip name resolve###

skip_name_resolve=1

###end skip name resolve###

###being slow query log###

slow_query_log=1

long_query_time=0.1

slow_query_log_file=/data/log/slow.log

###end slow query log###

###begin replication config###

read_only=0 #MySQL主,此选项设为0,MySQL从,此选项设为1

relay_log_purge=0

replicate_wild_ignore_table=information_schema.%

replicate_wild_ignore_table=performance_schema.%

#rpl_semi_sync_slave_enabled=1

#rpl_semi_sync_master_enabled=1

#rpl_semi_sync_master_timeout=1000 # 1 second

###end replication config###


227-230,从:#/etc/my.cnf

[client]

socket=/var/run/mysql/mysql.sock

[mysqld]

user=mysql

pid-file=/var/run/mysql/mysqld.pid

socket=/var/run/mysql/mysql.sock

basedir=/usr/local/mysql

datadir=/data/data

tmpdir=/data/tmp

server-id=2 #每个MySQL server的此选项都必须唯一

###begin innodb settiong###

innodb_file_per_table=1

innodb_lock_wait_timeout=500

innodb_buffer_pool_size=512M

###end innodb setting###

###key buffer size set###

key-buffer-size=10M

sort_buffer_size=10M

###key buffer size set###

###begin bin log###

log-bin=/data/log/log

expire-logs-days=90

###end bin log###

###begin general log###

#general_log=1

#general_log_file=/data/log/record.log

###end general log###

###begin error log###

log_error=/data/log/error.log

###end error log###

###begin skip name resolve###

skip_name_resolve=1

###end skip name resolve###

###being slow query log###

slow_query_log=1

long_query_time=0.1

slow_query_log_file=/data/log/slow.log

###end slow query log###

###begin replication config###

read_only=1 #MySQL主,此选项设为0,MySQL从,此选项设为1

relay_log_purge=0

replicate_wild_ignore_table=information_schema.%

replicate_wild_ignore_table=performance_schema.%

#rpl_semi_sync_slave_enabled=1

#rpl_semi_sync_master_enabled=1

#rpl_semi_sync_master_timeout=1000 # 1 second

###end replication config###



226-230:

 yum install -y perl-Module-Install.noarch

touch /data/log/error.log

chown -R mysql.mysql /data/log/error.log

chown -R mysql.mysql /data/log/

cd /usr/local/mysql/scripts

./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/data  --no-defaults &

echo 'export PATH=/usr/local/mysql/bin:$PATH'>>/etc/profile

source /etc/profile

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

 chkconfig mysqld on

/etc/init.d/mysqld start


#删除无用账号,设置root密码

mysql -e "delete from mysql.user where user=' ';delete from mysql.user where user='';flush privileges;

mysqladmin -u root password "123456"


#创建复制账号

create user repl@'172.16.54.%'  identified by '123456';

grant replication slave on *.* to repl@'172.16.54%' ;

flush privileges;

#创建MHA管理账号

create user mha_manager@'172.16.54.%' identified by '123456';

grant all on *.* to mha_manager@'172.16.54.%';

flush privileges;

#半同步复制

#2.8.1安装相关插件

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

mysql>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

#2.8.2配置相关参数#将前文提及的相关选项取消注释

/bin/sed -i 's/#rpl_semi/rpl_semi/p' /etc/my.cnf

#重启mysqld,使配置生效

/etc/init.d/mysqld restart


#设置主从复制

#主226:mysql -uroot -p123456 -e "show master status;"

#从226-230:mysql -uroot -p123456 -e "change master to master_host='172.16.54.226', master_user='repl', master_password='123456', master_port=36677, master_log_file='log.000003', master_log_pos=394;"

mysql -uroot -p123456 -e "start slave;"

mysql -uroot -p123456 -e "show slave status\G"



#MHA下载地址:https://github.com/yoshinorim/mha4mysql-node

#226-230,安装MHA:

yum install -y perl-DBD-MySQL cpan  perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-ExtUtils-Embed

cpan ExtUtils::Install

cpan Module::Install

yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

unzip mha4mysql-node-master.zip

mv mha4mysql-node-master /usr/local/

cd /usr/local/mha4mysql-node-master/

perl Makefile.PL

make&&make install

231-232:安装LVS:

yum install -y ipvsadm

yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes  cpanperl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker  perl-ExtUtils-Embed

cpan ExtUtils::Install

cpan Module::Install

unzip mha4mysql-node-master.zip

unzip mha4mysql-manager-master.zip

mv mha4mysql-node-master /usr/local/

mv mha4mysql-manager-master /usr/local/

cd /usr/local/mha4mysql-node-master/

perl Makefile.PL

make&&make install

cd /usr/local/mha4mysql-manager-master/

perl Makefile.PL

make&&make install


#配置MHA主231、232:

cat << EOF >> /etc/app1.cnf

[server default]

#mysql user and password

user=mha_manager

password=123456

port=3306

#replication user and password

repl_user=repl

repl_password=123456

#ssh user and port

ssh_user=root

ssh_port=22

#binlog directory

master_binlog_dir=/opt/data/log

#working directory on the manager

manager_workdir=/var/log/masterha/app1

#manager log file

manager_log=/var/log/masterha/app1/app1.log

#working directory on MySQL servers

remote_workdir=/var/log/masterha/app1

#If MySQL command line utilities are installed under a non-standard directory, use this option to set the directory. 

client_bindir=/usr/local/mysql/bin

#If MySQL libraries are installed under a non-standard directory, use this option to set the directory. 

client_libdir=/usr/local/mysql/lib

#To check by connecting/disconnecting every time, because it's more strict and it can detect TCP connection level failure more quickly. 

ping_type=CONNECT

#To fully control the order of priority (i.e. host2->host3->host4..)

latest_priority=0

# IP address failover solution

master_ip_failover_script=/usr/local/scripts/master_ip_failover

#Send a report (i.e. e-mail) when failover has completed or ended with errors

report_script=/usr/local/scripts/send_report

[server-Master1]

hostname=172.16.54.226

candidate_master=

[server-Master2]

hostname=172.16.54.227

candidate_master=1

[server-Slave1]

hostname=172.16.54.228

[server-Slave2]

hostname=172.16.54.230

EOF


#MHA管理,231、232: 

mkdir -p /usr/local/scripts/

vi /usr/local/scripts/master_ip_failover

#!/bin/bash

#--------------第一部分:变量及函数定义-----------------#

###Begin Variables define###

ssh_port=22

cmd=/sbin/ifconfig

vip=1

device=eno16777984:0

netmk=255.255.255.0

start_vip="${cmd} ${device} ${vip} netmask ${netmk} up"

stop_vip="${cmd} ${device} ${vip} netmask ${netmk} down"

###End Variables define###


###Begin Status Funciont###

status()

{

exit 0

}

###End Status Funciont###


###Begin Stop Or Stopssh Funciont###

stop()

{

exit 0

}

###End Stop Or Stopssh Funciont###


###Begin Start Funciont###

start()

{

/usr/bin/ssh -p ${ssh_port} ${ssh_user}@${orig_master_host} ""${stop_vip}"" 

/usr/bin/ssh -p ${ssh_port} ${ssh_user}@${new_master_host} ""${start_vip}""

/usr/bin/ssh -p ${ssh_port} ${ssh_user}@${new_master_host} "/etc/init.d/lvsrs stop"

exit 0

}

###End Start Funciont###

#--------------第一部分:变量及函数定义-----------------#


#--------------第二部分:命令行参数-----------------#

###Begin Get The Command-Line Parameters###

###eval set -- "`getopt -a -q -o n -l command::,ssh_user:,orig_master_host:,orig_master_ip:,orig_master_port:,new_master_host:,new_master_ip:,new_master_port:,new_master_user:,new_master_password: -- "$@"`"

eval set -- "`getopt -a -q -o n -l command::,ssh_user:,orig_master_host:,orig_master_ip:,new_master_host:,new_master_ip: -- "$@"`"


if [ $? != 0 ] ; then echo "Terminating..." >&2 ;exit 1;fi


while true

do

case "$1" in

--command)

command="${2}";

shift;;

--ssh_user)

ssh_user="${2}";

shift;;

--orig_master_host)

orig_master_host="${2}";

shift;;

--orig_master_ip)

orig_master_ip="${2}";

shift;;

--new_master_host)

new_master_host="${2}";

shift;;

--new_master_ip)

new_master_ip="${2}";

shift;;

--)

shift;

break;;

esac

shift

done

###End Get The Command-Line Parameters###

#--------------第二部分:命令行参数-----------------#


#--------------第三部分:函数调用-----------------#

if [ "${command}" == "status" ];

then

status;

fi

if [ "${command}" == "stop" ] || [ "${command}" == "stopssh" ] ;

then

stop;

fi


if [ "${command}" == "start" ];

        then    

        start;

        fi

#--------------第三部分:函数调用-----------------#


vi /usr/local/scripts/send_report

#!/bin/bash

#--------------第一部分:变量及函数定义-----------------#

send_report()

{

echo -e "Orig_master is ${dead_master_host}.\n New_master is ${new_master_host}.\n New_slave_hosts is ${new_slave_hosts}. \n ${body}" | mail -s ${subject} 413379253@qq.com

}

#--------------第一部分:变量及函数定义-----------------#

#--------------第二部分:命令行参数-----------------#

###Begin Get The Command-Line Parameters###

eval set -- "`getopt -a -q -o n -l dead_master_host:,body:,new_slave_hosts:,subject:,new_master_host: -- "$@"`"

if [ $? != 0 ] ; then echo "Terminating..." >&2 ;exit 1;fi

while true

do

case "$1" in

--dead_master_host)

dead_master_host="${2}";

shift;;

--body)

body="${2}";

shift;;

--new_slave_hosts)

new_slave_hosts="${2}";

shift;;

--subject)

subject="${2}";

shift;;

--new_master_host)

new_master_host="${2}";

shift;;

--)

shift;

break;;

esac

shift

done

###End Get The Command-Line Parameters###

#--------------第二部分:命令行参数-----------------#

send_report;



226-232:ssh分发:

ssh-keygen -t rsa

ssh-copy-id root@172.16.54.226

ssh-copy-id root@172.16.54.227

ssh-copy-id root@172.16.54.228

ssh-copy-id root@172.16.54.230

ssh-copy-id root@172.16.54.231

ssh-copy-id root@172.16.54.232


#MHA管理231、232 SSH、repl检查:

masterha_check_ssh --conf=/etc/app1.cnf

masterha_check_repl --conf=/etc/app1.cnf 

masterha_manager --conf=/etc/app1.cnf   #启动

masterha_check_status --conf=/etc/app1.cnf     #检查状态

#mysql 226-230:

vi /etc/cron.d/purge_relay_logs

# purge relay logs at 5am   

0 5 * * * root /usr/bin/purge_relay_logs --user=mha_manager --host=172.16.54.226 --password='123456' --port=3306 --workdir=/opt/data/tmp --disable_relay_log_purge >> /var/log/masterha/purge_relay_logs.log 2>&1



2.1创建内核目录的链接文件

[root@Lvs-15.23 ~ 13:08:34]#ls /usr/src/kernels

2.6.18-308.el5-x86_64

[root@Lvs-15.23 ~ 13:08:42]#

ln -s /usr/src/kernels/2.6.18-308.el5-x86_64  /usr/src/linux

[root@Lvs-15.23 ~ 13:09:48]#ll /usr/src/linux

lrwxrwxrwx 1 root root 38 01-04 17:54 /usr/src/linux -> 

/usr/src/kernels/2.6.18-308.el5-x86_64


2.2安装

[root@Lvs-15.23 xxm 13:13:00]#tar xf ipvsadm-1.24.tar.gz

[root@Lvs-15.23 xxm 13:13:11]#cd ipvsadm-1.24

[root@Lvs-15.23 ipvsadm-1.24 13:45:10]#make && make install


2.3开启路由转发功能

[root@Lvs-15.23 ~ 13:46:25]#/bin/sed -i 's/ip_forward = 0/ip_forward = 1/p' /etc/sysctl.conf

[root@Lvs-15.23 ~ 13:46:45]#sysctl -p


二、安装Keepalived

1下载

http://www.keepalived.org/

2安装

2.1软件安装

[root@Lvs-15.23 xxm 13:49:52]#tar xf keepalived-1.2.12.tar.gz 

[root@Lvs-15.23 xxm 13:50:02]#cd keepalived-1.2.12

[root@Lvs-15.23 keepalived-1.2.12 13:50:03]# 

./configure --prefix=/usr/local/keepalived

[root@Lvs-15.23 keepalived-1.2.12 13:51:38]#make && make install


2.2相关文件cp

此操作的原因->keepalived服务启动脚本默认指定了参数路径,所以为了能够正常启动keepalived服务,需要执行上述操作,当然也可以不cp目标文件,直接vi修改目标文件,将相应的参数指向为真实文件即可。

/bin/cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d

/bin/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig

/bin/cp /usr/local/keepalived/sbin/keepalived /usr/sbin

/bin/mkdir /etc/keepalived


2.3分离Keepalived日志文件

2.3.1 

[root@Lvs-15.23 keepalived 13:56:52]#vi /etc/sysconfig/keepalived

[root@Lvs-15.23 keepalived 13:57:54]#tail -2 /etc/sysconfig/keepalived 

KEEPALIVED_OPTIONS="-D -d -S 0"


2.3.2修改syslog.conf定义keepalived的日志

[root@Lvs-15.23 ~ 13:59:52]#tail -2 /etc/syslog.conf 

# Save keepalived messages to keepalived.log

local0.*                                                /var/log/keepalived.log


2.3.2重启syslog服务

[root@Lvs-15.23 ~ 14:00:03]#/etc/init.d/syslog restart


三、配置Keepalived.conf

3.1 keepalived.conf文件

[root@Lvs-15.23 app1 14:03:38]#cd /etc/keepalived/

[root@Lvs-15.23 keepalived 14:04:35]#vi keepalived.conf

[root@Lvs-15.23 keepalived 14:04:57]#cat keepalived.conf

! Configuration File for keepalived


! Globals configurations


!!!!!!!!!!!!===全局配置===!!!!!!!!!!!

global_defs  {  !全局配置

notification_email  {  !通知邮件的收件人列表 

test@qq.com

 } 

notification_email_from lvs-15.23@qq.com !发件人地址

smtp_server 127.0.0.1 !使用的邮箱服务器

smtp_connect_timeout 30 !连接smtp server的timeout

router_id masterha_manager_1

 } 

!!!!!!!!!!!!===全局配置===!!!!!!!!!!!


! VRRP configurations


!!!!!!!!!!!!===VRRP配置===!!!!!!!!!!!

vrrp_script check_masterha_manager  {  !声明一个自定义调用系统服务监控脚本,'check_masterha_manager'为自定义

script "/usr/local/scripts/check_masterha_manager.sh" !服务健康检查脚本,如果目标服务down,那么停掉本地                                                                   ! keepalived,实现failover

interval 3      !脚本被调用的频率,当前是每3秒调用一次

        }


vrrp_sync_group mha  {  !声明一个vrrp group,'mha'为自定义,group里定义了一系列有相同特性的instance

group  { 

mha_app1 !自定义instan_1 'mha_app1'

 } 

 } 


vrrp_instance mha_app1  {  !定义一个vrrp实例,'mha_app1'为自定义,具体实现group里定义的instance

state BACKUP !起初状态为bakcup

interface eth0 !虚拟ip绑定的目标网卡 


virtual_router_id 61 !虚拟路由ID标记,主要是个标记作用 整数型,range 0~100

priority 100 !优先级定义,值越大,优先级越高。整数型,range 0~255

advert_int 1 !检查间隔时间


authentication  {  !主从间实例的秘钥配对验证

auth_type PASS !简单的password验证

auth_pass 1111 !密码值

 } 

virtual_ipaddress  {  !进行failover的虚拟ip

! 192.168.15.96/24 label eth0:1  !也可以这样简单的写

192.168.15.96/24   !也可以这样简单的写

 } 


track_script  {         !跟踪监控脚本,调用之前定义的vrrp script 'check_masterha_manager'

                check_masterha_manager

                 }


nopreempt !不抢占 

smtp_alter ! failover后通过global里的邮件设置发通知邮件

notify_master "/usr/local/scripts/notify_master.sh"

notify_backup "/usr/local/scripts/notify_stop.sh"

notify_stop "/usr/local/scripts/notify_stop.sh"

 } 


!!!!!!!!!!!!===VRRP配置===!!!!!!!!!!!



!!!!!!!!!!!!===LVS配置===!!!!!!!!!!!

virtual_server_group MRead { 

192.168.15.96 36677


virtual_server 192.168.15.96 36677 { 

lvs_sched rr ! LVS的调度算法

lvs_method DR ! LVS的模式

protocol TCP 

nat_mask 255.255.255.0


sorry_server 192.168.15.11 36677 !备机,可做故障迁移用

real_server 192.168.15.12 36677 { 

weight 1

inhibit_on_failure

TCP_CHECK {  

connect_port 36677

connect_timeout 10

nb_get_retry 3

                delay_before_retry 1

}

real_server 192.168.15.13 36677 { 

weight 1

inhibit_on_failure

TCP_CHECK {  

connect_port 36677

connect_timeout 10

nb_get_retry 3

                delay_before_retry 1

}

real_server 192.168.15.14 35577 { 

weight 1

inhibit_on_failure

TCP_CHECK {  

connect_port 36677

connect_timeout 10

nb_get_retry 3

                delay_before_retry 1

}

!!!!!!!!!!!!===LVS配置===!!!!!!!!!!!


Lvs-15.24的keepalived配置文件有以下3处与Lvs-15.23不同,其他均一样:

[root@Lvs-15.24 keepalived 15:14:08]#cat keepalived.conf_real

......

notification_email_from lvs-15.24@qq.com !发件人地址

.......

router_id masterha_manager_2

......

virtual_router_id 61 ! 虚拟路由ID标记,主要是个标记作用 整数型,range 0~100

priority 50 ! 优先级定义,值越大,优先级越高。整数型,range 0~255

......


3.2 check_masterha_manager脚本

#!/bin/bash

###变量及函数定义###

MHA_OK=0 ##mha正常运行,MHA_OK为0,反之为1

CHECK_TIME=3 ##此次脚本检查共检查3次

VIP=192.168.4.96

function mha_check() {

/sbin/ip addr|grep ${VIP} > /dev/null 2>&1

if [ $? -eq 0  ]

then

/usr/bin/masterha_check_status --conf=/etc/app1.cnf > /dev/null 2>&1 #检查masterha_manager的运行状态

if [ $? -eq 2 ] 

then

MHA_OK=1

else

MHA_OK=0

fi

else

exit 0

fi

}

###变量及函数定义###

###实现三次检查###

while [ ${CHECK_TIME} -ne 0 ]

do

CHECK_TIME=$((${CHECK_TIME}-1))

mha_check

if [ ${MHA_OK} -eq 0 ] 

then

CHECK_TIME=0

exit 0

fi

if  [ ${MHA_OK} -eq 1 ]  && [ ${CHECK_TIME} -eq 0 ]

then

/etc/init.d/keepalived stop

        exit 1

fi

sleep 1

done

###实现三次检查###


3.3 notify_master.sh脚本

[root@Lvs-15.23 ~ 14:14:27]#vi /usr/local/scripts/notify_master.sh

[root@Lvs-15.23 ~ 14:15:44]#cat /usr/local/scripts/notify_master.sh

#!/bin/bash

scp root@Lvs-15.24:/etc/app1.cnf /etc/    #Lvs-15.24上要scp15.23的到本地

/usr/bin/nohup /usr/bin/masterha_manager --conf=/etc/app1.cnf --wait_on_monitor_error=10 --wait_on_failover_error=10 --remove_dead_master_conf < /dev/null > /var/log/masterha/app1/app1.log 2>&1 &

#/usr/bin/nohup /usr/bin/masterha_manager --conf=/etc/app1.cnf --wait_on_failover_error=10 --remove_dead_master_conf < /dev/null > /var/log/masterha/app1/app1.log 2>&1 &


3.4 notify_stop.sh脚本

[root@Lvs-15.23 ~ 14:15:47]#vi /usr/local/scripts/notify_stop.sh

[root@Lvs-15.23 ~ 14:16:41]#cat /usr/local/scripts/notify_stop.sh

#!/bin/bash

masterha_check_status --conf=/etc/app1.cnf|tr ' ' '\n'|grep pid > /dev/null 2>&1

if [ $? -eq 0  ]

then

kill -9 `masterha_check_status --conf=/etc/app1.cnf |cut -d' ' -f2|cut -d':' -f2|tr ')' ' '`

echo "kill command"

else

echo "no pid"

exit 0

fi


四、MySQL-slave上配置vip

在MySQL-15.11、MySQL-15.12、MySQL-15.13、MySQL-15.14上编写如下文件,启动并加入开机自启动


[root@MySQL-15.11 ~ 15:29:37]#vi /etc/init.d/lvsrs 

[root@MySQL-15.11 ~ 15:29:41]#ll /etc/init.d/lvsrs 

-rw-r--r-- 1 root root 1004 01-18 15:29 /etc/init.d/lvsrs

[root@MySQL-15.11 ~ 15:29:43]#chmod +x  /etc/init.d/lvsrs 

[root@MySQL-15.11 ~ 15:29:52]#cat  /etc/init.d/lvsrs 

#!/bin/bash

#chkconfig: 2345 80 90

#description:config VIP on LVS of Real Server


VIP=192.168.15.96


#./etc/rc.d/init.d/functions

case "$1" in

    start)

        echo " Start LVS  of  Real Server"

 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

 /sbin/route add -host ${VIP} dev lo:0

        echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

        echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce

        echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

        echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 

 /sbin/sysctl -p > /dev/null 2>&1

        ;;

    stop)

        /sbin/ifconfig lo:0 down

        echo "close LVS Director server"

        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce

        echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore

        echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

 /sbin/sysctl -p > /dev/null 2>&1

        ;;

    *)

        echo "Usage: $0 {start|stop}"

        exit 1

esac


[root@MySQL-15.11 ~ 15:32:06]#chkconfig lvsrs on

[root@MySQL-15.11 ~ 15:32:30]#chkconfig --list lvsrs

lvsrs           0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭




五、测试

1启动lvsrs

4台MySQL服务器启动lvsrs

注:MySQL-15.11也启动lvsrs,是因为将其定位了sorry_server,若没有此需求,当然可以不在MySQL-15.11上启动lvsrs。


以MySQL-15.12为例:

[root@MySQL-15.12 ~ 20:31:08]#/etc/init.d/lvsrs start

 Start LVS  of  Real Server

[root@MySQL-15.12 ~ 20:38:07]#ip addr|grep 15.96

    inet 192.168.15.96/32 brd 192.168.15.96 scope global lo:0


2 启动keepalived服务

两台Lvs服务器Lvs-15.23(主)、Lvs-15.24(从)启动keepalived服务

Lvs-15.23:

[root@Lvs-15.23 ~ 20:28:30]#/etc/init.d/keepalived start

启动keepalived:                                          [确定]

You have new mail in /var/spool/mail/root

[root@Lvs-15.23 ~ 20:40:07]#ip addr|grep 15.96

    inet 192.168.15.96/24 scope global secondary eth0

[root@Lvs-15.23 ~ 20:40:15]#masterha_check_status --conf=/etc/app1.cnf 

app1 (pid:17521) is running(0:PING_OK), master:192.168.15.11


Lvs-15.24:

[root@Lvs-15.24 ~ 20:40:52]#/etc/init.d/keepalived start

启动keepalived:                                          [确定]

[root@Lvs-15.24 ~ 20:40:57]#ip addr|grep 15.96

[root@Lvs-15.24 ~ 20:41:09]#masterha_check_status --conf=/etc/app1.cnf 

app1 is stopped(2:NOT_RUNNING).


3测试masterha_manager及Lvs的failover

Lvs-15.23上的keepalived服务停止

[root@Lvs-15.23 ~ 20:40:27]#/etc/init.d/keepalived stop

停止keepalived:                                          [确定]

[root@Lvs-15.23 ~ 20:42:43]#ip addr|grep 15.96

[root@Lvs-15.23 ~ 20:42:59]#masterha_check_status --conf=/etc/app1.cnf 

app1 is stopped(2:NOT_RUNNING).


查看Lvs-15.24的状态:

[root@Lvs-15.24 ~ 20:42:47]#ip addr|grep 15.96

    inet 192.168.15.96/24 scope global secondary eth0

[root@Lvs-15.24 ~ 20:43:29]#masterha_check_status --conf=/etc/app1.cnf 

app1 (pid:22419) is running(0:PING_OK), master:192.168.15.11

[root@Lvs-15.24 ~ 20:43:31]#ipvsadm -L

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.15.96:35577 rr

  -> MySQL-15.14:36677            Route   1      0          0         

  -> MySQL-15.13:36677            Route   1      0          0         

  -> MySQL-15.12:36677            Route   1      0          0        



用VIP访问试一下:

mysql -u'xxm_test' -p'123456' -h'192.168.15.96' -P'36677'

mysql -u'xxm_test' -p'123456' -h'192.168.15.95' -P'36677'

|||

均可正常访问

上一篇:阿里云发布空天数据库引擎Ganos aerospace 大幅提升空天传感数据存储和处理效率


下一篇:阿里云数据库助力智启蓝墨实现在线大容量存储与分析