Mysql +MHA+LVS+KEEPALIVED高可用围肥,讀寫分離,負載均衡 搭建(學(xué)習(xí)筆記十八)

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主穆刻,此選項設(shè)為0,MySQL從杠步,此選項設(shè)為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主,此選項設(shè)為0幽歼,MySQL從朵锣,此選項設(shè)為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


#刪除無用賬號,設(shè)置root密碼

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

mysqladmin?-u?root?password "123456"


#創(chuàng)建復(fù)制賬號

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

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

flush?privileges;

#創(chuàng)建MHA管理賬號

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

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

flush?privileges;

#半同步復(fù)制

#2.8.1安裝相關(guān)插件

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配置相關(guān)參數(shù)#將前文提及的相關(guān)選項取消注釋

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

#重啟mysqld甸私,使配置生效

/etc/init.d/mysqld?restart


#設(shè)置主從復(fù)制

#主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

#--------------第一部分:變量及函數(shù)定義-----------------#

###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###

#--------------第一部分:變量及函數(shù)定義-----------------#


#--------------第二部分:命令行參數(shù)-----------------#

###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###

#--------------第二部分:命令行參數(shù)-----------------#


#--------------第三部分:函數(shù)調(diào)用-----------------#

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

then

status;

fi

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

then

stop;

fi


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

????????then????

????????start;

????????fi

#--------------第三部分:函數(shù)調(diào)用-----------------#


vi /usr/local/scripts/send_report

#!/bin/bash

#--------------第一部分:變量及函數(shù)定義-----------------#

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

}

#--------------第一部分:變量及函數(shù)定義-----------------#

#--------------第二部分:命令行參數(shù)-----------------#

###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###

#--------------第二部分:命令行參數(shù)-----------------#

send_report;



226-232:ssh分發(fā):

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? ? ?#檢查狀態(tài)

#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創(chuàng)建內(nèi)核目錄的鏈接文件

[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開啟路由轉(zhuǎn)發(fā)功能

[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相關(guān)文件cp

此操作的原因->keepalived服務(wù)啟動腳本默認指定了參數(shù)路徑绞吁,所以為了能夠正常啟動keepalived服務(wù)幢痘,需要執(zhí)行上述操作,當然也可以不cp目標文件家破,直接vi修改目標文件颜说,將相應(yīng)的參數(shù)指向為真實文件即可。

/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服務(wù)

[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 !發(fā)件人地址

smtp_server?127.0.0.1 !使用的郵箱服務(wù)器

smtp_connect_timeout?30 !連接smtp?server的timeout

router_id?masterha_manager_1

?}?

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


!?VRRP?configurations


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

vrrp_script?check_masterha_manager??{??!聲明一個自定義調(diào)用系統(tǒng)服務(wù)監(jiān)控腳本门粪,'check_masterha_manager'為自定義

script?"/usr/local/scripts/check_masterha_manager.sh"?!服務(wù)健康檢查腳本,如果目標服務(wù)down马僻,那么停掉本地???????????????????????????????????????????????????????????????????!?keepalived庄拇,實現(xiàn)failover

interval?3??????!腳本被調(diào)用的頻率,當前是每3秒調(diào)用一次

????????}


vrrp_sync_group?mha??{? !聲明一個vrrp?group,'mha'為自定義,group里定義了一系列有相同特性的instance

group??{?

mha_app1 !自定義instan_1?'mha_app1'

?}?

?}?


vrrp_instance?mha_app1??{??!定義一個vrrp實例韭邓,'mha_app1'為自定義措近,具體實現(xiàn)group里定義的instance

state?BACKUP !起初狀態(tài)為bakcup

interface?eth0 !虛擬ip綁定的目標網(wǎng)卡?


virtual_router_id?61?!虛擬路由ID標記,主要是個標記作用?整數(shù)型女淑,range?0~100

priority?100 !優(yōu)先級定義瞭郑,值越大,優(yōu)先級越高鸭你。整數(shù)型屈张,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??{?????????!跟蹤監(jiān)控腳本,調(diào)用之前定義的vrrp?script?'check_masterha_manager'

????????????????check_masterha_manager

?????????????????}


nopreempt?!不搶占?

smtp_alter?!?failover后通過global里的郵件設(shè)置發(fā)通知郵件

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的調(diào)度算法

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 !發(fā)件人地址

.......

router_id?masterha_manager_2

......

virtual_router_id?61?!?虛擬路由ID標記,主要是個標記作用?整數(shù)型愉老,range?0~100

priority?50 !?優(yōu)先級定義场绿,值越大,優(yōu)先級越高嫉入。整數(shù)型焰盗,range?0~255

......


3.2?check_masterha_manager腳本

#!/bin/bash

###變量及函數(shù)定義###

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的運行狀態(tài)

if [ $? -eq 2 ]?

then

MHA_OK=1

else

MHA_OK=0

fi

else

exit 0

fi

}

###變量及函數(shù)定義###

###實現(xiàn)三次檢查###

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

###實現(xiàn)三次檢查###


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:關(guān)閉?1:關(guān)閉?2:啟用?3:啟用?4:啟用?5:啟用?6:關(guān)閉




五、測試

1啟動lvsrs

4臺MySQL服務(wù)器啟動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服務(wù)

兩臺Lvs服務(wù)器Lvs-15.23(主)倦逐、Lvs-15.24(從)啟動keepalived服務(wù)

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服務(wù)停止

[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的狀態(tài):

[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'

|||

均可正常訪問

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市宫补,隨后出現(xiàn)的幾起案子檬姥,更是在濱河造成了極大的恐慌,老刑警劉巖粉怕,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件健民,死亡現(xiàn)場離奇詭異,居然都是意外死亡贫贝,警方通過查閱死者的電腦和手機秉犹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來稚晚,“玉大人崇堵,你說我怎么就攤上這事】脱啵” “怎么了鸳劳?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長也搓。 經(jīng)常有香客問我赏廓,道長,這世上最難降的妖魔是什么傍妒? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任幔摸,我火速辦了婚禮,結(jié)果婚禮上颤练,老公的妹妹穿的比我還像新娘抚太。我一直安慰自己,他們只是感情好昔案,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著电媳,像睡著了一般踏揣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上匾乓,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天捞稿,我揣著相機與錄音,去河邊找鬼。 笑死娱局,一個胖子當著我的面吹牛彰亥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播衰齐,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼任斋,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了耻涛?” 一聲冷哼從身側(cè)響起废酷,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎抹缕,沒想到半個月后澈蟆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡卓研,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年趴俘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奏赘。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡寥闪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出志珍,到底是詐尸還是另有隱情橙垢,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布伦糯,位于F島的核電站柜某,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏敛纲。R本人自食惡果不足惜喂击,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望淤翔。 院中可真熱鬧翰绊,春花似錦、人聲如沸旁壮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽抡谐。三九已至裁奇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間麦撵,已是汗流浹背刽肠。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工溃肪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人音五。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓惫撰,卻偏偏與公主長得像,于是被迫代替她去往敵國和親躺涝。 傳聞我的和親對象是個殘疾皇子厨钻,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容