当前位置:   article > 正文

Keepalived与MySQL互为主从自动切换配置手册_数据库主备自动切换

数据库主备自动切换

 

目录

1.     概述... 5

1.1.      目的... 5

1.2.      环境说明... 5

2.     MySQL互为主从配置... 5

2.1.      双机hosts文件配置... 5

2.2.      主备机NTP时钟同步... 5

2.3.      双机互信配置... 5

2.4.      数据库配置... 7

2.4.1.       配置文件修改... 7

2.4.2.       创建复制账号并同步... 9

2.4.3.       配置密文命令访问... 9

2.5.      切换脚本... 10

2.5.1.       环境文件... 10

2.5.2.       检查脚本... 10

2.5.3.       切换脚本... 11

2.5.4.       回切脚本... 13

2.5.5.       停止脚本... 14

3.     Keepalived配置... 15

3.1.      切换原理... 15

3.2.      Keepalived的安装... 16

3.3.      Keepalived的配置... 17

3.3.1.       Master端配置... 17

3.3.2.       Slave端配置... 18

3.4.      修改默认输出日志位置... 19

3.5.      Keepalived启动和停止... 20

4.     切换验证... 20

4.1.      停止主端... 20

4.2.      检查状态... 20

4.3.      启动主端... 25

4.4.      回切验证... 26

5.     其它... 26

5.1.      日志的检查... 27

 

  1. 概述

1.1.     目的

为解决Mysql数据库单点问题,实现两台MySQL数据库互为主备,双向replication。当一Master出现问题,则将Slave切换为Master继续工作。

1.2.     环境说明

序号

服务器IP

用途

备注

1

10.50.15.100

主机A

Master

2

10.50.15.101

主机B

Slave

3

10.50.15.99

VIP

 

2.           MySQL互为主从配置

2.1.     双机hosts文件配置

双机hosts文件配置,需要在两台主机上分别修改/etc/hosts文件,如下所示:

10.50.15.100    MYSQL-APP-T01

10.50.15.101    MYSQL-APP-T02

2.2.     主备机NTP时钟同步

生产环境要求主备机,必须进行时钟同步。

2.3.     双机互信配置

1、  在Master主机上创建信任认证文件,如下

[root@MYSQL-APP-T01 logs]# ssh-keygen  -t  rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

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:

df:f7:66:83:ae:c1:af:ef:84:ef:b0:ff:55:d8:d9:ec root@MYSQL-APP-T01

The key's randomart image is:

+--[ RSA 2048]----+

|                 |

|                 |

|                 |

|               +o|

|        S     ..=|

|         ....  ..|

|          .=..o E|

|            Bo o+|

|           +OO.+o|

+-----------------+

 

2、  执行完成后,在/root/.ssh目录下生成如下文件

[root@MYSQL-APP-T01 .ssh]# ls -rtl

总用量 16

-rw-r--r-- 1 root root  394 7月  27 13:36 known_hosts

-rw-r--r-- 1 root root  400 7月  28 14:31 id_rsa.pub

-rw------- 1 root root 1675 7月  28 14:31 id_rsa

 

3、  在Slave主机上创建信任认证文件,如下

[root@MYSQL-APP-T02 logs]# ssh-keygen  -t  rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

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:

f5:ea:db:aa:64:06:59:8f:de:13:5f:53:a5:5f:e9:46 root@MYSQL-APP-T02

The key's randomart image is:

+--[ RSA 2048]----+

|                .|

|               .o|

|        . .   .Eo|

|       o + .  oo.|

|      o S o . oo.|

|       o . + ... |

|        = + .    |

|       + . o     |

|        ..+o.    |

+-----------------+

 

4、  执行完成后,同样也会在/root/.ssh目录下生成相同的文件。

5、  拷贝Master主机上的id_rsa.pub文件,到Slave主机的/root/.ssh/目录下,并重名为authorized_keys文件。

cd /root/.ssh

scp -r id_rsa.pub 10.50.15.101:/root/.ssh/authorized_keys

 

6、  拷贝Slave主机上的id_rsa.pub文件,到Master主机的/root/.ssh/目录下,并重名为authorized_keys文件

cd /root/.ssh

scp -r id_rsa.pub 10.50.15.100:/root/.ssh/authorized_keys

 

7、  使用scp命令随便传个文件进行测试验证,不需要输入密码即为配置正确。

2.4.     数据库配置

2.4.1.             配置文件修改

1、  修改Master主机上MySQL数据库的配置文件,然后新启动MySQL

[client]

port = 3306

socket = /mydata/data/mysql.sock

 

[mysql]

no-auto-rehash

 

[mysqld]

log-bin=mysql-bin

server-id=100

expire_logs_days = 10

 

socket = /mydata/data/mysql.sock

basedir = /mysql

datadir = /mydata/data

max_allowed_packet = 32M

explicit_defaults_for_timestamp = true

skip-ssl

secure-file-priv = NULL

lower_case_table_names = 1

back_log = 300

max_connections = 3000

max_connect_errors = 100

table_open_cache = 4096

external-locking = FALSE

sort_buffer_size = 16M

join_buffer_size = 16M

thread_cache_size = 16

query_cache_size = 128M

query_cache_limit = 4M

ft_min_word_len = 8

thread_stack = 512k

transaction_isolation = REPEATABLE-READ

tmp_table_size = 128M

max_heap_table_size = 128M

long_query_time = 6

slow_query_log

slow_query_log_file = /mydata/log/slow.log

 

[mysqldump]

quick

max_allowed_packet = 32M

 

[mysqld_safe]

open-files-limit = 8192

 

log-error = /mydata/log/mysql_3306.err

注:主要修改标黄底红字部分,其余部分为标准参数,请参考。

2、  修改Slave主机上MySQL数据库的配置文件,然后新启动MySQL。

[mysqld]

log-bin=mysql-bin

server-id=101

expire_logs_days = 10

 

3、  Master主机上和Slave主机上server-id不能一致,本文解取的IP地址最后一段。

4、  查询相关状态,以Master主机为例,如下

mysql> show variables like '%server_id%';

+----------------+-------+

| Variable_name  | Value |

+----------------+-------+

| server_id      | 100   |

| server_id_bits | 32    |

+----------------+-------+

2 rows in set (0.00 sec)

 

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 |      154 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

 

2.4.2.             创建复制账号并同步

1、  在Master库和Slave库分别执行,创建数据同步复制账号

mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO replication@'%' IDENTIFIED BY 'replication';

 

2、  在Slave主机上,执行同步操作(注意master_host参数主备机相互指向),如下:

mysql> change master to master_host='10.50.15.100',master_port=3306,master_user='replication',master_password='replication',master_log_file='mysql-bin.000001',master_log_pos=154;

注:参数据说明:

1)  master_host 要从哪个Master主机同步数据;

2)  master_port 数据库默认的连接端口

3)  master_user 数据同步复制的用户

4)  master_password 数据同步复制的用户密码

5)  master_log_file 主数据库show master status中File的值。

6)  master_log_pos 主数据库show master status中Position的值。

3、  在Slave主机上,查询同步状态“show slave status\G”,检查结果中Slave_IO_Running: Yes和Slave_SQL_Running: Yes,否则有异常。

2.4.3.             配置密文命令访问

Mysql数据库使用mysql或mysqldump等相关命令时,需要在命令行界面输入密码,当使用脚本时,在脚本里填写密码显然不太安全,因此可以设置Mysql的密文文件。

# mysql_config_editor set --login-path=local --host=localhost --user=root –password

# mysql_config_editor print --all

         使用时用参数--login-path=local即可。

2.5.     切换脚本

切换脚本规划,将所有切换脚本放在/home/sysadmin/switch目录下,如本次是mysql切换,因此在该目录下创建mysql目录。本次相关脚本说明如下:

进入/home/sysadmin/switch/mysql目录,如下文件:

Logs   //存储日志的文件目录

mybackup.sh   //清空slave配置,重新获取远程日志文件及Pos,并开启同步

mycheck.sh    //检查mysql运行状态,如果运行正常,退出。如果运行不正常调用pkill keepalived

mymaster.sh    //先判断同步复制是否执行完成,如果未执行完成等待1分钟后,停止同步(stop slave;),并且记录切换后的日志和pos

.mysqlenv   //脚本运行环境文件

mystop.sh  //设置参数保证数据不丢失,最后检查看是否还有写操作,最后1分钟退出

syncposfile  //每次切换后,Master最后一次File值和Position值。

 

2.5.1.             环境文件

1、  Master主机端的环境文件

MYSQL=/mysql/bin/mysql

MYSQL_CMD="--login-path=local"

#远端主机的IP地址

REMOTE_IP=10.50.15.101

 

export mysql="$MYSQL $MYSQL_CMD "

2、  Slave主机端的环境文件

MYSQL=/mysql/bin/mysql

MYSQL_CMD="--login-path=local"

#远端主机的IP地址

REMOTE_IP=10.50.15.100

 

export mysql="$MYSQL $MYSQL_CMD "

 

2.5.2.             检查脚本

#!/bin/sh

##################################################

#File Name  : mycheck.sh

#Date       : 2017-07-28

#Description: mysql is working MYSQL_OK is 1

#             mysql is down MYSQL_OK is 0

#Writer     :by gengjianqiu

##################################################

BASEPATH=/home/sysadmin/switch/mysql

LOGSPATH=$BASEPATH/logs

source $BASEPATH/.mysqlenv

 

CHECK_TIME=3

MYSQL_OK=1

 

function check_mysql_helth(){

        $mysql -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

                echo "$(date "+%Y-%m-%d %H:%M:%S") The mycheck.sh, mysql is running..." >> $LOGSPATH/mysql_switch.log

                exit 0

        fi

 

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

                echo "$(date "+%Y-%m-%d %H:%M:%S") The mycheck.sh, mysql is down, after switch..." >> $LOGSPATH/mysql_switch.log

                pkill keepalived

                exit 1

        fi

 

        sleep 1

done

 

2.5.3.             切换脚本

#!/bin/sh

##################################################

#File Name  : mymaster.sh

#Date       : 2017-07-28

#Description: First determine whether synchronous

#             replication is performed, and if no

#             execution is completed, wait for 1

#             minutes. Log logs and POS after

#             switching, and record files synchronously.

#Writer     :by gengjianqiu

##################################################

BASEPATH=/home/sysadmin/switch/mysql

LOGSPATH=$BASEPATH/logs

source $BASEPATH/.mysqlenv

 

$mysql -e "show slave status\G" > $LOGSPATH/mysqlslave.states

Master_Log_File=`cat $LOGSPATH/mysqlslave.states | grep -w Master_Log_File | awk -F": " '{print $2}'`

Relay_Master_Log_File=`cat $LOGSPATH/mysqlslave.states | grep -w Relay_Master_Log_File | awk -F": " '{print $2}'`

Read_Master_Log_Pos=`cat $LOGSPATH/mysqlslave.states | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}'`

Exec_Master_Log_Pos=`cat $LOGSPATH/mysqlslave.states | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}'`

 

i=1

 

while true

do

        if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ];then

                echo "$(date "+%Y-%m-%d %H:%M:%S") The mymaster.sh, slave sync ok... " >> $LOGSPATH/mysql_switch.log

                break

        else

                sleep 1

                if [ $i -gt 60 ];then

                        break

                fi

 

                continue

                let i++

        fi

done

 

 

$mysql -e "stop slave;"

$mysql -e "set global innodb_support_xa=0;"

$mysql -e "set global sync_binlog=0;"

$mysql -e "set global innodb_flush_log_at_trx_commit=0;"

$mysql -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'replication'@'%' IDENTIFIED BY 'replication';flush privileges;"

$mysql -e "show master status;" > $LOGSPATH/master_status_$(date "+%y%m%d-%H%M").txt

 

# sync pos file

/usr/bin/scp $LOGSPATH/master_status_$(date "+%y%m%d-%H%M").txt root@$REMOTE_IP:$BASEPATH/syncposfile/backup_master.status

echo "$(date "+%Y-%m-%d %H:%M:%S") The mymaster.sh, Sync pos file sucess." >> $LOGSPATH/mysql_switch.log

 

2.5.4.             回切脚本

#!/bin/sh

##################################################

#File Name  : mybackup.sh

#Date       : 2017-07-28

#Description: Empty the slave configuration, retrieve

#             the remote log file and Pos, and open

#             the synchronization

#Writer     :by gengjianqiu

##################################################

BASEPATH=/home/sysadmin/switch/mysql

LOGSPATH=$BASEPATH/logs

source $BASEPATH/.mysqlenv

 

$mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'replication'@'%' IDENTIFIED BY 'replication';flush privileges;"

$mysql -e "set global innodb_support_xa=0;"

$mysql -e "set global sync_binlog=0;"

$mysql -e "set global innodb_flush_log_at_trx_commit=0;"

$mysql -e "flush logs;"

$mysql -e "reset slave all;"

 

if [ -f $BASEPATH/syncposfile/backup_master.status ];then

        New_ReM_File=`cat $BASEPATH/syncposfile/backup_master.status | grep -v File |awk '{print $1}'`

        New_ReM_Position=`cat $BASEPATH/syncposfile/backup_master.status | grep -v File |awk '{print $2}'`

        echo "$(date "+%Y-%m-%d %H:%M:%S") This mybackup.sh, New_ReM_File:$New_ReM_File,New_ReM_Position:$New_ReM_Position" >> $LOGSPATH/mysql_switch.log

        $mysql -e "change master to master_host='$REMOTE_IP',master_port=3306,master_user='replication',master_password='replication',master_log_file='$New_ReM_File',master_log_pos=$New_ReM_Position;"

        $mysql -e "start slave;"

        $mysql -e "show slave status\G;" > $LOGSPATH/slave_status_$(date "+%y%m%d-%H%M").txt

        cat $LOGSPATH/slave_status_$(date "+%y%m%d-%H%M").txt >> $LOGSPATH/mysql_switch.log

        rm -f $BASEPATH/syncposfile/backup_master.status

else

        echo "$(date "+%Y-%m-%d %H:%M:%S") The scripts mybackup.sh running error..." > $LOGSPATH/mysql_switch.log

fi

 

2.5.5.             停止脚本

#!/bin/sh

##################################################

#File Name  : mystop.sh

#Date       : 2017-07-28

#Description: Set parameters to ensure that the data

#             is not lost, and finally check to see

#             if there are still write operations,

#             the last 1 minutes to exit

#Writer     :by gengjianqiu

##################################################

BASEPATH=/home/sysadmin/switch/mysql

LOGSPATH=$BASEPATH/logs

source $BASEPATH/.mysqlenv

 

 

$mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'replication'@'%' IDENTIFIED BY 'replication';flush privileges;"

$mysql -e "set global innodb_support_xa=1;"

$mysql -e "set global sync_binlog=1;"

$mysql -e "set global innodb_flush_log_at_trx_commit=1;"

 

$mysql -e "show master status\G" > $LOGSPATH/mysqlmaster0.states

M_File1=`cat $LOGSPATH/mysqlmaster0.states | awk -F': ' '/File/{print $2}'`

M_Position1=`cat $LOGSPATH/mysqlmaster0.states | awk -F': ' '/Position/{print $2}'`

sleep 2

$mysql -e "show master status\G" > $LOGSPATH/mysqlmaster1.states

M_File2=`cat $LOGSPATH/mysqlmaster1.states | awk -F': ' '/File/{print $2}'`

M_Position2=`cat $LOGSPATH/mysqlmaster1.states | awk -F': ' '/Position/{print $2}'`

 

i=1

 

while true

do

        if [ $M_File1 = $M_File2 ] && [ $M_Position1 -eq $M_Position2 ];then

                echo "$(date "+%Y-%m-%d %H:%M:%S") The mystop.sh, master sync ok..." >> $LOGSPATH/mysql_switch.log

                exit 0

        else

                sleep 1

                if [ $i -gt 60 ];then

                        break

                fi

 

                continue

                let i++

        fi

done

echo "$(date "+%Y-%m-%d %H:%M:%S") The mystop.sh, master sync exceed one minutes..." >> $LOGSPATH/mysql_switch.log

 

  1. Keepalived配置

3.1.     切换原理

Keepalived可实现将虚拟IP地址在实体物理机上来回漂移。Keepalived在转换状态时会依照状态来呼叫配置文件中内置的定义。

当进入Master状态时会呼叫notify_master定义的脚本

当进入Backup状态时会呼叫notify_backup定义的脚本

当keepalived程序终止时呼叫notify_stop定义的脚本

当发现异常情况时进入Fault状态呼叫notify_fault定义的脚本

切换的过程如下:

1)              在Master主机上keepalived运行时执行mycheck.sh脚本不停的检查mysql的运行状态,当发现mysql停止后将keepalived进程杀掉。

2)              此时Slave主机上会接管虚拟IP地址,并调用notify_master定义的脚本

3)              当原Master主机上的mysql和keepalived进程恢复正常后,会调用notify_backup定义的脚本,此时数据库的主端还在Savle主机上。

4)              回切,关闭Slave端的keepavlied进程,会调用notify_stop脚本,同时Master主机上会调用notify_master定义的脚本。此时数据库的主端在Master主机上

5)              启动Slave端的keepavlied进程,会调用notify_backup脚本,此时完成数据同步。

3.2.     Keepalived的安装

1、 下载安装包http://www.keepalived.org/download.html

2、 本文下载的为keepalived-1.3.5版本,并放在了/media目录下。需要安装依赖包如下

# yum install –y libnl libnl-devel libnl3 libnl3-cli libnl3-devel libnfnetlink libnfnetlink-devel

注:默认redhat6.8光盘中没有libnfnetlink-devel包,已从网上找到相关依赖包,我进行了打包keepalived_yilaibao.tar,需要可以联系我。

3、 编译keepalived安装包文件,并执行安装

# cd /media

# tar -zxvf keepalived-1.3.5.tar.gz

# cd keepalived-1.3.5

# ./configure --prefix=/usr/local/keepalived     //指定安装路径,默认为/usr/local/sbin

# 编译完成后,最后显示如下信息

Keepalived configuration

------------------------

Keepalived version       : 1.3.5

Compiler                 : gcc

Compiler flags           : -g -O2 -DFALLBACK_LIBNL1

Extra Lib                : -lssl -lcrypto -lcrypt  -lnl 

Use IPVS Framework       : Yes

IPVS sync daemon support : Yes

IPVS use libnl           : Yes

fwmark socket support    : Yes

Use VRRP Framework       : Yes

Use VRRP VMAC            : Yes

SNMP support             : No

SHA1 support             : No

Use Debug flags          : No

 

# make && make install  

 

4、 将keepalived添加到服务中,并且设置为自动启动,如下

# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

# mkdir -p /etc/keepalived

# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

# cp /media/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/

# cd /etc/rc.d/init.d/

# chkconfig --add keepalived

# chkconfig --level 345 keepalived on

 

3.3.     Keepalived的配置

在Master端和Savle端均安装好keepalived后,进行配置,修改/etc/keepalived/keepalived.conf文件。

3.3.1.             Master端配置

! Configuration File for keepalived

 

global_defs {

   router_id KeepAlive_Mysql   #标识,主从一致

}

 

vrrp_script check_run {

   script "/home/sysadmin/switch/mysql/mycheck.sh"  #Mysql状态检查脚本

   interval 10

}

 

vrrp_sync_group VG1 {

   group {

        VI_1

   }

}

 

vrrp_instance VI_1 {

    state BACKUP   # 注意,主从两端都配置成了backup,因为使用了nopreempt,即非抢占模式

 

    interface eth0

    virtual_router_id 51  #分组,主从相同

    priority 100  # 优先级,这个高一点则先把它作为master

    advert_int 1

    nopreempt  #不主动抢占资源,设置非抢占模式

    authentication {   #主从一致

        auth_type PASS

        auth_pass 1234.com

    }

    track_script {

        check_run

    }

    notify_master /home/sysadmin/switch/mysql/mymaster.sh

    notify_backup /home/sysadmin/switch/mysql/mybackup.sh

    notify_stop /home/sysadmin/switch/mysql/mystop.sh

 

    virtual_ipaddress {

        10.50.15.99/24 brd 10.50.15.255 dev eth0 label eth0:0  # 虚拟IP地址

    }

}

 

3.3.2.             Slave端配置

! Configuration File for keepalived

 

global_defs {

   router_id KeepAlive_Mysql

}

 

vrrp_script check_run {

   script "/home/sysadmin/switch/mysql/mycheck.sh"

   interval 10

}

 

vrrp_sync_group VG1 {

   group {

        VI_1

   }

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth1

    virtual_router_id 51

    priority 90

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1234.com

    }

    track_script {

        check_run

    }

    notify_master /home/sysadmin/switch/mysql/mymaster.sh

    notify_backup /home/sysadmin/switch/mysql/mybackup.sh

    notify_stop /home/sysadmin/switch/mysql/mystop.sh

 

    virtual_ipaddress {

        10.50.15.99/24 brd 10.50.15.255 dev eth1 label eth1:0

    }

}

 

3.4.     修改默认输出日志位置

主备机同时都修改,此次将keepalived的日志,计划保存在/var/log/keepalived.log文件中。

1、  修改/etc/sysconfig/keepalived参数文件

KEEPALIVED_OPTIONS="-D "  //将默认值修改为以下语句

KEEPALIVED_OPTIONS="-D -d -S 0"

 

2、  修改/etc/rsyslog.conf配置文件,在文件最后加入如下语句

local0.*                                                /var/log/keepalived.log

 

3、  重新启动相关服务

/etc/init.d/rsyslog restart

/etc/init.d/keepalived restart

 

3.5.     Keepalived启动和停止

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

#/etc/init.d/keepalived stop    //停止

 

  1. 切换验证

4.1.     停止主端

1、  将mysql进程杀死,

# pkill mysqld

4.2.     检查状态

1、  主端查看脚本切换日志/home/sysadmin/switch/mysql/logs/mysql_switch.log

2017-07-31 09:57:15 The mycheck.sh, mysql is running...

2017-07-31 09:57:25 The mycheck.sh, mysql is running...

2017-07-31 09:57:35 The mycheck.sh, mysql is running...

2017-07-31 09:57:45 The mycheck.sh, mysql is running...

2017-07-31 09:57:55 The mycheck.sh, mysql is running...

2017-07-31 09:58:05 The mycheck.sh, mysql is running...

2017-07-31 09:58:15 The mycheck.sh, mysql is running...

2017-07-31 09:58:25 The mycheck.sh, mysql is running...

2017-07-31 09:58:35 The mycheck.sh, mysql is running...

2017-07-31 09:58:47 The mycheck.sh, mysql is down, after switch...

2017-07-31 09:58:50 The mystop.sh, master sync ok...

 

2、  主端查看浮动IP地址的切换过程。

#浮动IP地址原先在Master端,如下:

 

# 切换后,在从Master端验查看,浮动IP已被切走到备机

 

# 在Slave端查看验证,确认

 

# 外部ping浮动IP地址效果,有一个丢包

 

 

3、  主端Keepalived日志/var/log/keepalived.log如下:

Jul 31 09:58:47 MYSQL-APP-T01 Keepalived_healthcheckers[25351]: Stopped

Jul 31 09:58:47 MYSQL-APP-T01 Keepalived_vrrp[25352]: /home/sysadmin/switch/mysql/mycheck.sh exited due to signal 15

Jul 31 09:58:47 MYSQL-APP-T01 Keepalived_vrrp[25352]: VRRP_Instance(VI_1) sent 0 priority

Jul 31 09:58:47 MYSQL-APP-T01 Keepalived_vrrp[25352]: VRRP_Instance(VI_1) removing protocol VIPs.

Jul 31 09:58:47 MYSQL-APP-T01 Keepalived[25350]: Stopping

Jul 31 09:58:48 MYSQL-APP-T01 Keepalived_vrrp[25352]: Stopped

Jul 31 09:58:48 MYSQL-APP-T01 Keepalived[25350]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2

 

4、  备端查看切换日志/home/sysadmin/switch/mysql/logs/mysql_switch.log

 

5、  备端,查看/var/log/keepalived.log日志

Jul 31 09:58:49 MYSQL-APP-T02 Keepalived_vrrp[28648]: VRRP_Instance(VI_1) Transition to MASTER STATE

Jul 31 09:58:50 MYSQL-APP-T02 Keepalived_vrrp[28648]: VRRP_Instance(VI_1) Entering MASTER STATE

Jul 31 09:58:50 MYSQL-APP-T02 Keepalived_vrrp[28648]: VRRP_Instance(VI_1) setting protocol VIPs.

Jul 31 09:58:50 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

Jul 31 09:58:50 MYSQL-APP-T02 Keepalived_vrrp[28648]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth1 for 10.50.15.99

Jul 31 09:58:50 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

Jul 31 09:58:50 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

Jul 31 09:58:50 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

Jul 31 09:58:50 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

Jul 31 09:58:50 MYSQL-APP-T02 Keepalived_vrrp[28648]: Opening script file /home/sysadmin/switch/mysql/mymaster.sh

Jul 31 09:58:55 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

Jul 31 09:58:55 MYSQL-APP-T02 Keepalived_vrrp[28648]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth1 for 10.50.15.99

Jul 31 09:58:55 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

Jul 31 09:58:55 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

Jul 31 09:58:55 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

Jul 31 09:58:55 MYSQL-APP-T02 Keepalived_vrrp[28648]: Sending gratuitous ARP on eth1 for 10.50.15.99

 

6、  查看主备端数据库测试表的数据

#主端

[root@MYSQL-APP-T01 ~]# mysql -uroot -p -e "use test;select * from tab1;"

+------+

| id   |

+------+

|    1 |

|    2 |

|    3 |

|    4 |

|    5 |

|    6 |

|    7 |

|    8 |

|    9 |

|   10 |

|   11 |

|   12 |

+------+

#备端

[root@MYSQL-APP-T02 ~]# mysql -uroot -p -e "use test;select * from tab1;"

+------+

| id   |

+------+

|    1 |

|    2 |

|    3 |

|    4 |

|    5 |

|    6 |

|    7 |

|    8 |

|    9 |

|   10 |

|   11 |

|   12 |

+------+

 

7、  当主端Mysql停止后,向备端Mysql插入数据

 

 

#查看插入数据的效果

[root@MYSQL-APP-T02 syncposfile]#  mysql -uroot -p-e "use test;select * from tab1;"

+------+

| id   |

+------+

|    1 |

|    2 |

|    3 |

|    4 |

|    5 |

|    6 |

|    7 |

|    8 |

|    9 |

|   10 |

|   11 |

|   12 |

|   13 |

|   14 |

|   15 |

|   16 |

|   17 |

|   18 |

|   13 |

|   14 |

|   15 |

|   16 |

|   17 |

|   18 |

+------+

 

4.3.     启动主端

1、  主端重新启动的Mysql和Keepalived的进程

#./bin/mysqld_safe --user=mysql &

2、  主端启动keepalived进程/etc/init.d/keepalived start

3、  验证主端数据是否同步成功

mysql> use test;

Database changed

mysql> select * from tab1;

+------+

| id   |

+------+

|    1 |

|    2 |

|    3 |

|    4 |

|    5 |

|    6 |

|    8 |

|    9 |

|   10 |

|   11 |

|   12 |

|    8 |

|   13 |

|   14 |

|   15 |

|   16 |

|   17 |

|   18 |

|   13 |

|   14 |

|   15 |

|   16 |

|   17 |

|   18 |

+------+

24 rows in set (0.00 sec)

说明:当主端恢复正常后,此时提供Mysql服务的还是在备机,并没有回切。

4.4.     回切验证

1、  备端停止keepalive进程 /etc/init.d/keepalived stop

2、  查看以上相关日志状态

3、  备端启动keepalive进程/etc/init.d/keepalived start

 

  1. 其它

5.1.     日志的检查

1、  切换及运行状态检查日志,/home/sysadmin/switch/mysql/logs目录下mysql_switch.log

由于keepalived每10秒检查一次mysql的运行状态,并将检查日志结果输出到mysql_switch.log中,运维过程中需要注意该日志的持续增长情况。

2、  Keepalived运行日志及切换日志 /var/log/keepalived.log

 

 

 

成为第一个赞同者

用户图标: 添加头像

编写评论...

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/675542
推荐阅读
相关标签
  

闽ICP备14008679号