发布网友 发布时间:2022-04-08 08:24
共2个回答
懂视网 时间:2022-04-08 12:45
一、安装mysql,并设置主备服务器
1、将下面的代码复制到一个后缀为.sh的shell脚本文件中(脚本很容易看懂)
# vi /etc/scripts/mysql.sh
##/bin/bash
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum -y install mysql mysql-server mysql-devel
#定义一个数值Level(设置数据库主从)
echo "Enter the "Master"(1) or "slave"=(2)"
read Level
#定义一个数值mysql(需要同步的具体某个数据库,需要同步所有数据库,就将这段以及后面有关代码删掉即可)
echo "Enter the Sync database"
read mysql
cat > /etc/my.cnf <<Eof
#做一些数据库优化
[mysql]
# CLIENT #
port = 3306
socket = /var/lib/mysql/mysql.sock
[mysqld]
# GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /var/lib/mysql/mysql.sock
pid-file = /var/lib/mysql/mysql.pid
# MyISAM #
key-buffer-size = 32M
myisam-recover = FORCE,BACKUP
# SAFETY #
max-allowed-packet = 16M
max-connect-errors = 1000000
# DATA STORAGE #
datadir = /var/lib/mysql/
# BINARY LOGGING #
log-bin = /var/lib/mysql/mysql-bin
expire-logs-days = 14
sync-binlog = 1
# REPLICATION #
relay-log = /var/lib/mysql/relay-bin
slave-net-timeout = 60
# CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type = 0
query-cache-size = 0
max-connections = 500
thread-cache-size = 50
open-files-limit = 65535
table-definition-cache = 4096
table-open-cache = 4096
# INNODB #
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 128M
innodb-flush-log-at-trx-commit = 2
innodb-file-per-table = 1
innodb-buffer-pool-size = 2G
# LOGGING #
log-error = /var/lib/mysql/mysql-error.log
log-queries-not-using-indexes = 1
slow-query-log = 1
slow-query-log-file = /var/lib/mysql/mysql-slow.log
srver-id = $Level
#需要同步整个数据库就将下面两段代码删掉
binlog-do-db = $mysql
replicate-do-db = $mysql
log-slave-updates
sync_binlog = 1
auto_increment_offset = $Level
auto_increment_increment = 2
Eof
service mysqld restart
chkconfig mysqld on
2、添加脚本执行权限
# chmod /etc/scripts/mysql.sh
二、安装keepalived
1、将如下代码复制到后缀名为.sh的shell脚本文件
# vi /etc/scripts/keepalived.sh
##!/bin/bash
yum install keepalived -y
#定义keepalived的虚拟IP
echo "VIP:"
read VIP
#定义主服务器,还是备份服务器
echo "MASTER OR BACKUP:"
read state
#定义优先级
echo "priority: (MASTER:100 BACKUP:99)"
read priority
#定义虚拟路由,主备要一样
echo "virtual_router_id:"
read virtual_router_id
cat > /etc/keepalived/keepalived.conf <<Eof
#! Configuration File for keepalived
global_defs {
#router_id mysql-ha1 #修改为自己的主机名
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk_slave.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state $state #都修改成BACKUP
interface eth0
virtual_router_id $virtual_router_id #默认51 主从都修改为60
priority $priority #在mysql-ha2上LVS上修改成80
advert_int 1
# nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_haproxy
}
virtual_ipaddress {
$VIP
}
}
Eof
echo "Enter the database password"
read password
cat /etc/keepalived/check_slave.sh <<Eof
##/bin/bash
Mysql=$(mysql -uroot -p$password -N -s -e "select 10")
if [ $? -ne 0 ] || [ "$Mysql" -ne "10" ];then
service keepalived stop
exit 1
else
SlaveStatus=($(mysql -uroot -p$password -e "show slave statusG;"|grep "_Running"|awk ‘{print $NF}‘))
if [ "$SlaveStatus[0]" = "No" ] || [ "${SlaveStatus[1]}" = "No" ];then
service keepalived stop
fi
fi
Eof
2、添加脚本执行权限
# chmod /etc/scripts/keepalived.sh
本文出自 “流星宇” 博客,谢绝转载!
keepalived+mysql主从实现数据库冗余
标签:keepalived+mysql主从实现数据库冗余
热心网友 时间:2022-04-08 09:53
keepalived的mysql主挂掉后,主上的keepalived应该调用脚本将自己的VIP down掉,从上的keepalived会检测的VIP消失,然后把自己的VIP加上,然后执行master脚本。