Linux高可用(HA)之MySQL多主一从+Keepalived跨机房集群部署

添加host解析、时间同步和ssh互信(注:这里的做ssh互信的时候使用到一个脚本借助expect实现了面交互操作了)

[root@DS-CentOS51 ~]# echo "172.16.0.51 mysql-master01
> 172.16.0.60 mysql-master02
> 172.16.0.63 mysql-slave01
> 172.16.0.69 mysql-slave02" >> /etc/hosts
[root@DS-CentOS51 ~]# echo "*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1" >> /var/spool/cron/root && ntpdate pool.ntp.org
 8 Jan 04:12:25 ntpdate[64178]: adjust time server 128.138.141.172 offset -0.068359 sec
[root@DS-CentOS51 ~]# ssh-keygen  -t rsa -f ~/.ssh/id_rsa  -P ''
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
fc:77:72:4d:c0:ed:06:ad:09:8c:b3:d3:d4:95:dc:66 root@DS-CentOS51.dwhd.org
The key's randomart image is:
+--[ RSA 2048]----+
|              . +|
|           o o *E|
|          o + =oo|
|       .   = . * |
|        S o . o +|
|         . .   + |
|          . o o .|
|           . +   |
|                 |
+-----------------+
[root@DS-CentOS51 ~]# cat auto_auth.sh
#!/bin/bash
#########################################################################
# File Name: auto_auth.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2016年01月08日 星期五 04时18分20秒
#########################################################################
 
password=$2
rundir=`pwd`
 
if ! which expect >/dev/null 2>&1; then yum install -y -q expect;fi
 
expect << EOF
set timeout 30
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$1
expect {
    "(yes/no)" {send "yes\r"; exp_continue}
    "password:" {send "$password\r"}
}
expect eof
EOF
 
scp ${rundir}/$0 root@$1:~/
[root@DS-CentOS51 ~]# echo mysql-{master0{1,2},slave0{1,2}}| sed 's/ /\n/g'|xargs -i bash auto_auth.sh {} lookback
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-master01
root@mysql-master01's password:
Now try logging into the machine, with "ssh 'root@mysql-master01'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
auto_auth.sh                                                                                                                                  100%  606     0.6KB/s   00:00   
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-master02
root@mysql-master02's password:
Now try logging into the machine, with "ssh 'root@mysql-master02'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
auto_auth.sh                                                                                                                                  100%  606     0.6KB/s   00:00   
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-slave01
root@mysql-slave01's password:
Now try logging into the machine, with "ssh 'root@mysql-slave01'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
auto_auth.sh                                                                                                                                  100%  606     0.6KB/s   00:00   
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-slave02
root@mysql-slave02's password:
Now try logging into the machine, with "ssh 'root@mysql-slave02'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
auto_auth.sh                                                                                                                                  100%  606     0.6KB/s   00:00   
[root@DS-CentOS51 ~]#

[root@DS-CentOS60 ~]# sed -i '$d' auto_auth.sh
[root@DS-CentOS60 ~]# ssh-keygen  -t rsa -f ~/.ssh/id_rsa  -P ''
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
8e:05:99:63:7a:5e:f0:69:4f:65:23:84:16:fb:66:2e root@DS-CentOS60.dwhd.org
The key's randomart image is:
+--[ RSA 2048]----+
|        .o.      |
|       ooo       |
|      B.. . +    |
|     o = o + .   |
|    . . S =      |
|     o * *       |
|      o E o      |
|         .       |
|                 |
+-----------------+
[root@DS-CentOS60 ~]# echo mysql-{master0{1,2},slave0{1,2}}| sed 's/ /\n/g'|xargs -i bash auto_auth.sh {} lookback
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
 Package: centos-release-6-7.el6.centos.12.3.x86_64 (@CentOS/6.7)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-master01
The authenticity of host 'mysql-master01 (172.16.0.51)' can't be established.
RSA key fingerprint is 8d:44:a4:74:d1:69:08:0c:89:9b:71:ba:25:3c:ff:89.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-master01,172.16.0.51' (RSA) to the list of known hosts.
root@mysql-master01's password:
Now try logging into the machine, with "ssh 'root@mysql-master01'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-master02
The authenticity of host 'mysql-master02 (172.16.0.60)' can't be established.
RSA key fingerprint is 6a:ef:31:00:88:7f:3d:8d:56:73:e1:2b:d4:f9:61:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-master02,172.16.0.60' (RSA) to the list of known hosts.
root@mysql-master02's password:
Now try logging into the machine, with "ssh 'root@mysql-master02'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-slave01
The authenticity of host 'mysql-slave01 (172.16.0.63)' can't be established.
RSA key fingerprint is fb:7f:a8:df:a1:70:9b:88:68:d4:e1:20:2f:07:a4:78.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-slave01,172.16.0.63' (RSA) to the list of known hosts.
root@mysql-slave01's password:
Now try logging into the machine, with "ssh 'root@mysql-slave01'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-slave02
The authenticity of host 'mysql-slave02 (172.16.0.69)' can't be established.
RSA key fingerprint is 1b:5e:e7:cf:51:ed:b7:8d:f7:46:23:57:05:53:43:4b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-slave02,172.16.0.69' (RSA) to the list of known hosts.
root@mysql-slave02's password:
Now try logging into the machine, with "ssh 'root@mysql-slave02'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
[root@DS-CentOS60 ~]#

[root@DS-CentOS63 ~]# sed -i '$d' auto_auth.sh
[root@DS-CentOS63 ~]# ssh-keygen  -t rsa -f ~/.ssh/id_rsa  -P ''
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
9f:32:dd:3f:48:66:4e:53:5c:ac:c0:70:fa:9e:a7:d1 root@DS-CentOS63.dwhd.org
The key's randomart image is:
+--[ RSA 2048]----+
|          .o.  . |
|           oo   o|
|          .  o o |
|           .  +  |
|        S   ..   |
|         o +*o   |
|        o +*=oE  |
|         o  o=.  |
|            . .. |
+-----------------+
[root@DS-CentOS63 ~]# echo mysql-{master0{1,2},slave0{1,2}}| sed 's/ /\n/g'|xargs -i bash auto_auth.sh {} lookback
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
 Package: centos-release-6-7.el6.centos.12.3.x86_64 (@CentOS/6.7)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-master01
The authenticity of host 'mysql-master01 (172.16.0.51)' can't be established.
RSA key fingerprint is 8d:44:a4:74:d1:69:08:0c:89:9b:71:ba:25:3c:ff:89.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-master01,172.16.0.51' (RSA) to the list of known hosts.
root@mysql-master01's password:
Now try logging into the machine, with "ssh 'root@mysql-master01'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-master02
The authenticity of host 'mysql-master02 (172.16.0.60)' can't be established.
RSA key fingerprint is 6a:ef:31:00:88:7f:3d:8d:56:73:e1:2b:d4:f9:61:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-master02,172.16.0.60' (RSA) to the list of known hosts.
root@mysql-master02's password:
Now try logging into the machine, with "ssh 'root@mysql-master02'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-slave01
The authenticity of host 'mysql-slave01 (172.16.0.63)' can't be established.
RSA key fingerprint is fb:7f:a8:df:a1:70:9b:88:68:d4:e1:20:2f:07:a4:78.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-slave01,172.16.0.63' (RSA) to the list of known hosts.
root@mysql-slave01's password:
Now try logging into the machine, with "ssh 'root@mysql-slave01'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-slave02
The authenticity of host 'mysql-slave02 (172.16.0.69)' can't be established.
RSA key fingerprint is 1b:5e:e7:cf:51:ed:b7:8d:f7:46:23:57:05:53:43:4b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-slave02,172.16.0.69' (RSA) to the list of known hosts.
root@mysql-slave02's password:
Now try logging into the machine, with "ssh 'root@mysql-slave02'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
[root@DS-CentOS63 ~]#

[root@DS-CentOS69 ~]# sed -i '$d' auto_auth.sh
[root@DS-CentOS69 ~]# ssh-keygen  -t rsa -f ~/.ssh/id_rsa  -P ''
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
b6:60:fe:17:40:ae:69:de:48:2a:61:e0:9a:b2:02:5b root@DS-CentOS69.dwhd.org
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|        .        |
|       o         |
|.       o        |
|..    ooS.       |
|..E  o=o ..      |
|o= . =.o.  .     |
|* . . o.. .      |
|+. .    ..       |
+-----------------+
[root@DS-CentOS69 ~]# echo mysql-{master0{1,2},slave0{1,2}}| sed 's/ /\n/g'|xargs -i bash auto_auth.sh {} lookback
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
 Package: centos-release-6-7.el6.centos.12.3.x86_64 (@CentOS/6.7)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-master01
The authenticity of host 'mysql-master01 (172.16.0.51)' can't be established.
RSA key fingerprint is 8d:44:a4:74:d1:69:08:0c:89:9b:71:ba:25:3c:ff:89.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-master01,172.16.0.51' (RSA) to the list of known hosts.
root@mysql-master01's password:
Now try logging into the machine, with "ssh 'root@mysql-master01'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-master02
The authenticity of host 'mysql-master02 (172.16.0.60)' can't be established.
RSA key fingerprint is 6a:ef:31:00:88:7f:3d:8d:56:73:e1:2b:d4:f9:61:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-master02,172.16.0.60' (RSA) to the list of known hosts.
root@mysql-master02's password:
Now try logging into the machine, with "ssh 'root@mysql-master02'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-slave01
The authenticity of host 'mysql-slave01 (172.16.0.63)' can't be established.
RSA key fingerprint is fb:7f:a8:df:a1:70:9b:88:68:d4:e1:20:2f:07:a4:78.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-slave01,172.16.0.63' (RSA) to the list of known hosts.
root@mysql-slave01's password:
Now try logging into the machine, with "ssh 'root@mysql-slave01'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@mysql-slave02
The authenticity of host 'mysql-slave02 (172.16.0.69)' can't be established.
RSA key fingerprint is 1b:5e:e7:cf:51:ed:b7:8d:f7:46:23:57:05:53:43:4b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-slave02,172.16.0.69' (RSA) to the list of known hosts.
root@mysql-slave02's password:
Now try logging into the machine, with "ssh 'root@mysql-slave02'", and check in:
 
  .ssh/authorized_keys
 
to make sure we haven't added extra keys that you weren't expecting.
 
[root@DS-CentOS69 ~]#

master01上安装MariaDB

[root@DS-CentOS51 ~]# wget -q http://www.dwhd.org/wp-content/uploads/2016/01/install_mysql.sh && bash install_mysql.sh
#这是一个自动编译安装MySQL5.5 5.6 5.7、MariaDB5.5 10.0 10.1、MariaDB-Galear、Percona5.5 5.6支持主从复制集群的shell脚本

 

 

安装结束会显示相关信息

 

master02上安装MariaDB(后面节点安装都需要等Master01安装结束才可以继续)

 

1

wget -q http://www.dwhd.org/wp-content/uploads/2016/01/install_mysql.sh && bash install_mysql.sh

 

 

安装结束会显示相关信息

sslave01、slave02上安装MariaDB,和Master02上一样,由于这里是自动安装就不多演示了

Master01上安装Keepalived

[root@DS-CentOS51 ~]# wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
[root@DS-CentOS51 ~]# scp keepalived-1.2.19.tar.gz root@mysql-master02:~/
keepalived-1.2.19.tar.gz                                                                                                                      100%  322KB 322.4KB/s   00:00   
[root@DS-CentOS51 ~]# tar xf keepalived-1.2.19.tar.gz
[root@DS-CentOS51 ~]# cd keepalived-1.2.19/
[root@DS-CentOS51 ~/keepalived-1.2.19]# ./configure --prefix=/usr/local/keepalived --sysconfdir=/etc --with-kernel-dir=/usr/src/kernels/`uname -r` --enable-sha1
[root@DS-CentOS51 ~/keepalived-1.2.19]# make -j $(awk '/processor/{i++}END{print i}' /proc/cpuinfo) && make install && cd ..
[root@DS-CentOS51 ~]# echo "export PATH=/usr/local/keepalived/sbin:\$PATH" > /etc/profile.d/keepalived.sh
[root@DS-CentOS51 ~]# . /etc/profile.d/keepalived.sh
[root@DS-CentOS51 ~]# sed -i "$(awk '/^MANPATH\t/{n=NR}END{print n}' /etc/man.config)a MANPATH\t/usr/local/keepalived/share/man" /etc/man.config
[root@DS-CentOS51 ~]# mv /etc/keepalived/keepalived.conf{,_`date "+%F-%T"`_backup}
[root@DS-CentOS51 ~]# mkdir -p /etc/keepalived/scripts
[root@DS-CentOS51 ~]# ln -sv /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@DS-CentOS51 ~]# chkconfig keepalived on

Master02上安装Keepalived

[root@DS-CentOS60 ~]# tar xf keepalived-1.2.19.tar.gz
[root@DS-CentOS60 ~]# cd keepalived-1.2.19/
[root@DS-CentOS60 ~/keepalived-1.2.19]# ./configure --prefix=/usr/local/keepalived --sysconfdir=/etc --with-kernel-dir=/usr/src/kernels/`uname -r` --enable-sha1
[root@DS-CentOS60 ~/keepalived-1.2.19]# make -j $(awk '/processor/{i++}END{print i}' /proc/cpuinfo) && make install && cd ..
[root@DS-CentOS60 ~]# echo "export PATH=/usr/local/keepalived/sbin:\$PATH" > /etc/profile.d/keepalived.sh
[root@DS-CentOS60 ~]# . /etc/profile.d/keepalived.sh
[root@DS-CentOS60 ~]# sed -i "$(awk '/^MANPATH\t/{n=NR}END{print n}' /etc/man.config)a MANPATH\t/usr/local/keepalived/share/man" /etc/man.config
[root@DS-CentOS60 ~]# mv /etc/keepalived/keepalived.conf{,_`date "+%F-%T"`_backup}
[root@DS-CentOS60 ~]# mkdir -p /etc/keepalived/scripts
[root@DS-CentOS60 ~]# ln -sv /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@DS-CentOS60 ~]# chkconfig keepalived on

配置Master01上的Keepalived

[root@DS-CentOS51 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    notification_email {
        admin@dwhd.org
    }
 
    notification_email_from mysql-keepalived@ds.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id MySQL-HA
}
 
vrrp_script check_mysql {
    script "/etc/keepalived/scripts/mysql_check.sh"
    interval 2
}
 
vrrp_sync_group VG1 {
    group {
        VI_1
    }
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 88
    priority 100 
    advert_int 1
    nopreempt #不进行抢占操作
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_mysql
    }
 
    virtual_ipaddress {
        172.16.51.100 label eth0:0
    }
 
    notify_master /etc/keepalived/scripts/master.sh
    notify_backup /etc/keepalived/scripts/backup.sh
    notify_stop /etc/keepalived/scripts/stop.sh
}
[root@DS-CentOS51 ~]#

配置Master02上的Keepalived

[root@DS-CentOS60 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
    notification_email {
        admin@dwhd.org
    }
 
    notification_email_from mysql-keepalived@ds.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id MySQL-HA
}
 
vrrp_script check_mysql {
    script "/etc/keepalived/scripts/mysql_check.sh"
    #检查mysqld进程是否存活的脚本,当发现连接不上mysql,自动把keepalived进程干掉,让VIP进行漂移
    interval 2
}
 
vrrp_sync_group VG1 {
    group {
        VI_1
    }
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 88
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_mysql
    }
 
    virtual_ipaddress {
        172.16.51.100 label eth0:0
    }
 
    notify_master /etc/keepalived/scripts/master.sh #状态改变为master以后执行的脚本
    notify_backup /etc/keepalived/scripts/backup.sh #状态改变为backup以后执行的脚本
    notify_stop /etc/keepalived/scripts/stop.sh #VRRP停止以后执行的脚本
    #notify_fault /etc/keepalived/scripts/fault.sh #状态改变为fault后执行的脚本。
}
[root@DS-CentOS60 ~]#

配置Master01上的mysql_check.sh

[root@DS-CentOS51 ~]# cat /etc/keepalived/scripts/mysql_check.sh
#!/bin/bash
#########################################################################
# File Name: mysql_check.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2016年01月08日 星期五 05时12分56秒
#########################################################################
 
username=root
password=ZWE4MDM3NTgwNjc2
bindir="/usr/local/mariadb/bin"
mysql_check() { ${bindir}/mysql -u${username} -p${password} -e "select version();"; }
mysql_pid_check() { ps aux | pgrep mysqld; }
 
while :; do
    [ -n "$(mysql_check)" ] && [ -n "$(mysql_pid_check)" ] && exit 0
    [ -n "$(mysql_check)" ] && exit 0 || {
        [[ "$i" -gt "4" ]] \
            && break \
            || { let i++ && sleep 0.3 && continue; }
    }
done
 
service keepalived stop
[root@DS-CentOS51 ~]#

配置Master02上的mysql_check.sh

[root@DS-CentOS60 ~]# cat /etc/keepalived/scripts/mysql_check.sh
#!/bin/bash
#########################################################################
# File Name: /etc/keepalived/scripts/mysql_check.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2016年01月08日 星期五 06时30分53秒
#########################################################################
 
username=root
password=ZmE1MmYyZTIxNjU4
bindir="/usr/local/mariadb/bin"
 
mysql_check() { ${bindir}/mysql -u${username} -p${password} -e "select version();"; }
mysql_pid_check() { ps aux | pgrep mysqld; }
 
while :; do
    [ -n "$(mysql_check)" ] && [ -n "$(mysql_pid_check)" ] && exit 0
    [ -n "$(mysql_check)" ] && exit 0 || {
        [[ "$i" -gt "4" ]] \
            && break \
            || { let i++ && sleep 0.3 && continue; }
    }
done
 
service keepalived stop

配置Master01上的master.sh脚本

[root@DS-CentOS51 ~]# cat /etc/keepalived/scripts/master.sh
#!/bin/bash
#########################################################################
# File Name: /etc/keepalived/scripts/master.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2016年01月08日 星期五 05时33分57秒
#########################################################################
 
username=root
password=ZWE4MDM3NTgwNjc2
bindir="/usr/local/mariadb/bin"
 
Master_Log_File=$(${bindir}/mysql -u${username} -p${password} -e "show slave status\G"| awk '/ Master_Log_File/{print $2}')
Relay_Master_Log_File=$(${bindir}/mysql -u${username} -p${password} -e "show slave status\G" | awk '/Relay_Master_Log_File/{print $2}')
Read_Master_Log_Pos=$(${bindir}/mysql -u${username} -p${password} -e "show slave status\G" | awk '/Read_Master_Log_Pos/{print $2}')
Exec_Master_Log_Pos=$(${bindir}/mysql -u${username} -p${password} -e "show slave status\G" | awk '/Exec_Master_Log_Pos/{print $2}')
 
while :; do
    [ "$Master_Log_File" = "$Relay_Master_Log_File" ] && [[ "$Read_Master_Log_Pos" -eq "$Exec_Master_Log_Pos" ]] && { echo "OK" && break; }
    [ "$Master_Log_File" != "$Relay_Master_Log_File" ] || [[ "$Read_Master_Log_Pos" -ne "$Exec_Master_Log_Pos" ]] && {
        sleep 10
        [[ "$i" -gt "60" ]] && break
        let i+=10 && continue; }
done
 
${bindir}/mysql -u${username} -p${password} -e "stop slave;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_support_xa=0;"
${bindir}/mysql -u${username} -p${password} -e "set global sync_binlog=0;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_flush_log_at_trx_commit=0;"
${bindir}/mysql -u${username} -p${password} -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';flush privileges;"
${bindir}/mysql -u${username} -p${password} -e "show master status;" > /tmp/master_status_$(date "+%F-%T").txt
[root@DS-CentOS51 ~]#
配置Master02上的master.sh脚本

[root@DS-CentOS60 ~]# cat /etc/keepalived/scripts/master.sh
#!/bin/bash
#########################################################################
# File Name: /etc/keepalived/scripts/master.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2016年01月08日 星期五 05时33分57秒
#########################################################################
 
username=root
password=ZmE1MmYyZTIxNjU4
bindir="/usr/local/mariadb/bin"
 
Master_Log_File=$(${bindir}/mysql -u${username} -p${password} -e "show slave status\G"| awk '/ Master_Log_File/{print $2}')
Relay_Master_Log_File=$(${bindir}/mysql -u${username} -p${password} -e "show slave status\G" | awk '/Relay_Master_Log_File/{print $2}')
Read_Master_Log_Pos=$(${bindir}/mysql -u${username} -p${password} -e "show slave status\G" | awk '/Read_Master_Log_Pos/{print $2}')
Exec_Master_Log_Pos=$(${bindir}/mysql -u${username} -p${password} -e "show slave status\G" | awk '/Exec_Master_Log_Pos/{print $2}')
 
while :; do
    [ "$Master_Log_File" = "$Relay_Master_Log_File" ] && [[ "$Read_Master_Log_Pos" -eq "$Exec_Master_Log_Pos" ]] && { echo "OK" && break; }
    [ "$Master_Log_File" != "$Relay_Master_Log_File" ] || [[ "$Read_Master_Log_Pos" -ne "$Exec_Master_Log_Pos" ]] && {
        sleep 10
        [[ "$i" -gt "60" ]] && break
        let i+=10 && continue; }
done
 
${bindir}/mysql -u${username} -p${password} -e "stop slave;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_support_xa=0;"
${bindir}/mysql -u${username} -p${password} -e "set global sync_binlog=0;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_flush_log_at_trx_commit=0;"
${bindir}/mysql -u${username} -p${password} -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';flush privileges;"
${bindir}/mysql -u${username} -p${password} -e "show master status;" > /tmp/master_status_$(date "+%F-%T").txt

配置master01上的baskup.sh脚本

[root@DS-CentOS51 ~]# cat /etc/keepalived/scripts/backup.sh
#!/bin/bash
#########################################################################
# File Name: baskup.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2016年01月08日 星期五 05时55分05秒
#########################################################################
 
username=root
password=ZWE4MDM3NTgwNjc2
bindir="/usr/local/mariadb/bin"
 
${bindir}/mysql -u${username} -p${password} -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';flush privileges;"
${bindir}/mysql -u${username} -p${password} -e "set global event_scheduler=0;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_support_xa=0;"
${bindir}/mysql -u${username} -p${password} -e "set global sync_binlog=0;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_flush_log_at_trx_commit=0;"

配置master02上的backup.sh脚本

[root@DS-CentOS60 ~]# cat /etc/keepalived/scripts/backup.sh
#!/bin/bash
#########################################################################
# File Name: backup.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2016年01月08日 星期五 06时11分31秒
#########################################################################
 
username=root
password=ZmE1MmYyZTIxNjU4
bindir="/usr/local/mariadb/bin"
 
${bindir}/mysql -u${username} -p${password} -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';flush privileges;"
${bindir}/mysql -u${username} -p${password} -e "set global event_scheduler=0;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_support_xa=0;"
${bindir}/mysql -u${username} -p${password} -e "set global sync_binlog=0;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_flush_log_at_trx_commit=0;"

配置master01上的stop.sh脚本

[root@DS-CentOS51 ~]# cat /etc/keepalived/scripts/stop.sh
#!/bin/bash
#########################################################################
# File Name: /etc/keepalived/scripts/stop.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2016年01月08日 星期五 06时13分20秒
#########################################################################
 
username=root
password=ZWE4MDM3NTgwNjc2
bindir="/usr/local/mariadb/bin"
 
${bindir}/mysql -u${username} -p${password} -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '1q2w3e4r';flush privileges;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_support_xa=1;"
${bindir}/mysql -u${username} -p${password} -e "set global sync_binlog=1;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_flush_log_at_trx_commit=1;"
 
M_File1=$(${bindir}/mysql -u${username} -p${password} -e "show master status\G" | awk '/File/{print $2}')
M_Position1=$(${bindir}/mysql -u${username} -p${password} -e "show master status\G" | awk '/Position/{print $2}')
sleep 1
M_File2=$(${bindir}/mysql -u${username} -p${password} -e "show master status\G" | awk '/File/{print $2}')
M_Position2=$(${bindir}/mysql -u${username} -p${password} -e "show master status\G" | awk '/Position/{print $2}')
 
while :; do
    [ "$M_File1" = "$M_File1" ] && [[ "$M_Position1" -eq "$M_Position2" ]] && { echo "ok" && break; }
    [ "$M_File1" != "$M_File1" ] || [[ "$M_Position1" -ne "$M_Position2" ]] && {
        sleep 10
        [[ "$i" -gt "60" ]] && break
        let i+=10 && continue; }
done

配置master02上的stop.sh脚本

[root@DS-CentOS60 ~]# cat /etc/keepalived/scripts/stop.sh
#!/bin/bash
#########################################################################
# File Name: stop.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2016年01月08日 星期五 06时25分55秒
#########################################################################
 
username=root
password=ZmE1MmYyZTIxNjU4
bindir="/usr/local/mariadb/bin"
 
${bindir}/mysql -u${username} -p${password} -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '1q2w3e4r';flush privileges;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_support_xa=1;"
${bindir}/mysql -u${username} -p${password} -e "set global sync_binlog=1;"
${bindir}/mysql -u${username} -p${password} -e "set global innodb_flush_log_at_trx_commit=1;"
 
M_File1=$(${bindir}/mysql -u${username} -p${password} -e "show master status\G" | awk '/File/{print $2}')
M_Position1=$(${bindir}/mysql -u${username} -p${password} -e "show master status\G" | awk '/Position/{print $2}')
sleep 1
M_File2=$(${bindir}/mysql -u${username} -p${password} -e "show master status\G" | awk '/File/{print $2}')
M_Position2=$(${bindir}/mysql -u${username} -p${password} -e "show master status\G" | awk '/Position/{print $2}')
 
while :; do
    [ "$M_File1" = "$M_File1" ] && [[ "$M_Position1" -eq "$M_Position2" ]] && { echo "ok" && break; }
    [ "$M_File1" != "$M_File1" ] || [[ "$M_Position1" -ne "$M_Position2" ]] && {
        sleep 10
        [[ "$i" -gt "60" ]] && break
        let i+=10 && continue; }
done

设置master01 02上Keepalived脚本有执行权限

[root@DS-CentOS51 ~]# chmod +x /etc/keepalived/scripts/*.sh
[root@DS-CentOS51 ~]# ssh root@mysql-master02 "chmod +x /etc/keepalived/scripts/*.sh && ls -l /etc/keepalived/scripts/"
总用量 16
-rwxr-xr-x 1 root root  833 1月   8 06:12 backup.sh
-rwxr-xr-x 1 root root 1826 1月   8 06:28 master.sh
-rwxr-xr-x 1 root root  764 1月   8 06:31 mysql_check.sh
-rwxr-xr-x 1 root root 1467 1月   8 06:26 stop.sh
[root@DS-CentOS51 ~]#

时间: 2024-12-23 09:01:29

Linux高可用(HA)之MySQL多主一从+Keepalived跨机房集群部署的相关文章

Linux高可用(HA)之Heartbeat+Nginx+MySQL+NFS实现WEB+SQL服务高可用

环境说明: node1:ZhongH100.wxjr.com.cn 172.16.6.100 node2:ZhongH101.wxjr.com.cn 172.16.6.101 NFS存储:ZhongH102.wxjr.com.cn 172.16.6.102 VIP:172.16.7.200 node1和node2的主机名.SSH互通 操作系统:都是CentOS 6.6 x86_64 (自己配置好yum源.epel源.关闭SELinux.关闭防火墙) 注意:是[root@ZhongH100 ~]还

对于一个偶尔高并发的活动页面(涉及db操作,db为mysql,集群部署),你怎么做

问题描述 对于一个偶尔高并发的活动页面(涉及db操作,db为mysql,集群部署),你怎么做 对于一个偶尔高并发的活动页面(涉及db操作,db为mysql,集群部署),你怎么做 解决方案 高并发的话,可以通过据库集群.库表散列.缓存等技术: 偶尔的话,建议选择mongoDB非关系数据库.你是开发的话我想应该懂mongoDB吧. 对了景安新推出快云mongoDB,你可以去免费公测试试. 解决方案二: 高并发,一般使用负载均衡解决前端访问问题,用进程管理器解决业务逻辑调度问题,db如果是你的业务瓶颈

全是干货---Linux 高可用(HA)集群基本概念详解

http://www.linuxidc.com/Linux/2013-08/88522.htm 高可用集群的衡量标准    HA(High Available), 高可用性群集是通过系统的可靠性(reliability)和可维护性(maintainability)来度量的.工程上,通常用平均无故障时间(MTTF)来度量系统的可靠性,用平均维修时间(MTTR)来度量系统的可维护性.于是可用性被定义为:HA=MTTF/(MTTF+MTTR)*100%   具体HA衡量标准: 99% 一年宕机时间不超

Corosync+Pacemaker+DRBD+Mysql高可用HA配置

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://koumm.blog.51cto.com/703525/1769116 操作系统: CentOS 6.6 x64,本文采用rpm方式安装corosync+pacemaker+drbd,采用二进制版本安装mysql-5.6.29.本文是在Corosync+Pacemaker+DRBD+NFS高可用实例配置基础上进行配置修改,然后进行测试的安装过程. 一.双机配置 1. app1,a

5项优化4种高可用方案,MySQL常用架构调优这样做!

选择Percona Server.MariaDB还是MYSQL   1.Mysql三种存储引擎   MySQL提供了两种存储引擎:MyISAM和 InnoDB,MySQL4和5使用默认的MyISAM存储引擎.从MYSQL5.5开始,MySQL已将默认存储引擎从MyISAM更改为InnoDB.   MyISAM没有提供事务支持,而InnoDB提供了事务支持.   XtraDB是InnoDB存储引擎的增强版本,被设计用来更好的使用更新计算机硬件系统的性能,同时还包含有一些在高性能环境下的新特性.  

具备负载均衡功能MySQL服务器集群部署及实现

1. 引言 MySQL是一个高速度.高性能.多线程.开放源代码,建立在客户/服务器(Client/Server)结构上的关系型数据库管理系统(RDBMS).它始于1979年,最初是Michael Widenius为瑞典TcX公司创建的UNIREG数据库系统,当时的UNIREG没有SQL(Structured Query Language结构化查询语言)接口,限制了它的应用.1996年5月,Widenius开发出了MySQL的最初版本,开始在Internet上公开发行.MySQL的开发人员从一开始

具有负载均衡功能的MySQL服务器集群部署及实现_Mysql

在实际生产环境中,部署和实现具有一定负载均衡功能的 MySQL服务器集群,对于提高用户数据库应用系统的性能.速度和稳定性具有明显的作用.本文简要介绍了在 FreeBSD 7.0-Release系统上部署实现MySQL服务器集群的方案,并对可能出现的问题提供了相应的解决方法.1. 引言MySQL是一个高速度.高性能.多线程.开放源代码,建立在客户/服务器(Client /Server)结构上的关系型数据库管理系统(RDBMS).它始于1979年,最初是Michael Widenius为瑞典TcX公

在linux下,怎么创建多个tomcat,也就是说创建一个tomcat集群,分散一个tomcat的压力

问题描述 要求就是题目说的,不知道我说明白了没,就是建立一个tomcat集群,分散一个tomcat压力.请高手指点一下!! 解决方案 解决方案二:使用apache,用google搜索一下apahcetomcat群集,很多帖子说明怎么做的.解决方案三:具体忘了,大概就是server.xml中修改配置,windows和linux没有多大的不同

基于资源编排在专有网络环境下快速部署高可用的Dubbox服务(ZooKeeper版)

本文将介绍在专有网络VPC(Virtual Private Cloud)下,基于资源编排服务,快速部署高可用的Dubbox服务的过程.Dubbox服务采用的注册中心是ZooKeeper集群.做这件事情的意义在于:节约部署Dubbox的时间,降低部署Dubbox过程中出错的风险. ROS阿里云资源编排(Resource Orchestration)是一种简单易用的云计算资源管理和自动化运维服务.用户通过模板描述多个云计算资源的依赖关系.配置等,并自动完成所有资源的创建和配置,以达到自动化部署.运维