mysql 主主+KEEPLIVEAD

作者: admin 分类: mysql 发布时间: 2013-06-19 09:53 ė 6 没有评论

 

MYSQL 主主 KEEPALIVED配置

2台服务器

虚拟IP:10.10.0.200

物理服务器IP:10.10.0.44

物理服务器IP:10.10.0.45.

 

安装MYSQL

我配的比较规范

设置了2个relay-log=/mysql/relaylog3306/relay-bin日志存放的目录

并且加入log_slave_updates参数

开启MYSQL-BIN 修改SERVER-ID

My.cnf中replicate-do-db=repl 不写此字段代表同步所有库

将10.10.0.44设置为10.10.0.45的主服务器

在10.10.0.44上新建授权用户

grant replication slave on *.* to ‘test’@’10.10.0.45’ identified by ‘test’;

flush privileges;

flush tables with read lock;

 

show master status;查看MASTER状态查看POS

 

LOCK TABLES解锁表

登陆10.10.0.45

执行以下SQL语句,将10.10.0.44 设置为自己的主服务器

change master to master_host=’10.10.0.44′,master_user=’test’,master_password=’test’,master_log_file=’mysql-bin.000001′,master_log_pos=1219;

slave start启动从库

show slave status\G; 查看状态

 

在10.10.0.45上创建用户

登陆10.10.0.45执行

grant replication slave on *.* to ‘test’@’10.10.0.44’ identified by ‘test’;

 

在10.10.0.44 上将10.10.0.45 设为自己的主服务器

登陆10.10.0.44执行

master_host=’10.10.0.45′,master_user=’test’,master_password=’test’,master_log_file=’mysql-bin.000001′,master_log_pos=1219;

 

测试下主从是否同步

create table tb2( id    int not null, valvarchar(256));

 

分别在2个数据库中查询一下以确认相互是否同步

下面是KEEPALIVED的安装使用

登陆10.10.0.44 执行安装keepalived

wgethttp://www.keepalived.org/software/keepalived-1.2.7.tar.gz下载

./configure –prefix=/

 

make&& make install

 

修改keepalived配置文件

Vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

router_idtenddata

}

vrrp_scriptcheck_run {

script “/mysql/mysql_check.sh”

interval 5

}

vrrp_sync_group VG1 {

group {

VI_1

}

}

 

vrrp_instance VI_1 {

state MASTER

interface em1

virtual_router_id102

priority 100

advert_int 1

nopreempt

authentication {

auth_type PASS

auth_passtenddata

}

virtual_ipaddress {

10.10.0.200

}

track_script {

check_run

}

}

/etc/init.d/keepalived start 启动KEEPALIVED

 

接下来10.10.0.45 也同10.10.0.44一样安装好KEEPALIVED后

修改keepalived配置

vim /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

 

global_defs {

route_idtenddata

}

 

vrrp_scriptcheck_run {

script “/mysql/mysql_check.sh”

interval 5

}

 

vrrp_sync_group VG1 {

group {

VI_1

}

}

vrrp_instance VI_1 {

state BACKUP

interface em1

virtual_router_id102

priority 80

advert_int 1

authentication {

auth_type PASS

auth_passtenddata

}

track_script {

check_run

}

virtual_ipaddress {

10.10.0.200

}

}

 

接下来是MYSQL_CHECK.SH的脚本

#!/bin/bash

MYSQL=/usr/local/mysql3306/bin/mysql

MYSQL_HOST=localhost

MYSQL_USER=root

CHECK_TIME=3

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

MYSQL_OK=1

 

functioncheck_mysql_helth (){

$MYSQL -h $MYSQL_HOST -u $MYSQL_USER  -e “show status;” >/dev/null 2>&1

if [ $? = 0 ] ;then

MYSQL_OK=1

else

MYSQL_OK=0

fi

return $MYSQL_OK

}

 

while [ $CHECK_TIME -ne 0 ]

do

let “CHECK_TIME -= 1”

check_mysql_helth

if [ $MYSQL_OK = 1 ] ; then

CHECK_TIME=0

exit 0

fi

if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]

then

/etc/init.d/keepalived stop

exit 1

fi

sleep 1

done

至此配置完毕可以停掉其中一台MYSQL进行测试

 

 

本文出自 小Q,转载时请注明出处及相应链接。

本文永久链接: http://www.linuxqq.com/archives/1001.html

0
更多
Ɣ回顶部