MySQL双主+keepalived实现高可用实现(热备)
环境:centos6.7 最小化安装
192.168.100.152 master 主
192.168.100.153 slave 从
192.168.100.132 v_ip 浮动IP
配置ssh密码登录,关闭selinux,关闭防火墙
1、安装mysql
#yum install mysql* -y
2、配置mysql配置文件
#配置/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id = 1 //这个ID是mysql数据库的唯一标识符
log-bin = mysql-bin
log_bin_trust_function_creators = 1
binlog_format = mixed
relay-log = relay-bin
relay-log-index = slave-relay-bin.index
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-character-set=utf8
#slave_skip_errors = 1146
#binlog-ignore-db=mysql
[client]
default-character-set=utf8
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
3、启动mysqld服务(启动mysql数据库)
#service mysqld start
#chkconfig mysqld on
4、设置mysql数据库root密码
#mysqladmin -uroot password '12345678'
4、创建授权用户,配置同步。
#####################################################################################################################################################################################
master上:
mysql>grant replication slave on *.* to 'rep'@'192.168.100.153' identified by '12345678';
查看slave上的mysql的master_log_file和master_log_pos;
master_log_file=`mysql -uroot -p12345678-e "show master status;" | grep mysql | cut -f 1`
master_log_pos=`mysql -uroot -p12345678 -e "show master status;" | grep mysql | cut -f 2`
mysql>change master to master_host='192.168.100.153',master_user='rep',master_password='12345678',master_log_file="\'$fsmanager_log_file\'",master_log_pos=$fsmanager_log_pos;
mysql>start slave;
service mysqld restart
slave上:
mysql>grant replication slave on *.* to 'rep'@'192.168.100.152' identified by '12345678';
查看slave上的mysql的master_log_file和master_log_pos
master_log_file=`mysql -uroot -p12345678 -e "show master status;" | grep mysql | cut -f 1`
master_log_pos=`mysql -uroot -p12345678 -e "show master status;" | grep mysql | cut -f 2`
mysql>change master to master_host='192.168.100.152',master_user='rep',master_password='12345678',master_log_file="\'$fsmanager_log_file\'",master_log_pos=$fsmanager_log_pos;
mysql>start slave;
service mysqld restart
#####################################################################################################################################################################################
mysql -uroot -p12345678 -e "grant all privileges on *.* to 'root'@'v_ip' identified by '12345678' with grant option;"
mysql -uroot -p12345678 -e "grant all privileges on *.* to 'root'@'masterr' identified by '12345678' with grant option;"
############# 配置高可用 #########
1、安装
#yum install -y kernel-devel openssl-devel popt-devel keepalived
#######################################################
master上:
vim /etc/keepalived/keepalived.conf
global_defs {
router_id mysql1
}
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 3
}
vrrp_instance VI_1 {
state MASTER //只有MASTER和BACKUP 两种状态,都必须大写。
interface eth0 //进行通信的端口
virtual_router_id 146 //此ID是网络中的唯一 0--255之间
priority 100 //#权重,数值越大,权重越大。MASTER大于SLAVE
advert_int 1 //MASTER和SLAVE负载均衡器之间同步检查的时间间隔。单位是:秒
authentication { //MASTER和SLAVE的认证方式
auth_type PASS
auth_pass andrew
}
virtual_ipaddress { //用到的浮动IP组
192.168.100.132
}
track_script {
chk_mysql
}
}
slave上:
global_defs {
router_id mysql2
}
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 3
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 146
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass andrew
}
virtual_ipaddress {
192.168.100.132
}
track_script {
chk_mysql
}
}
#######################################################
cat >/etc/keepalived/check_mysql.sh<<-EOF
#!/bin/bash
d=`date --date today +%Y%m%d_%H:%M:%S`
n=`ps -C mysqld --no-heading|wc -l`
if [ $n -eq "0" ]; then
service mysqld start
n2=`ps -C mysqld --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d mysql down,keepalived will stop" >> /var/log/check_mysql.log
service keepalived stop
fi
fi
EOF
chmod 755 /etc/keepalived/check_mysql.sh
service keepalived restart
chkconfig keepalived on