首页 > 数据库 >shell脚本:dm8数据库安装

shell脚本:dm8数据库安装

时间:2023-07-03 19:13:07浏览次数:33  
标签:shell grep NAME 数据库 echo INSTANCE USER dm8 HOME

程序作用:
1.初始化操作系统
2.安装dm数据库软件
3.初始化实例
4.配置参数
5.删除实例
6.删除数据库软件

整个安装程序的目录结构如下:
image

说明:

点击查看代码
run.sh说明:
	作用:统一执行脚本,调用其他脚本进行操作系统参数配置、数据库软件安装、服务启动等
	用法:
		前提:
			1.把所有的脚本、sql文件、安装包上传服务器
			2.服务器安装expect软件
			3.配置dm8_common_var.sh文件参数,设置数据库相关参数
		操作方法:
			1.操作系统参数配置 sh run.sh preinstall
			2.安装数据库软件和实例 sh run.sh install
			3.删除实例和卸载数据库软件 sh run.sh uninstall
			4.操作系统参数配置&&数据库软件安装和实例初始化 sh run.sh all
			
	报错处理:
		1.在数据库安装时出错,先卸载数据库,解决报错,重新安装
		2.在数据库卸载时报错,重新执行卸载即可
		3.多次安装卸载过程中报错时留下的文件,可能会在安装或者卸载过程中,额外提示,只需要在expect过程加上该段即可
			
dm8_common_var.sh说明:数据库配置项脚本,在执行脚本时自动加载
dm8_preInstall.sh说明:配置操作系统相关参数
AutoParaAdj2.6.sql说明:数据库软件相关配置项
dm8_systemd.sh说明:数据库服务管理脚本

dm8_install.sh说明:
	作用:
		1.用于安装dm8数据库和初始化实例
		2.卸载数据库实例和数据库软件	

因为两个sql文件不是自己写的,所以这里不做内容展示:

run.sh文件内容如下:

点击查看代码
#!/bin/sh
###################################################################
# 在root用户下执行该操作脚本,用于初始化操作系统参数
# 填写配置文件dm8_common_var.sh脚本,然后使用root用户执行脚本安装dm8
###################################################################

# 当前脚本目录
SCRIPT_HOME=$(cd $(dirname $0); pwd)
PREINSTALL_SCRIPT=$SCRIPT_HOME/dm8_preInstall.sh
COMMON_SCRIPT=$SCRIPT_HOME/dm8_common_var.sh
INSTALL_SCRIPT=$SCRIPT_HOME/dm8_install.sh
SYSTEMD_SCRIPT=$SCRIPT_HOME/dm8_systemd.sh
cd $SCRIPT_HOME || exit 111


declare() {
    echo "==please input $1 [options]
  options:
      all             初始化操作系统参数&&安装数据库软件和实例
      preinstall      初始化操作系统参数
      install         安装数据库软件和实例
      uninstall       卸载数据库实例和软件

"
}


config_modify() {
    # 只允许当前格式配置的修改
    # $1 为配置文件
    # $2 为修改的配置项
    # $3 为修改值
    echo "  sed -i 's#^'"$2"'=.*#'"$2"'='"$3"'#g' $1"
    sed -i 's#^'"$2"'=.*#'"$2"'='"$3"'#g' $1
}


confirm_script() {
    # 判断脚本是否在当前目录
    # $1 为输入脚本名
    if [ -f $1 ]; then
        # 检查脚本的权限,需要有执行权限
        # chmod a+x $1   # 不单独做配置 
        DB_GROUP=$(id $USER|awk '{print $2}'|awk -F'(' '{print $2}'|sed 's#)##g') 
        echo "==对$SCRIPT_HOME目录和目录下脚本授权"
        chown -R $USER:$DB_GROUP $dbInstallBaseDir 
        chown $USER:$DB_GROUP  $SCRIPT_HOME/DMInstall.bin
        chmod a+x $SCRIPT_HOME/DMInstall.bin 
        chown $USER:$DB_GROUP  $SCRIPT_HOME/*.sh
        chmod a+x $SCRIPT_HOME/*.sh
        chown -R $USER:$DB_GROUP $SCRIPT_HOME
    fi
}


common_var() {
    echo "==声明变量"
    source $SCRIPT_HOME/dm8_common_var.sh
    
    # 根据输入的配置文件进行配置修改
    # $1为输入文件名
    if [ $(basename $1) == $(basename $INSTALL_SCRIPT) ]; then
        echo "==修改$1文件配置"
        config_modify $INSTALL_SCRIPT USER $USER
        config_modify $INSTALL_SCRIPT SOFT_HOME $dbInstallBaseDir
        config_modify $INSTALL_SCRIPT INSTANCE_NAME $INSTANCE_NAME
        config_modify $INSTALL_SCRIPT INSTANCE_PORT $INSTANCE_PORT 
        config_modify $INSTALL_SCRIPT DB_USER $DB_USER
        config_modify $INSTALL_SCRIPT DB_PASSWD $DB_PASSWD
        config_modify $INSTALL_SCRIPT INSTANCE_PATH $dbInstallBaseDir
    elif [ $(basename $1) == $(basename $PREINSTALL_SCRIPT) ]; then
        echo "==预留修改配置段落"

    else
        echo "==预留段落"

    fi 
}


opertor_install() {
    # 安装过程单独包装,方便调用
    common_var $INSTALL_SCRIPT
    confirm_script $INSTALL_SCRIPT

    [ -f "/etc/dm_svc.conf" ]&&rm -f /etc/dm_svc.conf
    echo "==安装数据库软件"
    su - $USER -s $INSTALL_SCRIPT $1 install
    sh $SYSTEMD_SCRIPT create dmap

    echo "==初始化数据库实例"       
    su - $USER -s $INSTALL_SCRIPT $1 initial
    sh $SYSTEMD_SCRIPT create dmserver

    echo "==初始化配置项"
    # 检查服务是否在线,不在线则提示退出
    if [ $(ps -elf|grep dmserver|grep -v grep | grep -i $INSTANCE_NAME|grep dm.ini|wc -l) -eq 0 ]; then
        echo "==检测服务不在线,启动DmService$INSTANCE_NAME服务"
        systemctl start DmService$INSTANCE_NAME || exit 111
    fi

    su - $USER -s $INSTALL_SCRIPT $1 configure
    echo "==初始化参数后重启实例"
    systemctl restart DmService$INSTANCE_NAME
}

if [ $# != 1 ]; then
    declare $0    
    exit 111
fi


case $1 in
    ALL|all|All)
        echo "==操作系统参数初始化"
        sh $SCRIPT_HOME/dm8_preInstall.sh

        # 安装数据库
        opertor_install install
        ;;

    preinstall)
        echo "==操作系统参数初始化"
        sh $SCRIPT_HOME/dm8_preInstall.sh;;

    install)
        opertor_install $1
        ;;

    uninstall)
        common_var $INSTALL_SCRIPT
        confirm_script $INSTALL_SCRIPT

        echo "==删除数据库实例"
        su - $USER -s $INSTALL_SCRIPT $1 removedb
        sh $SYSTEMD_SCRIPT remove dmserver
        
        echo "==卸载数据库软件"
        su - $USER -s $INSTALL_SCRIPT $1 removesoft
        sh $SYSTEMD_SCRIPT remove dmap
        ;;

    *)
        declare $0    
        exit 111;; 
esac

声明环境变量的脚本内容dm8_common_var.sh如下:(另一人编写提供)

点击查看代码
#数据库用户
USER=dmdba
#数据库用户所在用户组
GROUP_NAME=dinstall
#达梦数据库安装及实例所在目录
dbInstallBaseDir="/u01/dmdb"
#建议逻辑CPU个数
adviceCpu=4
#建议内存(M)
adviceMemory=15000
#建议剩余硬盘空间(M)
adviceDiskFree=200000

#数据库实例名
INSTANCE_NAME=TONGXIN
#数据库实例端口
INSTANCE_PORT=15333
#数据库DBA用户名
DB_USER=SYSDBA
#数据库DBA密码
DB_PASSWD=SYSDBA
#达梦软件(含key)

操作系统优化脚本d8_preinstall.sh内容如下:(另一人编写提供)

点击查看代码
#!/bin/bash
source ~/.bash_profile

#数据库软件安装基准目录
dbInstallBaseDir="/u01/dmdb"
#挂载点初始判断目录
mountPointInitDir=${dbInstallBaseDir}
#建议CPU个数
adviceCpu=4
#建议内存(M)
adviceMemory=15000
#建议剩余硬盘空间(M)
adviceDiskFree=200000
#检查的数据库用户名
USER_NAME=dmdba
GROUP_NAME=dinstall

#以下部分不可修改
#检查警告信息:硬件等不可改变信息提示是否继续
warningInfo=""
#是否需要重启服务器(调整grub后需要重启才会生效)
isNeedReboot=N

#依赖安装
dependcyInstall=Y

recordFile=dm_checklog_$(date +%Y%m%d-%H%M).txt

#提示着色
function set_prompt_color(){
    echo=echo
    for cmd in echo /bin/echo; do
        $cmd >/dev/null 2>&1 || continue
        if ! $cmd -e "" | grep -qE '^-e'; then
            echo=$cmd
            break
        fi
    done
    csi=$($echo -e "\033[")
    #####color
    cend="${csi}0m"
    cdgreen="${csi}32m"
    cred="${csi}1;31m"
    cgreen="${csi}1;32m"
    cyellow="${csi}1;33m"
    cblue="${csi}1;34m"
    cmagenta="${csi}1;35m"
    ccyan="${csi}1;36m"
    ####prompt
    csuccess="$cdgreen"
    cfailure="$cred"
    cquestion="$cmagenta"
    cwarning="$cyellow"
    cmsg="$ccyan"
}

#用户检查
checkUser(){
    echo "${cmsg}检查执行脚本用户环境${cend}"
    #检查是否root用户
    [ $EUID -ne 0 ] && (echo "${cfailure}当前账户为$(whoami),请使用root用户!${cend}"; exit 101)
}

#硬件资源检测(CPU,内存,磁盘空间)
checkHardware(){
    echo "${cmsg}检查硬件信息CPU,内存和挂载点剩余空间${cend}"
    #检查CPU个数
    cpuNum=$(lscpu |grep "^CPU(s):" | awk '{print $2}')
    memNum=$(free -m |grep -i "Mem" | awk '{print $2}')
    [[ $cpuNum -lt  ${adviceCpu} ]] && warningInfo="${warningInfo}逻辑CPU为${cpuNum},小于建议值${adviceCpu};\n"
    [[ $memNum -lt  ${adviceMemory} ]] && warningInfo="${warningInfo}物理内存为${memNum}M,小于建议值${adviceMemory}M;\n"
    
    #如果mountPointInitDir以/结尾,去掉结尾的/
    mountPointInitDir=$(echo $mountPointInitDir | sed -r -e 's#(.*)/$#\1#')
    #从指定的根目录找到挂载点
    while true;do
        #找到挂载点,推出循环
        if [ $(df -h |grep -E "^.*$mountPointInitDir$|^.*$mountPointInitDir/$" | wc -l) -gt 0 ];then
            #echo $(df -h |grep -E "^.*$mountPointInitDir$|^.*$mountPointInitDir/$")
            freeDisk=$(df -m |grep -E "^.*$mountPointInitDir$|^.*$mountPointInitDir/$" | awk '{print $4}')
            [[ $freeDisk -lt  ${adviceDiskFree} ]] && warningInfo="${warningInfo}数据库安装目录挂载点剩余磁盘空间为${freeDisk}M,小于建议值${adviceDiskFree}M;\n"
            break    
        fi
        #取上级目录
        mountPointInitDir=$(dirname $mountPointInitDir)
    done
    
    #输出警告信息
    echo -e "${cfailure}${warningInfo}${cend}"

}

#数据库用户检查及安装目录创建,授权
userExistCheck(){
    echo "${cmsg}检查是否存在数据库安装用户及用户组${cend}"
    #创建达梦安装根目录
    [[ -d ${dbInstallBaseDir} ]] || mkdir -p ${dbInstallBaseDir}
    
    #检查用户组是否存在
    grep -E  "^${GROUP_NAME}" /etc/group >& /dev/null
    [ $? -eq 0 ] || {
        echo "${cmsg}创建组${GROUP_NAME}${cend}"
        groupadd ${GROUP_NAME} 
    }
    
    id ${USER_NAME} >/dev/null 2>&1
    
    #用户存在,则修改用户所属组。不存在则新建用户
    if [ $? -eq 0 ];then
        usermod -g ${GROUP_NAME} ${USER_NAME}  >& /dev/null
    else 
        echo "${cmsg}${USER_NAME}不存在,创建用户${USER_NAME}${cend}"  
        #新建用户
        useradd -g ${GROUP_NAME} ${USER_NAME}
    fi
    
    #修改根目录所属用户组
    chown -R ${USER_NAME}:${GROUP_NAME} ${dbInstallBaseDir}
}


#glibc-2.28-36.1.ky10.aarch64
#glibc-common-2.28-36.1.ky10.aarch64
#glibc-devel-2.28-36.1.ky10.aarch64
#gcc-7.3.0-20190804.h30.ky10.aarch64
#libgcc-7.3.0-20190804.h30.ky10.aarch64

#检查包
checkPackage(){
    echo "${cmsg}检查依赖包$1${cend}"
    rpm -q $1 >/dev/null 2>&1
    
    #依赖包不存在
    if [ $? -ne 0 ];then
        echo "${cfailure}未安装依赖包:$1${cend}"
        dependcyInstall=N
    fi
}

#依赖检查
depencyCheck(){
    checkPackage glibc
    checkPackage glibc-common
    #checkPackage glibc-devel
    #checkPackage gcc
    checkPackage libgcc
    
    [ ${dependcyInstall} == "N" ] && {
        echo "${cfailure}含有未安装的依赖,退出${cend}"
        exit 101
    }
}

#记录修改日志
recordLog(){
    echo "$2:$1" >>${recordFile}
}

#关闭numa
offnuma(){
    echo "${cmsg}检查是否关闭numa,没关闭将关闭numa${cend}"
    if [ $(more /etc/default/grub | grep -E "GRUB_CMDLINE_LINUX\s*=.*numa=" | wc -l) -eq 0 ];then
        sed -i 's#GRUB_CMDLINE_LINUX\s*=\s*\"#GRUB_CMDLINE_LINUX=\"numa=off #' /etc/default/grub
        grub2-mkconfig -o /boot/grub2/grub.cfg
        #修改numa参数需要重启,设置需要重启参数
        isNeedReboot=Y
        #记录修改日志
        recordLog "关闭numa配置,GRUB_CMDLINE_LINUX中新增numa=off" "/etc/default/grub"
    fi
}

#关闭防火墙
closeFirewall(){
    echo "${cmsg}关闭防火墙${cend}"
    systemctl stop firewalld >/dev/null 2>&1
    systemctl disable firewalld >/dev/null 2>&1
}

#关闭seliunx
closeSeliunx(){
    echo "${cmsg}检查seliunx是否禁用,没禁用将禁用seliunx${cend}"
    setenforce 0 >/dev/null 2>&1
    #存在配置项,则删除原配置文件值
    if [ $(more /etc/selinux/config |grep -E "^\s*SELINUX\s*=enf|^\s*SELINUX\s*=per" | wc -l) -ge 1 ];then
        sed -i '/^[^#].*ELINUX\s*=.*/d' config
cat >> /etc/selinux/config << EOF
SELINUX=disabled
EOF
        recordLog "修改SELINUX为SELINUX=disabled" "/etc/selinux/config"
    elif [ $(more /etc/selinux/config |grep -E "^\s*SELINUX\s*=disabled" | wc -l) -eq 0 ];then
cat >> /etc/selinux/config << EOF
SELINUX=disabled
EOF
        recordLog "修改SELINUX为SELINUX=disabled" "/etc/selinux/config"    
    fi
}

#禁用大页
disableHugePages(){
    echo "${cmsg}检查是否禁用透明大页,没禁用将禁用透明大页${cend}"
#    if [ $(more /sys/kernel/mm/transparent_hugepage/enabled |grep "\[never\]" | wc -l) -eq 0 ];then
#      echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
    #如果启用了大页进行禁用相关操作
    if [ $(grep Huge /proc/meminfo |grep "AnonHugePages:" | awk '{print $2}') != "0" ];then
        if [ $(more /etc/default/grub | grep -E "GRUB_CMDLINE_LINUX\s*=.*transparent_hugepage=" | wc -l) -eq 0 ];then
            #临时生效
            echo never > /sys/kernel/mm/transparent_hugepage/enabled
            #永久生效
            sed -i 's#GRUB_CMDLINE_LINUX\s*=\s*\"#GRUB_CMDLINE_LINUX=\"transparent_hugepage=never #' /etc/default/grub
            grub2-mkconfig -o /boot/grub2/grub.cfg
            #修改numa参数需要重启,设置需要重启参数
            isNeedReboot=Y
            #记录修改日志
            recordLog "禁用大页配置,GRUB_CMDLINE_LINUX中新增transparent_hugepage=never" "/etc/default/grub"
        fi
    fi
}

#调整时区及系统日志时间相关
adjTimeZone(){
    echo "${cmsg}调整时区为上海${cend}"
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime >/dev/null 2>&1 || true
    hwclock -w || true
    timedatectl set-timezone "Asia/Shanghai" || true
    systemctl restart rsyslog >/dev/null 2>&1 || true
}

#修改配置,key=value
checkAndAdjustConfig(){
    #3个参数时才进行相关操作,
    #第一个参数是key,第二个参数是value,第三个参数是文件,第四个文件是设置的值和前面的分割符号比如=或空格
    if [ $# -eq 4 ];then
        configKey=$1
        configValue=$2
        configFile=$3
        operationFalg=$4
        
        if [ $(more $configFile |grep -E "^${configKey} ${operationFalg} ${configValue}$" | wc -l) -eq 1 ];then
            return 0
        fi
   
        #判断配置项是否存在,存在删掉(可能有多个同名key)
        if [ $(more $configFile |grep -E "^${configKey}\s*${operationFalg}" | wc -l) -ge 1 ];then
            #echo $(more $configFile |grep -E "^${configKey}\s*=")
            sed -i "/^${configKey}\s*${operationFalg}.*/d" $configFile            
        fi
#配置加
cat >> ${configFile} << EOF
${configKey} ${operationFalg} ${configValue}
EOF
    fi
    
    recordLog "${configFile}新增/修改配置项${configKey} ${operationFalg} ${configValue}" ${configFile}
}


#内核参数调整
adjSysctl(){
    echo "${cmsg}检查并调整/etc/sysctl.conf${cend}"
    checkAndAdjustConfig "net.ipv6.conf.all.disable_ipv6" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv6.conf.default.disable_ipv6" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.neigh.default.gc_stale_time" 120 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.default.arp_announce" 2 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.all.arp_announce" 2 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.lo.arp_announce" 2 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.icmp_echo_ignore_broadcasts" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.icmp_ignore_bogus_error_responses" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.ip_forward" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.all.send_redirects" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.default.send_redirects" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.all.rp_filter" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.default.rp_filter" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.default.accept_source_route" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "kernel.sysrq" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "kernel.core_uses_pid" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_syncookies" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_max_tw_buckets" 6000 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_sack" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_window_scaling" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_max_orphans" 3276800 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_max_syn_backlog" 262144 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_timestamps" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_synack_retries" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_syn_retries" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_tw_recycle" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_tw_reuse" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_mem" "94500000 915000000 927000000" /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_fin_timeout" 1 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_keepalive_time" 1800 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_keepalive_probes" 3 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.tcp_keepalive_intvl" 15 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.ip_local_port_range" "1024    65000" /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.default.accept_redirects" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.all.secure_redirects" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.default.secure_redirects" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.all.accept_redirects" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "net.ipv4.conf.all.accept_source_route" 0 /etc/sysctl.conf =
    checkAndAdjustConfig "vm.overcommit_memory" 0 /etc/sysctl.conf =
    sysctl -p >/dev/null 2>&1
}

#资源限制
adjLimit(){
    echo "${cmsg}检查并调整/etc/security/limits.conf${cend}"
    checkAndAdjustConfig "${USER_NAME} soft core" "unlimited" /etc/security/limits.conf " "
    checkAndAdjustConfig "${USER_NAME} hard core" "unlimited" /etc/security/limits.conf " "
    checkAndAdjustConfig "${USER_NAME} soft nofile" "65536" /etc/security/limits.conf " "
    checkAndAdjustConfig "${USER_NAME} hard nofile" "65536" /etc/security/limits.conf " "
    checkAndAdjustConfig "${USER_NAME} soft nproc" "65536" /etc/security/limits.conf " "
    checkAndAdjustConfig "${USER_NAME} hard nproc" "65536" /etc/security/limits.conf " "
    checkAndAdjustConfig "${USER_NAME} soft stack" "65536" /etc/security/limits.conf " "
    checkAndAdjustConfig "${USER_NAME} hard stack" "65536" /etc/security/limits.conf " "
}
#主机名修改


#检查、修改系统参数
checkAndModoperationParam(){
    #关闭selliunx相关操作
    closeSeliunx
    #关闭numa相关操作
    offnuma
    #禁用大页操作
    disableHugePages
    #内核参数调整
    adjSysctl
    #资源限制修改
    adjLimit
}

#主函数
main(){
    echo "调整配置信息:"> ${recordFile}
    #引入提示颜色
    set_prompt_color
    #用户检查
    checkUser
    #硬件配置检测
    checkHardware
      
    #如果硬件含有检查不及预期信息,进行提醒
    [ ! -z ${warningInfo} ] && {
        read -t 60 -p "资源检查含有警告信息,继续请键入y或Y,否则退出:" contFlag
        contFlag=${contFlag:-"N"}
    }
    
    if [ ${contFlag} == "Y" ] || [ ${contFlag} == "y" ];then
        #检查用户是否存在,不存在则创建用户
        userExistCheck
        #依赖检查
        depencyCheck
        #关闭防火墙
        closeFirewall
        #时区等调整
        adjTimeZone
        #检查,调整系统参数(有的参数调整需要重启服务器)
        checkAndModoperationParam
        
		echo "${cquestion}达梦数据库安装检查配置调整结束。检查及调整配置信息见${recordFile}${cend}"
        #需要重启,提示是否重启
        [[ $isNeedReboot == "Y" ]] && {
            read -t 60 -p "有禁用透明大页或关闭numa的操作,需要重启操作系统才会永久生效,现在重启系统请键入y或Y,否则继续:" contFlag
            contFlag=${contFlag:-"N"}
			
            if [ ${contFlag} == "Y" ] || [ ${contFlag} == "y" ];then
                shutdown -r now
            fi
        }
    else
        echo "${cfailure}退出达梦数据库安装检查。。。${cend}"    
    fi 
}

main

安装数据库和初始化实例dm8_install.sh脚本内容如下:

点击查看代码
#!/bin/bash

#########################################
# 自动交互安装dm8数据库
# os: linux 7
# arch: x86
#########################################

# 安装用户
USER=dmdba
# 脚本目录
HOME=$(cd $(dirname $0); pwd)  # 脚本目录
# # 软件
DM_SOFT=$HOME/DMInstall.bin  # 软件
# DM软件安装目录
SOFT_HOME=/u01/dmdb
# 实例名
INSTANCE_NAME=TONGXIN
# 最终创建的实例会在该目录下额外创建一个同实例名的目录
INSTANCE_PATH=/u01/dmdb
INSTANCE_PORT=15333
LINCESE_FILE=$HOME/dm.key
PARAMETER_FILE=$HOME/AutoParaAdj2.6.sql
# 页大小、编码 utf-8、空格填充、日志文件2G、簇大小32、大小写敏感、VARCHAR类型长度
INSTANCE_OPS="page_size=32 CHARSET=1 BLANK_PAD_MODE=0 log_size=2048 EXTENT_SIZE=16 CASE_SENSITIVE=1  LENGTH_IN_CHAR=0"
DB_USER=SYSDBA
DB_PASSWD=SYSDBA
IP=$(/usr/sbin/ifconfig|grep inet|grep '255.255.255.0'|awk '{print $2}')
PARA_NUM=$#
OPERATER=$1
OPTION=$2

# 辅助输出
ECHO_LINE() {
  #echo -e "\n"
  echo "-----------------------------$1---------------------------------------"
}


declare() {
  echo "==please input $0 [command] [options]
  command:
      install           install dm8 software and isntance
      uninstall         uninstall dm8 software and remove instance
  options:
      install           install dm8 software when command eq install
      initial           init instance  when command eq install
      configure         init configure when command eq install
      removedb          remove instance dir when command eq uninstall
      removesoft        remove software when command eq uninstall" 
      exit 111
}

# 初始化检查文件、目录及其他参数
INIT() {
  ECHO_LINE "user check"
  # check the user
  [ $(whoami) == root ] && (echo "the current user is root, please change a normal user"; exit 111)
  [ $(whoami) == $USER ] || (echo "the current user is $(whoami), please check"; exit 111)
  # 确认软件权限,属主可执行

  # 这里先简单检查下目录,不检查属主这些信息
#  if [ ! -d $SOFT_HOME ]; then
#    检查/u01属主
#  else
#    检查dmdbs属主
#  fi
  [ -d $SOFT_HOME ] || mkdir -p $SOFT_HOME

  # 检查IP地址是不是一个,两个的时候提示报错
  # 待添加
  echo "it's ok for user and $SOFT_HOME check"
  echo ""

}


# 安装数据库软件
INSTALL() {
  ECHO_LINE "software install start"

  [ -f $DM_SOFT ] || (echo "$DM_SOFT is not exists, please upload file"; exit 111)
  [ -f $PARAMETER_FILE ] || (echo "$PARAMETER_FILE is not exists, please upload file"; exit 111)
  export LANG=gbk  # 显示英文
 
  # 安装目录需要为空
  echo "rm -rf $SOFT_HOME/*"
  rm -rf $SOFT_HOME/*  

  # 自动交互处理安装过程
  /usr/bin/expect << EOF
set timeout -1
spawn $DM_SOFT -i
expect "*installer's language*"
send "E\r"
expect "*input the path of Key File*"
send  "N\r"
expect "*Set The TimeZone*"
send "N\r"
expect "*Input the number of the Installation Type*"
send "1\r"
expect "*Input the install path*"
send "$SOFT_HOME\r"
expect "*Confirm the install path*"
send "Y\r"
expect "*Confirm to Install*"
send "Y\r"
expect eof
EOF
  # Please select the installer's language (E/e:English C/c:Chinese) [E/e]:
  # Whether to input the path of Key File? (Y/y:Yes N/n:No) [Y/y]:
  # Whether to Set The TimeZone? (Y/y:Yes N/n:No) [Y/y]:
  # Please Input the number of the Installation Type [1 Typical]:
  # Please Input the install path [/home/dmdba/dmdbms]:
  # Please Confirm the install path(/u01/dmdba)? (Y/y:Yes N/n:No) [Y/y]:
  # Confirm to Install? (Y/y:Yes N/n:No):

  [ $? == 0 ] || (echo "dm8 software install error"; exit 111)

  # ENV modify
  echo "export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:$SOFT_HOME/bin" >> /home/$USER/.bash_profile
  echo "export DM_HOME=$SOFT_HOME" >> /home/$USER/.bash_profile
  echo "export PATH=\$PATH:\$LD_LIBRARY_PATH:\$DM_HOME/bin" >> /home/$USER/.bash_profile
  source /home/$USER/.bash_profile
  # check the ENV about avaiable SOFT_HOME
  [ -z "$(env|grep -i DM_HOME)" ] && (echo "please check the /home/$USER/.bash_profile about DM_HOME"; exit 111)

  # 安装完成后需要在root用户下执行$SOFT_HOME/script/root/root_installer.sh,动作如下:
  echo "chmod a+x $SOFT_HOME/bin/*"
  # 出现过一次安装后没有权限的情况
  chmod a+x $SOFT_HOME/bin/*  
}



# 启动数据库实例监听 
STARTUP_LISTENER() {
  echo "startup progress about $INSTANCE_NAME"
  $SOFT_HOME/bin/dmserver path=$INSTANCE_PATH/$INSTANCE_NAME/dm.ini  & 
  sleep 180 && echo -e "\n"
}


# 停止数据库监听
SHUTDOWN_LISTENER() {
  echo "stop progress about $INSTANCE_NAME"
  ps -elf|grep dmserver|grep -v grep | grep -i $INSTANCE_NAME | grep -i dm.ini | awk '{print $4}' |xargs kill -9 || exit 111
  sleep 4  
}


# 初始化实例
INITIAL() {
  ECHO_LINE "init instance start"
  # dminit
  source /home/$USER/.bash_profile
  $SOFT_HOME/bin/dminit path=$INSTANCE_PATH PORT_NUM=$INSTANCE_PORT DB_NAME=$INSTANCE_NAME $INSTANCE_OPS
  [ $? == 0 ] || (echo "init instance error"; exit 111)

  # startup
  #STARTUP_LISTENER
  
}



# 参数配置
CONFIGURE() {
  ECHO_LINE "configure parameter"
  source /home/$USER/.bash_profile
  # 在root下检查数据库是否启动,未启动则执行启动

  # 执行SQL文件初始化
  service_num=$(ps -elf|grep dmserver | grep $INSTANCE_NAME|grep -i ini|grep -v grep |wc -l)
  [ $service_num == 0 ] && (echo "please startup the $INSTANCE_NAME"; exit 111)
  cpu_num=$(cat /proc/cpuinfo |grep -i process|wc -l)
  memory_size=$(free -m |grep -i Mem|awk '{print $2}')
  sed -i 's#v_mem_mb int:= ;#v_mem_mb int:= '$memory_size';#g' $PARAMETER_FILE
  sed -i 's#v_cpus int:= .*;#v_cpus int:= '$cpu_num';#g' $PARAMETER_FILE
  #DB_USER=SYSDBA
  #DB_PASSWD=SYSDBA
  $SOFT_HOME/bin/disql $DB_USER/${DB_PASSWD}@${IP}:${INSTANCE_PORT} \`${PARAMETER_FILE} || exit 111
  
  sleep 3
  echo "" && echo "==modify $INSTANCE_PATH/$INSTANCE_NAME/dm.ini"
  # modify dm.ini
  sed -i '/Server compatible mode, 0:none, 1:SQL92, 2:Oracle, 3:MS SQL Server, 4:MySQL, 5:DM6, 6:Teradata/s/OMPATIBLE_MODE                 = 0/OMPATIBLE_MODE                 = 2/g' $INSTANCE_PATH/$INSTANCE_NAME/dm.ini
  sed -i '/Whether try to resolve PL object name by synonym/s/ENABLE_PL_SYNONYM               = 0/ENABLE_PL_SYNONYM               = 1/g' $INSTANCE_PATH/$INSTANCE_NAME/dm.ini
  sed -i '/Optimize hierarchical query/s/CNNTB_OPT_FLAG                  = 0/CNNTB_OPT_FLAG                  = 1/g' $INSTANCE_PATH/$INSTANCE_NAME/dm.ini
  sed -i '/Whether try to resolve PL object name by synonym/s/ENABLE_PL_SYNONYM               = 0/ENABLE_PL_SYNONYM               = 1/g' $INSTANCE_PATH/$INSTANCE_NAME/dm.ini

  # 如下参数在默认配置文件中没有
  distinct_num=$( grep 'ENABLE_ADJUST_DIST_COST         =' $INSTANCE_PATH/$INSTANCE_NAME/dm.ini | wc -l)
  if [ $distinct_num == 0 ]; then
    echo "		ENABLE_ADJUST_DIST_COST         = 3" >> $INSTANCE_PATH/$INSTANCE_NAME/dm.ini
  else
    sed -i '/s/ENABLE_ADJUST_DIST_COST         = 0/ENABLE_ADJUST_DIST_COST         = 3/g' $INSTANCE_PATH/$INSTANCE_NAME/dm.ini
  fi

  # 重启数据库
  sleep 2 
  #SHUTDOWN_LISTENER
  #STARTUP_LISTENER
}


# 卸载数据库
REMOVEDB() {
  ECHO_LINE "uninstall instance"
  if [ -d $INSTANCE_PATH/$INSTANCE_NAME ]; then
    PID_INI=$(ps -elf|grep -v grep|grep -i dmserver|grep -i dm.ini|grep -i $INSTANCE_NAME|awk '{print $4}')
    if [ ! -z $PID_INI ]; then
      PARA_USER=$(ps -elf|grep -v grep|grep -i dmserver|grep -i dm.ini|grep -i $INSTANCE_NAME|awk '{print $3}')
      # kill不掉也无关删除操作
      [ $PARA_USER == $USER ] && (echo "stop dmserver progress about $INSTANCE_NAME"; kill -9 $PID_INI)
    fi

    # 开始删除数据库目录就行
    echo "rm -rf $INSTANCE_PATH/$INSTANCE_NAME"
    rm -rf $INSTANCE_PATH/$INSTANCE_NAME || exit 111

  else
    echo "$INSTANCE_PATH/$INSTANCE_NAME is not exists"
#    echo "$INSTANCE_PATH/$INSTANCE_NAME is not exists, please check the DB is exists"
#    exit 111
  fi
  
  echo  ""
}


# 卸载数据库软件
REMOVESOFT() {
  ECHO_LINE "uninstall software"
  # 管理软件进程
  PID_DMAP=$(ps -elf|grep -v grep|grep dmap|awk '{print $4}')
  if [ ! -z $PID_DMAP ]; then
    DMAP_USER=$(ps -elf|grep -v grep|grep dmap|awk '{print $3}')
    [ $DMAP_USER == $USER ] && (echo "stop dmap progress"; kill -9 $PID_DMAP)
  fi

  # 卸载
  /usr/bin/expect << EOF
set timeout -1
spawn $SOFT_HOME/uninstall.sh -i
expect "*uninstall DM database*"
send "Y\r"
expect "*deletet the dm_svc.conf*"
send "Y\r"
expect eof
EOF

  echo ""
#  echo  "Execute the command by \"root\":"
#  echo "$SOFT_HOME/root_uninstaller.sh"
}

# 按照关键字操作
main() {
  # 检查是否安装expect
  [ $(rpm -qa |grep expect|wc -l) == 0 ] && (echo "please install expect"; exit 111)  

  case $OPERATER in
    install)
      if [ $OPTION == install ]; then
        # 安装数据库软件
        INIT
        INSTALL
      elif [ $OPTION == initial ]; then
        # 初始化实例
        INITIAL
      elif [ $OPTION == configure ]; then
        # 初始化配置项
        CONFIGURE
      else
        declare
        exit 111 
      fi;;

    uninstall)
      if [ $OPTION == removedb ]; then
        # 删除实例
        REMOVEDB
      elif [ $OPTION == removesoft ]; then
        # 删除数据库软件
        REMOVESOFT
      else
        declare
        exit 111
      fi;;

    *)
      declare
      exit 111;;
   esac
}


main

服务管理设置dm8_systemd.sh脚本内容如下:

点击查看代码
#!/bin/sh
################################################
# 将dm8安装的数据库软件dmap服务使用systemd管理
# 将dm8安装的实例软件dmserver服务使用systemd管理
################################################


# 脚本目录
SCRIPT_HOME=$(cd $(dirname $0); pwd)
# DM软件安装目录
SOFT_HOME=/u01/dmdb
# 实例名
INSTANCE_NAME=TONGXIN
# 实例安装目录
INSTANCE_PATH=/u01/dmdb

declare() {
    # 脚本用法用量说明
    echo "==please input $1 [command] [options] <other options>"
    echo "  command:
      create                 Create a service about options
      remote                 Remove a servuce about options 
  options:
      dmap                   Dm8 software
      dmserver               Dm8 instance
  other options:
      instance_name          Dm8 instance name"
}


if [ $# -lt 2 ]; then
    # 检查参数
    declare $0    
    exit 111 
fi


case $1 in 
    create)
        if [ $2 == dmap ]; then
            echo "==创建DmAPService的systemd管理&&启动DmAPService服务"
            sh $SOFT_HOME/script/root/root_installer.sh
            
        elif [ $2 == dmserver ]; then
            #cd $SOFT_HOME/script/root/ || exit 111
            echo "==创建DmService$INSTANCE_NAME服务&&启动DmService$INSTANCE_NAME服务"
            sh $SOFT_HOME/script/root/dm_service_installer.sh -t dmserver -dm_ini $INSTANCE_PATH/$INSTANCE_NAME/dm.ini  -p $INSTANCE_NAME || exit 111
            systemctl restart DmService$INSTANCE_NAME || exit 111           
            
        else
            echo "==请输入正确的值"
            declare $0
            exit 111
            
        fi;;
    remove)
        if [ $2 == dmap ]; then
            echo "==删除dmap启动的systemd管理"
            [ -f "$SOFT_HOME/root_uninstaller.sh" ] && sh $SOFT_HOME/root_uninstaller.sh 
           
        elif [ $2 == dmserver ]; then
            echo "==关闭DmService$INSTANCE_NAME服务"
            systemctl disable DmService$INSTANCE_NAME
            systemctl stop DmService$INSTANCE_NAME
            
        else
            echo "==请输入正确的值"
            declare $0
            exit 111
        fi;;
esac

cd $SCRIPT_HOME || exit 


标签:shell,grep,NAME,数据库,echo,INSTANCE,USER,dm8,HOME
From: https://www.cnblogs.com/qinwengang/p/17523733.html

相关文章

  • python连接Oracle数据库实现数据查询并导入MySQL数据库
    1.项目背景由于项目需要连接第三方Oracle数据库,并从第三方Oracle数据库中查询出数据并且显示,而第三方的Oracle数据库是Oracle11的数据库。而django4.1框架支持支持Oracle数据库服务器19c及以上版本,需要7.0或更高版本的cx_OraclePython驱动;django3.2支持Oracle数据库......
  • 阿里数据库内核月报
    数据库内核月报2023/06MySQL·源码解析·并发Replaceinto导致死锁MySQL·源码解析·mysql子查询执行方式介绍极致性价比:自研数据库PolarDBon自研芯片倚天2023/05MySQL·源码解析·InnoDB中undo日志的组织及实现PolarDB·引擎特性·DDL中MDL锁的优化和演......
  • shell命令之expect报错
    一、运行shtest.sh报错test.sh:行7:spawn:未找到命令test.sh:行10:send:未找到命令test.sh:行11:interact:未找到命令原因:第一,你不该用sh,因为这是expect脚本,直接执行./e.sh或expect./e.sh第二,确认expect是在/usr/local/bin下参考:https://zhidao.baidu.com/question/7528840......
  • Navicat 查看数据库密码
    Navicat提供了导出连接的功能,当忘记数据库密码时,可以通过导出文件中的加密密码进行解密,查看数据库密码。导出链接,选择需要查看密码的数据库勾选导出密码,否则无法查看在导出文件中找到加密密码在线工具解密,https://tool.lu/coderunner,输入PHP代码,并将文件中的加密密......
  • MYSQL数据库转DM达梦数据库函数替换及注意事项
    1、调整IF函数为 case 函数MYSQL: IF(condition, value_if_true, value_if_false) if(a.class_sort_code='0301',(selectgroup_concat(sku_attr_id)sku_Attrfroma_sku_attr_relaWHEREmodel_id=a.model_idorderbysku_attr_id),'')sku_attrD......
  • 数据库
    1.关系型数据库mysqlsqlserveroraclepostgresql2.非关系型数据库redis1、Redis数据库与接口缓存2、Redis缓存穿透,缓存击穿,缓存雪崩原因与解决方案3、Redis哨兵模式集群高可用esMongoDBHBaseNeo4j3.时序数据库InfluxDBpython操作InfluxDB......
  • bartender配数据库链接
          ......
  • 数据库中用户删除不掉总是报错,依赖如何处理干净?
    摘要:本文主要介绍如何进行用户的各种依赖识别与清理,并简单介绍下推荐的权限管理方式。本文分享自华为云社区《GaussDB(DWS)用户删除不掉时候总是报错,依赖如何处理干净?》,作者:Malick。数据库的使用中,有时会遇到某些用户离职,或者角色变更时,要对其账号进行销户,权限进行回收等操作......
  • Shell | 数值运算
    Shell和其它编程语言不同,Shell不能直接进行算数运算,必须使用数学计算命令Shell中常用的数学计算命令如下所示:$((expression))操作符:可以进行基本的整数运算。let:对整数进行数学运算,类似于$((expression))操作符。expr:可以进行基本的整数运算,如加减乘除、取模等......
  • 达梦数据库 表被锁的处理办法
    1、查看锁语句SELECT*FROMV$LOCKWHEREBLOCKED='1'; 字段说明:【ADDR 】列表示锁的内存地址;【TRX_ID 】列表示锁所属的事务ID;【LTYPE 】列表示锁的类型,可能是OBJECT(对象锁)或者TID(TID锁);【LMODE】列表示锁的模式,可能的取值有S(共享锁)、X(排他......