創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的西湖網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
HA(High Available)即由兩臺計(jì)算機(jī)組成并對外提供一組相同的服務(wù),也叫做一主一備模式。正常情況下由主服務(wù)器提供服務(wù),備服務(wù)器處于待機(jī)備用,備機(jī)可以隨時(shí)接替主服務(wù)器的工作。也就是當(dāng)主服務(wù)器宕機(jī)或所提供的服務(wù)不可用時(shí),備用服務(wù)器會主動激活并且替換主服務(wù)器繼續(xù)提供服務(wù),這時(shí)主服務(wù)器上的服務(wù)資源包括網(wǎng)絡(luò)(IP)、存儲、服務(wù)(Web/數(shù)據(jù)庫)就會轉(zhuǎn)移到備機(jī)接管,從而提供不間斷的服務(wù)?;诖?,便可以將服務(wù)器的宕機(jī)時(shí)間減少到最低,對業(yè)務(wù)實(shí)現(xiàn)不中斷或短暫中斷。
由一組計(jì)算機(jī)(多臺)組成一個(gè)整體并向用戶提供相同的網(wǎng)絡(luò)資源或服務(wù),這種模式叫做HA集群(High Available Cluster)。
Oracle HA高可用,又叫做雙機(jī)熱備,一般用于關(guān)鍵性業(yè)務(wù)。
Oracle雙機(jī)熱備模式
常用的有Data Guard、RAC(Real Application Clusters)、基于HA軟件實(shí)現(xiàn)的雙機(jī)熱備。
l Data Guard采用重做日志復(fù)制技術(shù),對主業(yè)務(wù)數(shù)據(jù)進(jìn)行實(shí)時(shí)的異步同步備份,有一主一備、一主多備模式,一般常用于容災(zāi)(異地備份、災(zāi)難恢復(fù));存儲獨(dú)立、數(shù)據(jù)完整備份;備機(jī)可以做數(shù)據(jù)分析、報(bào)表統(tǒng)計(jì)等;主備自由切換,能實(shí)現(xiàn)自動快速故障轉(zhuǎn)移;備用角色強(qiáng)制轉(zhuǎn)換為主用角色會破環(huán)Data Guard架構(gòu);設(shè)計(jì)復(fù)雜、維護(hù)難度大
l RAC多個(gè)實(shí)例同時(shí)運(yùn)行,無主備概念,有集群負(fù)載功能,其中某一臺down機(jī),不影響整體服務(wù),不存在故障切換時(shí)間,可以提供高性能服務(wù);存儲共享,由ASM管理存儲;硬件成本低;設(shè)計(jì)簡單、維護(hù)難度大
l HA雙機(jī)熱備可以提供高可用性,保證業(yè)務(wù)的持續(xù)穩(wěn)定運(yùn)行,可以實(shí)現(xiàn)自動快速故障轉(zhuǎn)移,存在短暫的切換時(shí)間(10-30s)。一般用于關(guān)鍵性業(yè)務(wù);存儲共享;有商業(yè)的HA方案,也可以使用開源的高可用軟件keepalved、heartbeat搭建實(shí)現(xiàn)HA方案;設(shè)計(jì)復(fù)雜、維護(hù)難度小
l HA雙機(jī)和Data Guard
l Data Guard和RAC
l Oracle HA雙機(jī)熱備+Keepalived
l Oracle HA雙機(jī)+DataGuard+Keepalived集群
軟件版本:
Orace 11g R2
Keepalived 1.3.2
主服務(wù)器: DB1
備服務(wù)器: DB2
Hostname | IP | OS | Role |
hmdg-db1 | 172.16.10.25 | CentOS6.9 | MASTER |
hmdg-db2 | 172.16.10.26 | CentOS6.9 | BACKUP |
VIP | 172.16.10.130、172.16.10.131 | ||
Share Disk | /dev/sdb1 mount on /oradata | ||
ORACLE_BASE | /u01/app/oracle | ||
ORACLE_HOME | /u01/app/oracle/product/11.2.0/db_1 | ||
ORACLE_SID | HMODB | ||
Datadir | /oradata/HMODB | ||
Controlfiles | /oradata/HMODB/control01.ctl, /u01/app/oracle/flash_recovery_area/HMODB/control02.ctl, /home/oracle/rman/HMODB/control03.ctl |
1、 同時(shí)在主備上安裝Oracle數(shù)據(jù)庫軟件,數(shù)據(jù)庫的安裝目錄和環(huán)境變量保持一致
2、 僅在主服務(wù)器上建立數(shù)據(jù)庫實(shí)例(主服務(wù)器上掛載共享存儲并使用DBCA新建庫)
3、 主服務(wù)創(chuàng)建數(shù)據(jù)庫實(shí)例后,將主庫上的控制文件、參數(shù)文件(SPFILE)、以及密碼文件傳輸?shù)絺溆梅?wù)器上相應(yīng)的目錄
l 備用服務(wù)器DB2
##創(chuàng)建對應(yīng)的數(shù)據(jù)庫文件目錄 [root@hmdg-db2 ~]# su - oracle [oracle@hmdg-db2 ~]$ mkdir /u01/app/oracle/admin/HMODB [oracle@hmdg-db2 ~]$ mkdir /u01/app/oracle/admin/HMODB/{adump,dpdump,pfile} [oracle@hmdg-db2 ~]$ mkdir /u01/app/oracle/flash_recovery_area/HMODB ##密碼文件 [oracle@hmdg-db2 ~]$ scp oracle@hmdg-db1:/u01/app/oracle/product/11.2.0/db_1/dbs/orapwHMODB /u01/app/oracle/product/11.2.0/db_1/dbs/orapwHMODB ##SPFILE參數(shù)文件 [oracle@hmdg-db2 ~]$ scp oracle@hmdg-db1:/u01/app/oracle/product/11.2.0/db_1/dbs/spfileHMODB.ora /u01/app/oracle/product/11.2.0/db_1/dbs/spfileHMODB.ora ##控制文件,僅需要將位于非共享存儲的數(shù)據(jù)目錄下的控制文件傳輸?shù)狡渌疟P或目錄位置下 [oracle@hmdg-db2 ~]$ scp oracle@hmdg-db1:/u01/app/oracle/flash_recovery_area/HMODB/control02.ctl /u01/app/oracle/flash_recovery_area/HMODB/control02.ctl [oracle@hmdg-db2 ~]$cp /u01/app/oracle/flash_recovery_area/HMODB/control02.ctl /home/oracle/rman/HMODB/control03.ctl
1、 主服務(wù)器上關(guān)閉數(shù)據(jù)庫實(shí)例
2、 主服務(wù)器上卸載共享存儲
3、 備服務(wù)器上掛載共享存儲
4、 備服務(wù)器上啟動監(jiān)聽
5、 備服務(wù)器上啟動數(shù)據(jù)庫實(shí)例
注意:不要同時(shí)掛載共享存儲啟動數(shù)據(jù)庫實(shí)例,否則會導(dǎo)致數(shù)據(jù)不一致
l 主服務(wù)器DB1
##關(guān)閉數(shù)據(jù)庫實(shí)例 [oracle@hmdg-db1 ~]$ sqlplus / as sysdba SQL> shutdown immediate ##關(guān)閉數(shù)據(jù)庫監(jiān)聽 [oracle@hmdg-db1 ~]$ lsnrctl stop ##卸載共享存儲 [oracle@hmdg-db1 ~]$ umount /oradata
l 備服務(wù)器DB2
##掛載/dev/sdb1共享存儲到/oradta下 [root@hmdg-db2 ~]# mount /dev/sdb1 /oradata/ ##啟動數(shù)據(jù)庫監(jiān)聽 [oracle@hmdg-db2 ~]$ lsnrctl start [oracle@hmdg-db2 ~]$ export ORACLE_SID=HMODB ##啟動數(shù)據(jù)庫實(shí)例 [oracle@hmdg-db2 ~]$ sqlplus / as sysdba SQL> startup ORACLE instance started. Total System Global Area 1603411968 bytes Fixed Size 2213776 bytes Variable Size 1056966768 bytes Database Buffers 536870912 bytes Redo Buffers 7360512 bytes Database mounted. Database opened. SQL> ##檢驗(yàn)數(shù)據(jù)庫 SQL> SHOW PARAMETER DB_NAME NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ db_name string HMODB SQL> SELECT OPEN_MODE,NAME,DATABASE_ROLE FROM V$DATABASE; OPEN_MODE NAME DATABASE_ROLE -------------------- --------- ---------------- READ WRITE HMODB PRIMARY
由于安裝數(shù)據(jù)庫實(shí)例時(shí),創(chuàng)建了多個(gè)控制文件,并且這些控制文件都保存在不同的磁盤上,在主備進(jìn)行切換的時(shí),位于非共享存儲的控制文件需要手動進(jìn)行同步。這是由于在關(guān)閉數(shù)據(jù)庫實(shí)例時(shí)Oracle同時(shí)將當(dāng)前數(shù)據(jù)庫狀態(tài)信息記錄到控制文件中,在沒有啟動數(shù)據(jù)庫實(shí)例的備機(jī)就無法同步自身的其他的控制文件。這時(shí)就需要使用共享存儲里的控制文件進(jìn)行恢復(fù)。
1、 在DB2關(guān)閉正在運(yùn)行數(shù)據(jù)庫實(shí)例
##DB2: [oracle@hmdg-db2 ~]$ sqlplus / as sysdba SQL> shutdown immediate [root@hmdg-db2 ~]# umount /oradata/
2、在DB1上進(jìn)行恢復(fù)啟動數(shù)據(jù)庫實(shí)例
##DB1: [root@hmdg-db1 ~]# mount /dev/sdb1 /oradata/ [oracle@hmdg-db1 ~]$ export ORACLE_SID=HMODB [oracle@hmdg-db1 ~]$ sqlpus / as sysdba SQL> startup nomount SQL> alter database mount; SQL> alter database open;
如果控制文件在不同的磁盤目錄,那么就需要執(zhí)行這一步恢復(fù)控制文件
[oracle@hmdg-db1 ~]$ rman target / RMAN> restore controlfile from '/oradata/HMODB/control01.ctl';
啟動數(shù)據(jù)庫監(jiān)聽
[oracle@hmdg-db1 ~]$ lsnrctl start
或者直接只有操作系統(tǒng)的cp、scp等命令進(jìn)行拷貝恢復(fù)
[oracle@hmdg-db1 ~]$ cp /oradata/HMODB/control01.ctl /u01/app/oracle/flash_recovery_area/HMODB/control02.ctl SQL> SELECT NAME,OPEN_MODE,NAME,DATABASE_ROLE FROM V$DATABASE; NAME OPEN_MODE NAME DATABASE_ROLE --------- -------------------- --------- ---------------- HMODB READ WRITE HMODB PRIMARY
配置oracle用戶的ssh密鑰,用于主備通過oracle身份自動檢測對方的數(shù)據(jù)共享存儲狀態(tài)。在keepalived腳本管理中自動檢查對方的掛載狀態(tài),并在主備切換的過程中,待接管的服務(wù)器等待對方umount共享存儲之后,正常啟動數(shù)據(jù)庫實(shí)例。(如果主服務(wù)異常關(guān)機(jī)或網(wǎng)絡(luò)中斷,不需要等待對方umount存儲,主服務(wù)器異常關(guān)機(jī)時(shí)keepalived會即時(shí)檢測到并自動切換到備機(jī)上)
[oracle@hmdg-db1 ~]$ ssh-keygen -t rsa -b 2048 [oracle@hmdg-db1 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub oracle@hmdg-db2 [oracle@hmdg-db2 ~]$ ssh-keygen -t rsa -b 2048 [oracle@hmdg-db2 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub oracle@hmdg-db1
添加主備防火墻互通規(guī)則
# iptables -I INPUT -p tcp -s 172.16.10.0/24 -j ACCEPT # iptables -I INPUT -p udp -s 172.16.10.0/24 -j ACCEPT
主服務(wù)器的keepalived配置,備用服務(wù)器需要做相應(yīng)的修改
! Configuration File for keepalived global_defs { notification_email { mail@huangming.org } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ORACLE_HA_MASTER #backup為ORACLE_HA_BACKUP !vrrp_skip_check_adv_addr } vrrp_sync_group ORACLE_HA_GROUP { group { Oracle_HA_1 #定義一個(gè)vrrp實(shí)例組 } } vrrp_script monitor_oracle_status { #oracle數(shù)據(jù)庫服務(wù)器狀態(tài)檢查腳本 script "/usr/local/keepalived/scripts/monitor.sh" interval 10 #腳本執(zhí)行時(shí)間間隔 fall 3 #腳本執(zhí)行的最大失敗次數(shù) rise 1 #腳本執(zhí)行成功一次則返回正常狀態(tài) weight 20 #腳本執(zhí)行成功時(shí)的附加權(quán)值 } vrrp_script change_monitor_status { #服務(wù)器狀態(tài)監(jiān)控并修改主備檢查腳本 script "/usr/local/keepalived/scripts/change_monitor_status.sh" interval 2 } vrrp_instance Oracle_HA_1 { state BACKUP #主備都設(shè)置為BACKUP interface eth2 #綁定vip的網(wǎng)絡(luò)接口 virtual_router_id 200 #主備保存一致 priority 100 #備為90 advert_int 2 #主備狀態(tài)同步時(shí)間間隔 nopreempt #主設(shè)置為不搶占模式 authentication { auth_type PASS auth_pass 11112222 } virtual_ipaddress { 172.16.10.130/24 dev eth2 #需要綁定的VIP 172.16.10.131/24 dev eth2 } track_script { monitor_oracle_status change_monitor_status } track_interface { eth0 #同時(shí)監(jiān)控檢測其他的網(wǎng)接口 } notify_master "/usr/local/keepalived/scripts/keepalived_notify.sh master" notify_backup "/usr/local/keepalived/scripts/keepalived_notify.sh backup" notify_fault "/usr/local/keepalived/scripts/keepalived_notify.sh fault" notify_stop "/usr/local/keepalived/scripts/keepalived_notify.sh stop" ! notify管理腳本的定義 ! 分別對應(yīng)keepalived轉(zhuǎn)換為master、backup、fault、stop狀態(tài)時(shí)執(zhí)行的腳本 ! 在oracle的雙機(jī)管理中主要是依賴這些腳本實(shí)現(xiàn)資源的接管 }
[root@hmdg-db1 scripts]# ls -l /usr/local/keepalived/scripts/ -rwxr-xr-x 1 root root 818 Apr 14 20:13 change_monitor_status.sh -rw-r--r-- 1 root root 57 Apr 14 20:13 Controlfile.sql -rw-r--r-- 1 root root 1308 Apr 14 20:13 keepalived -rwxr-xr-x 1 root root 10549 Apr 14 23:18 keepalived_notify.sh -rwxr-xr-x 1 root root 926 Apr 14 23:12 monitor_primary_oracle.sh -rwxr-xr-x 1 root root 895 Apr 15 16:46 monitor.sh -rwxr-xr-x 1 root root 895 Apr 14 20:13 monitor_standby_oracle.sh -rw-r--r-- 1 root root 1657 Apr 15 15:13 oracle_init.sh -rwxr-xr-x 1 root root 8441 Apr 14 20:13 notify.sh_20180413
關(guān)于這些keepalived腳本,由于篇幅有限我沒有全部貼到本文中,大家可以訪問我的Github倉庫找到:https://github.com/hmlinux/oracle-keepalived
l keepalived_notify.sh #keepalived的notify腳本,用于實(shí)現(xiàn)主備的資源自動切換
l monitor.sh #keepalved的監(jiān)控腳本,監(jiān)控oracle數(shù)據(jù)庫運(yùn)行狀態(tài)、共享存儲狀態(tài)
l change_monitor_status.sh #異?;蛘z測腳本,當(dāng)備服務(wù)器處于正常運(yùn)行狀態(tài),并且處于keepalived的BACKUP狀態(tài)時(shí),將自身的監(jiān)控腳本修改為在備機(jī)就緒時(shí)執(zhí)行都返回正常結(jié)果,主要作用時(shí)讓正常的備機(jī)可以隨時(shí)接管主服務(wù)器的資源
l monitor_primary_oracle.sh #處于MASTER狀態(tài)時(shí)的服務(wù)器監(jiān)控腳本
l monitor_standby_oracle.sh #處于BACKUP狀態(tài)時(shí)的服務(wù)器監(jiān)控腳本
l oracle_init.sh #notify腳本調(diào)用oracle啟動、停止、狀態(tài)檢測命令
keepalived_notify.sh
#!/bin/bash # author: hm@huangming.org # keepalived notify script #bind vip interface interface="eth2" #keepalived virtual_ipaddress virtual_ipaddress="172.16.10.130","172.16.10.131" MASTER_HOSTNAME="hmdg-db1" #DB1 BACKUP_HOSTNAME="hmdg-db2" #DB2 LOGDIR="/usr/local/keepalived/log" LOGFILE="$LOGDIR/keepalived_haswitch.log" TMPLOG=/tmp/notify_.log sharedisk="/dev/sdb1" sharedisk_mount_point="/oradata" # oracle_datadir="/oradata/path/HMODB" oracle_datadir="/oradata" # Source oracle database instance startup/shutdown script . /usr/local/keepalived/scripts/oracle_init.sh control_files="/$oracle_datadir/$ORACLE_SID/control01.ctl","/u01/app/oracle/flash_recovery_area/$ORACLE_SID/control02.ctl","/home/oracle/rman/$ORACLE_SID/control03.ctl" RETVAL=0 OLD_IFS="$IFS" IFS="," [ -d $LOGDIR ] || mkdir $LOGDIR controlfile_backpath="/backup/oracle/control" controlfile_back=$controlfile_backpath/control_$(date '+%Y%d%m%H%M%S') [ -d $controlfile_backpath ] || mkdir -p $controlfile_backpath && chown -R oracle:oinstall $controlfile_backpath info_log() { printf "$(date '+%b %d %T %a') $HOSTNAME [keepalived_notify]: $1" } control01_ctl=`printf ${control_files[0]}` backup_controlfile() { su - oracle << EOF export ORACLE_SID=$ORACLE_SID $ORACLE_HOME/bin/sqlplus -S "/ as sysdba" alter database backup controlfile to '$controlfile_back'; exit EOF } #runuser -l oracle -c "export ORACLE_SID=$ORACLE_SID;rman target / cmdfile=/usr/scripts/ControlfileRestore.sql" restore_controlfile() { su - oracle << EOF export ORACLE_SID=$ORACLE_SID $ORACLE_HOME/bin/rman target / nocatalog RESTORE CONTROLFILE FROM '$control01_ctl'; exit EOF } ssh_p=$(netstat -ntp | awk '/sshd/{print $4}' | awk -F':' '{print $2}' | head -1) chk_remote_node_sharedisk() { if [ $(hostname) == $MASTER_HOSTNAME ];then if ping -c1 -w2 $BACKUP_HOSTNAME &>/dev/null;then runuser -l oracle -c "ssh -p$ssh_p $BACKUP_HOSTNAME 2>/dev/null df | grep $sharedisk | wc -l" fi fi if [ $(hostname) == $BACKUP_HOSTNAME ];then if ping -c1 -w2 $MASTER_HOSTNAME &>/dev/null;then runuser -l oracle -c "ssh -p$ssh_p $MASTER_HOSTNAME 2>/dev/null df | grep $sharedisk | wc -l" fi fi } master() { info_log "Database Switchover To MASTER\n" info_log "Check remote node sharedisk mounted.\n" i=1 while (($i<=30)) do chk_status=$(chk_remote_node_sharedisk) if [ $chk_status -ge 1 ];then info_log "$sharedisk is already mounted on remote node or busy. checking [$i]...\n" else info_log "$sharedisk check passed.\n" break fi if [ $i -eq 20 ];then info_log "Disk status abnormal.\n" exit 1 fi sleep 1 i=$(($i+1)) done ismount=$(df -h | grep $sharedisk | grep $sharedisk_mount_point | wc -l) if [ $ismount -eq 0 ];then info_log "mount $sharedisk on $sharedisk_mount_point\n" mount $sharedisk $sharedisk_mount_point RETVAL=$? if [ $RETVAL -eq 0 ];then #shutdown_instance info_log "restore controlfile 1\n" startup_nomount restore_controlfile else info_log "Error: $sharedisk cannot mount or $sharedisk_mount_point busy\n" exit $RETVAL fi else disk=$(df -h | grep $sharedisk_mount_point | awk '{print $1}') if [ $disk == $sharedisk ];then info_log "mount: $sharedisk is already mounted on $sharedisk_mount_point\n" else info_log "Warning: $sharedisk already mounted on $disk\n" fi fi status=$(check_instance_status | grep -Eio -e "\bOPEN\b" -e "\bMOUNTED\b" -e "\bSTARTED\b") if [ "$status" == "OPEN" ];then info_log "a database already open by the instance.\n" elif [ "$status" == "MOUNTED" ];then info_log "re-open database instance\n" open_instance | tee $TMPLOG opened=$(cat $TMPLOG | grep -Eio "\bDatabase altered\b") if [ "$opened" != "Database altered" ];then info_log "Error: database instance open fail!\n" exit 2 fi elif [ "$status" == "STARTED" ];then info_log "alter database to mount\n" mount_instance | tee $TMPLOG mounted=$(cat $TMPLOG | grep -Eio "\bDatabase altered\b") if [ "$mounted" == "Database altered" ];then info_log "alter database to open\n" open_instance | tee $TMPLOG opened=$(cat $TMPLOG | grep -Eio "\bDatabase altered\b") if [ "$opened" == "Database altered" ];then info_log "Database opened.\n" else info_log "Database open failed\n" exit 4 fi else info_log "Database mount failed\n" exit 3 fi else info_log "Startup database and open instance\n" shutdown_instance &>/dev/null startup_instance | tee $TMPLOG started=$(cat $TMPLOG | grep -Eio "\bDatabase opened\b") if [ "$started" != "Database opened" ];then info_log "Database instance open fail.\n" info_log "restore controlfile 2\n" shutdown_instance | tee $TMPLOG startup_nomount | tee $TMPLOG restore_controlfile shutdown_instance | tee $TMPLOG startup_instance | tee $TMPLOG started=$(cat $TMPLOG | grep -Eio "\bDatabase opened\b") if [ "$started" != "Database opened" ];then info_log "Database restore fail!\n" exit 5 else info_log "Database opened.\n" fi else info_log "Database opened.\n" fi fi info_log "Startup listener\n" runuser -l oracle -c "lsnrctl status &>/dev/null" if [ $? -eq 0 ];then info_log "listener already started.\n" else info_log "starting listener...\n" runuser -l oracle -c "lsnrctl start &>/dev/null" if [ $? -eq 0 ];then info_log "The listener startup successfully\n" else info_log "Listener start failure!\n" fi fi echo } backup() { info_log "Database Switchover To BACKUP\n" ismount=$(df -h | grep $sharedisk | grep $sharedisk_mount_point | wc -l) if [ $ismount -ge 1 ];then disk=$(df -h | grep $sharedisk_mount_point | awk '{print $1}') if [ $disk == $sharedisk ];then status=$(check_instance_status | grep -Eio -e "\bOPEN\b" -e "\bMOUNTED\b" -e "\bSTARTED\b") if [ "$status" == "OPEN" -o "$status" == "MOUNTED" ];then info_log "Database instance state is mounted\n" info_log "Backup current controlfile.\n" echo -e "\nSQL> alter database backup controlfile to '$controlfile_back';\n" backup_controlfile info_log "Shutdown database instance, please wait...\n" shutdown_instance | tee $TMPLOG shuted=$(cat $TMPLOG | grep -Eio "\binstance shut down\b") if [ "$shuted" == "instance shut down" ];then info_log "Database instance shutdown successfully.\n" else info_log "Database instance shutdown failed.\n" info_log "shutdown abort.\n" shutdown_abort fi elif [ "$status" == "STARTED" ];then info_log "Database instance state is STARTED\n" info_log "Shutdown database instance, please wait...\n" shutdown_instance | tee $TMPLOG shuted=$(cat $TMPLOG | grep -Eio "\binstance shut down\b") if [ "$shuted" == "instance shut down" ];then info_log "Database instance shutdown successfully.\n" else info_log "Database instance shutdown failed.\n" info_log "shutdown abort.\n" shutdown_abort fi else shutdown_instance | tee $TMPLOG info_log "Database instance not available.\n" fi echo info_log "umount sharedisk\n" echo umount $sharedisk_mount_point && RETVAL=$? if [ $RETVAL -eq 0 ];then info_log "umount $sharedisk_mount_point success.\n" else info_log "umount $sharedisk_mount_point fail!\n" fi else info_log "$sharedisk is not mount on $sharedisk_mount_point or busy.\n" fi else info_log "$sharedisk_mount_point is no mount\n" fi info_log "stopping listener...\n" runuser -l oracle -c "lsnrctl status" &>/dev/null RETVAL=$? if [ $RETVAL -eq 0 ];then runuser -l oracle -c "lsnrctl stop" &>/dev/null RETVAL=$? if [ $RETVAL -eq 0 ];then info_log "The listener stop successfully\n" else info_log "Listener stop failure!\n" fi else info_log "listener is not started.\n" fi echo } notify_master() { echo -e "\n-------------------------------------------------------------------------------" echo "`date '+%b %d %T %a'` $(hostname) [keepalived_notify]: Transition to $1 STATE"; echo "`date '+%b %d %T %a'` $(hostname) [keepalived_notify]: Setup the VIP on $interface $virtual_ipaddress"; } notify_backup() { echo -e "\n-------------------------------------------------------------------------------" echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify]: Transition to $1 STATE"; echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify]: removing the VIP on $interface for $virtual_ipaddress"; } case $1 in master) notify_master MASTER | tee -a $LOGFILE master | tee -a $LOGFILE ;; backup) notify_backup BACKUP | tee -a $LOGFILE backup | tee -a $LOGFILE ;; fault) notify_backup FAULT | tee -a $LOGFILE backup | tee -a $LOGFILE ;; stop) notify_backup STOP | tee -a $LOGFILE /etc/init.d/keepalived start #sleep 6 && backup | tee -a $LOGFILE ;; *) echo "Usage: `basename $0` {master|backup|fault|stop}" RETVAL=1 ;; esac exit $RETVAL
l 將主服務(wù)器切換為BACKUP狀態(tài)
執(zhí)行./keepalived_notify.sh backup
[root@hmdg-db1 scripts]# ./keepalived_notify.sh backup ------------------------------------------------------------------------------- Apr 15 17:23:05 Sun hmdg-db1 [keepalived_notify]: Transition to BACKUP STATE Apr 15 17:23:05 Sun hmdg-db1 [keepalived_notify]: removing the VIP on eth2 for 172.16.10.130,172.16.10.131 Apr 15 17:23:05 Sun hmdg-db1 [keepalived_notify]: Database Switchover To BACKUP Apr 15 17:23:05 Sun hmdg-db1 [keepalived_notify]: /oradata is no mount Apr 15 17:23:05 Sun hmdg-db1 [keepalived_notify]: stopping listener... Apr 15 17:23:05 Sun hmdg-db1 [keepalived_notify]: listener is not started.
觀察執(zhí)行腳本的輸出結(jié)果,這是由于在本機(jī)上并沒有啟動oracle實(shí)例,所以腳本最終提示本機(jī)并沒有啟動oracle數(shù)據(jù)庫實(shí)例
l 將主服務(wù)器切換為MASTER狀態(tài)
執(zhí)行./keepalived_notify.sh master
[root@hmdg-db1 scripts]# ./keepalived_notify.sh master ------------------------------------------------------------------------------- Apr 15 17:23:43 Sun hmdg-db1 [keepalived_notify]: Transition to MASTER STATE Apr 15 17:23:43 Sun hmdg-db1 [keepalived_notify]: Setup the VIP on eth2 172.16.10.130,172.16.10.131 Apr 15 17:23:43 Sun hmdg-db1 [keepalived_notify]: Database Switchover To MASTER Apr 15 17:23:43 Sun hmdg-db1 [keepalived_notify]: Check remote node sharedisk mounted. Apr 15 17:23:43 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 check passed. Apr 15 17:23:43 Sun hmdg-db1 [keepalived_notify]: mount /dev/sdb1 on /oradata Apr 15 17:23:43 Sun hmdg-db1 [keepalived_notify]: restore controlfile 1 ORACLE instance started. Total System Global Area 1603411968 bytes Fixed Size 2213776 bytes Variable Size 1056966768 bytes Database Buffers 536870912 bytes Redo Buffers 7360512 bytes Recovery Manager: Release 11.2.0.1.0 - Production on Sun Apr 15 17:23:44 2018 Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved. connected to target database: HMODB (not mounted) using target database control file instead of recovery catalog RMAN> Starting restore at 15-APR-18 allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=572 device type=DISK channel ORA_DISK_1: copied control file copy output file name=/oradata/HMODB/control01.ctl output file name=/u01/app/oracle/flash_recovery_area/HMODB/control02.ctl output file name=/home/oracle/rman/HMODB/control03.ctl Finished restore at 15-APR-18 RMAN> Recovery Manager complete. Apr 15 17:23:46 Sun hmdg-db1 [keepalived_notify]: alter database to mount Database altered. Apr 15 17:23:50 Sun hmdg-db1 [keepalived_notify]: alter database to open Database altered. Apr 15 17:23:57 Sun hmdg-db1 [keepalived_notify]: Database opened. Apr 15 17:23:57 Sun hmdg-db1 [keepalived_notify]: Startup listener Apr 15 17:23:57 Sun hmdg-db1 [keepalived_notify]: starting listener... Apr 15 17:23:57 Sun hmdg-db1 [keepalived_notify]: The listener startup successfully
觀察執(zhí)行腳本的輸出結(jié)果
1、腳本接收轉(zhuǎn)換到MASTER信息
2、首先提示綁定的VIP
3、然后檢測遠(yuǎn)程備用節(jié)點(diǎn)的磁盤是否掛載,或本機(jī)是否掛載了磁盤,檢測成功則把磁盤掛載
4、啟動數(shù)據(jù)庫到nomount狀態(tài),并恢復(fù)控制文件
5、啟動數(shù)據(jù)庫實(shí)例到open狀態(tài)
6、啟動數(shù)據(jù)庫監(jiān)聽,切換完成
注意:如果使用系統(tǒng)命令手動切換,應(yīng)該要保證正在運(yùn)行oracle服務(wù)的主節(jié)點(diǎn)先正常關(guān)閉,并且卸載共享存儲之后,再啟動備用節(jié)點(diǎn)的oracle服務(wù)(即不能同時(shí)掛載共享存儲)
l 再次將主服務(wù)器切換到BACKUP狀態(tài)
執(zhí)行./keepalived_notify.sh backup
[root@hmdg-db1 scripts]# ./keepalived_notify.sh backup ------------------------------------------------------------------------------- Apr 15 17:34:54 Sun hmdg-db1 [keepalived_notify]: Transition to BACKUP STATE Apr 15 17:34:54 Sun hmdg-db1 [keepalived_notify]: removing the VIP on eth2 for 172.16.10.130,172.16.10.131 Apr 15 17:34:54 Sun hmdg-db1 [keepalived_notify]: Database Switchover To BACKUP Apr 15 17:34:55 Sun hmdg-db1 [keepalived_notify]: Database instance state is mounted Apr 15 17:34:55 Sun hmdg-db1 [keepalived_notify]: Backup current controlfile. SQL> alter database backup controlfile to '/backup/oracle/control/control_20181504173454'; Database altered. Apr 15 17:34:55 Sun hmdg-db1 [keepalived_notify]: Shutdown database instance, please wait... Database closed. Database dismounted. ORACLE instance shut down. Apr 15 17:35:04 Sun hmdg-db1 [keepalived_notify]: Database instance shutdown successfully. Apr 15 17:35:04 Sun hmdg-db1 [keepalived_notify]: umount sharedisk Apr 15 17:35:05 Sun hmdg-db1 [keepalived_notify]: umount /oradata success. Apr 15 17:35:05 Sun hmdg-db1 [keepalived_notify]: stopping listener... Apr 15 17:35:08 Sun hmdg-db1 [keepalived_notify]: The listener stop successfully
觀察執(zhí)行腳本的輸出結(jié)果
1、 腳本接收轉(zhuǎn)換到BACKUP信息
2、 首先移除VIP(由keepalived綁定到主機(jī)上浮動IP地址,同時(shí)也是對外提供服務(wù)的IP地址),實(shí)際上是由keepalived檢測到異?;虮旧淼姆?wù)器不可用時(shí)自動剔除VIP
3、 備份控制文件到/backup/oracle/control/目錄下
4、 正常關(guān)閉數(shù)據(jù)庫實(shí)例
5、 卸載共享存儲
6、 關(guān)閉數(shù)據(jù)庫監(jiān)聽程序
l notify腳本切換演示
如果備機(jī)主動切換到MASTER狀態(tài)時(shí),腳本首先或檢測對方磁盤是否umount,如果沒有則最多等待20秒,提示異常并正常推出切換到MASTER的請求
[root@hmdg-db1 scripts]# ./keepalived_notify.sh master ------------------------------------------------------------------------------- Apr 15 17:40:08 Sun hmdg-db1 [keepalived_notify]: Transition to MASTER STATE Apr 15 17:40:08 Sun hmdg-db1 [keepalived_notify]: Setup the VIP on eth2 172.16.10.130,172.16.10.131 Apr 15 17:40:08 Sun hmdg-db1 [keepalived_notify]: Database Switchover To MASTER Apr 15 17:40:08 Sun hmdg-db1 [keepalived_notify]: Check remote node sharedisk mounted. Apr 15 17:40:08 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [1]... Apr 15 17:40:09 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [2]... Apr 15 17:40:10 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [3]... Apr 15 17:40:12 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [4]... Apr 15 17:40:13 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [5]... Apr 15 17:40:14 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [6]... Apr 15 17:40:15 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [7]... Apr 15 17:40:16 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [8]... Apr 15 17:40:17 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [9]... Apr 15 17:40:18 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [10]... Apr 15 17:40:20 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [11]... Apr 15 17:40:21 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [12]... Apr 15 17:40:22 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [13]... Apr 15 17:40:23 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [14]... Apr 15 17:40:24 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [15]... Apr 15 17:40:25 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [16]... Apr 15 17:40:26 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [17]... Apr 15 17:40:27 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [18]... Apr 15 17:40:29 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [19]... Apr 15 17:40:30 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [20]... Apr 15 17:40:30 Sun hmdg-db1 [keepalived_notify]: Disk status abnormal.
這時(shí)如果需要手動切換,則需要先將遠(yuǎn)程備用節(jié)點(diǎn)的服務(wù)關(guān)閉并umount存儲之后,才能正常切換
[root@hmdg-db1 scripts]# ./keepalived_notify.sh master ------------------------------------------------------------------------------- Apr 15 17:45:29 Sun hmdg-db1 [keepalived_notify]: Transition to MASTER STATE Apr 15 17:45:29 Sun hmdg-db1 [keepalived_notify]: Setup the VIP on eth2 172.16.10.130,172.16.10.131 Apr 15 17:45:29 Sun hmdg-db1 [keepalived_notify]: Database Switchover To MASTER Apr 15 17:45:29 Sun hmdg-db1 [keepalived_notify]: Check remote node sharedisk mounted. Apr 15 17:45:29 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [1]... Apr 15 17:45:30 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [2]... Apr 15 17:45:31 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [3]... Apr 15 17:45:32 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [4]... Apr 15 17:45:33 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [5]... Apr 15 17:45:35 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [6]... Apr 15 17:45:36 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [7]... Apr 15 17:45:37 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [8]... Apr 15 17:45:38 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [9]... Apr 15 17:45:39 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [10]... Apr 15 17:45:40 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [11]... Apr 15 17:45:41 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [12]... Apr 15 17:45:42 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 is already mounted on remote node or busy. checking [13]... Apr 15 17:45:44 Sun hmdg-db1 [keepalived_notify]: /dev/sdb1 check passed. Apr 15 17:45:44 Sun hmdg-db1 [keepalived_notify]: mount /dev/sdb1 on /oradata Apr 15 17:45:44 Sun hmdg-
網(wǎng)站欄目:OracleHA雙機(jī)主備基于共享存儲模式并利用keepalived管理實(shí)現(xiàn)高可用
鏈接分享:http://weahome.cn/article/jegsse.html