GBase 8s数据库随系统启动、自重启配置
近期的客户想实现数据库自重启功能(即故障后自动重启),该功能需要在自动启动的基础上进行改进。
关于systemd方式实现自启动的方法可参考之前的文档:https://gbasedbt.com/index.php/archives/161/
以下仍是通过systemd方式实现GBase 8s数据库的自启动、自重启与关闭。
注意:自启动、自重启仅适用于单机,而不要用于集群环境
适用于操作系统: RHEL7/CENTOS7, 以及基于RHEL7内核的系统。
systemd需要的一个配置文件目录为
1,service 文件所在位置 /usr/lib/systemd/system/
1, 在/usr/lib/systemd/system/目录下创建 GBaseService_gbase01.service文件
主要的参数:
Type配置为forking,以便可以调用Restart
如果不需要重启,Type配置为oneshot,且把Restart/RestartSec注释掉
特别注意必须配置EnvironmentFile环境变量文件
# /usr/lib/systemd/system/GBaseService_gbase01.service
# 注:以下中文注释用于理解
[Unit]
# Wants:需要的服务
# After:在指定的服务正常启动后才会启动
Description=GBase 8s Database Server
Documentation=file:/opt/gbase/release/en_us/0333
Wants=network-online.target
After=network.target network-online.target
[Service]
# Type:oneshot只有在该服务的主服务进程退出之后,systemd 才会认为该服务启动完成,才会开始启动后继单元。
# forking守护进程(服务)在启动时会转入后台运行
# User:使用启动的用户
# Group:使用启动的用户组
# EnvironmentFile:使用的环境变量文件,以Key=Value的方式存储,需要注意的是 '$' 变量不扩展
# ExecStart:启动命令,需要绝对路径
# ExecStop:关闭命令,需要绝对路径
# TimeoutStartSec:服务启动超时时长,默认的DefaultTimeoutStartSec值为90秒
# Restart:always 只要不是通过systemctl stop来停止服务,任何情况下都必须要重启服务
# RestartSec:systemctl检测到异常,重启等待时长,单位是秒
#Type=oneshot
Type=forking
User=gbasedbt
Group=gbasedbt
RemainAfterExit=yes
Environment="CLIENT_LOCALE=zh_CN.utf8"
Environment="DB_LOCALE=zh_CN.utf8"
Environment="GBASEDBTSERVER=gbase01"
Environment="GBASEDBTSQLHOSTS=/opt/gbase/etc/sqlhosts"
Environment="GL_USEGLU=1"
Environment="GBASEDBTDIR=/opt/gbase"
Environment="DBDATE=Y4MD-"
Environment="ONCONFIG=onconfig.gbase01"
Environment="PATH=/opt/gbase/bin:/opt/gbase/sbin:/opt/gbase/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
ExecStart=/opt/gbase/bin/oninit
ExecStop=/opt/gbase/bin/onshutdown.sh
TimeoutStartSec=1800
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target
说明:
[Unit] 区块:启动顺序与依赖关系
[Service] 区块:启动行为
[Install] 区块:定义如何安装这个配置文件,即怎么做到开机启动。
注意:
1,以上环境变量(Environment)和目录需要依据实际使用情况添加和修改;
2,onshutdown.sh脚本中的shell请确认为sh而不是默认的ksh;
3,ExecStop之前可使用ExecStopPost;
2, 管理GBaseService_gbase01服务
systemd加入GBaseService_gbase01服务,并设置GBaseService_gbase01服务随系统启动
# systemctl enable GBaseService_gbase01
启动gbasedbt服务(数据库应未启动)
# systemctl start GBaseService_gbase01
检查gbasedbtd状态
# systemctl status GBaseService_gbase01
关闭gbasedbt服务
# systemctl stop GBaseService_gbase01
关闭gbasedbt服务的自启动
# systemctl disable GBaseService_gbase01
如果数据库使用onmode -ky正常关闭,需要使用systemctl restart方式重启数据库
# systemctl restart GBaseService_gbase01
如果数据库不是使用systemctl stop或者正常关闭,如使用kill -9或者onclean -ky或者异常关闭时,将在RestartSec指定的时间后自动启动数据库。如下:
[root@192 ~]# systemctl status GBaseService_gbase01
● gbasedbt.service - GBase 8s Database Server
Loaded: loaded (/usr/lib/systemd/system/GBaseService_gbase01; enabled; vendor preset: disabled)
Active: activating (auto-restart) (Result: signal) since Wed 2024-07-31 16:56:58 CST; 47s ago
Docs: file:/opt/gbase/release/en_us/0333
Process: 2707 ExecStart=/opt/gbase/bin/oninit (code=exited, status=0/SUCCESS)
Main PID: 2709 (code=killed, signal=KILL)
7月 31 16:56:58 192.168.80.151 systemd[1]: GBaseService_gbase01.service: Failed with result 'signal'.
附:创建systemd服务的脚本
如下BuildSystemd.sh
脚本使用root用户执行时,自动创建默认数据库环境下的自动启动
注意:自启动不应用于集群环境
#!/bin/bash
##################################################################
# Filename: BuildSystemd.sh
# Function: Build GBaseService_${servername}.service and add to systemd
# Write by: liaosnet@gbasedbt.com
# Version : 1.4.14
##################################################################
##### Defind env
export LANG=C
_loginfo(){
echo -e "[$(date +'%F %T')] $*"
}
if [ ! x"$(whoami)" = "xroot" ]; then
_loginfo "MUST root user to run!"
exit 1
fi
id gbasedbt >/dev/null 2>&1
if [ ! $? -eq 0 ]; then
_loginfo "USER gbasedbt not found!"
exit 1
fi
if [ ! -f /GBASEDBTTMP/.infxdirs ]; then
_loginfo ".infxdirs not found!"
exit 1
fi
GBASEDIR=$(awk '{print $1; exit}' /GBASEDBTTMP/.infxdirs)
# build env_file
GBASEENV=$(su - gbasedbt -c "env | egrep '(GBASEDBT|ONCONFIG|PATH|DB_LOCALE|CLIENT_LOCALE|GL_|DBDATE|LD_LIBRARY_PATH)'")
GBASESERVER=$(echo "${GBASEENV}" | awk -F'=' '/GBASEDBTSERVER/{print $2}')
if [ x"${GBASESERVER}" = x ]; then
_loginfo "Servername not found!"
exit 1
fi
#
if [ -f /usr/lib/systemd/system/GBaseService_${GBASESERVER}.service ]; then
_loginfo "GBaseService_${GBASESERVER}.service is exists! Please delete it."
exit 1
fi
# build systemd
_loginfo "Build GBaseService_${GBASESERVER}.service file."
cat <<EOF > GBaseService_${GBASESERVER}.service
# /usr/lib/systemd/system/GBaseService_${GBASESERVER}.service
# 注:以下中文注释用于理解
[Unit]
# Wants:需要的服务
# After:在指定的服务正常启动后才会启动
Description=GBase 8s Database Server
Documentation=file:${GBASEDIR}/release/en_us/0333
Wants=network-online.target
After=network.target network-online.target
[Service]
# Type:oneshot只有在该服务的主服务进程退出之后,systemd 才会认为该服务启动完成,才会开始启动后继单元。
# forking守护进程(服务)在启动时会转入后台运行
# User:使用启动的用户
# Group:使用启动的用户组
# EnvironmentFile:使用的环境变量文件,以Key=Value的方式存储,需要注意的是 '$' 变量不扩展
# ExecStart:启动命令,需要绝对路径
# ExecStop:关闭命令,需要绝对路径
# TimeoutStartSec:服务启动超时时长,默认的DefaultTimeoutStartSec值为90秒
# Restart:always 只要不是通过systemctl stop来停止服务,任何情况下都必须要重启服务
# RestartSec:systemctl检测到异常,重启等待时长,单位是秒
#Type=oneshot
Type=forking
User=gbasedbt
Group=gbasedbt
RemainAfterExit=yes
EOF
echo "${GBASEENV}" | awk '{print "Environment=\042"$0"\042"}' >> GBaseService_${GBASESERVER}.service
cat <<EOF >> GBaseService_${GBASESERVER}.service
ExecStart=${GBASEDIR}/bin/oninit
ExecStop=${GBASEDIR}/bin/onshutdown.sh
TimeoutStartSec=1800
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target
EOF
#
if [ -f ${GBASEDIR}/bin/onshutdown.sh ]; then
sed -i "s#/ksh#/sh#g" ${GBASEDIR}/bin/onshutdown.sh
fi
#
_loginfo "Move GBaseService_${GBASESERVER}.service to /usr/lib/systemd/system/ ."
if [ -f GBaseService_${GBASESERVER}.service ]; then
mv GBaseService_${GBASESERVER}.service /usr/lib/systemd/system/
fi
# 加入systemd
_loginfo "Add to systemd. If GBase 8s is Online, stop it. Then use systemctl start GBaseService_${GBASESERVER}.service ."
systemctl enable GBaseService_${GBASESERVER}.service
exit 0
注:本文更新于2025-08-02
附:systemd配置参数参考: https://www.jinbuguo.com/systemd/systemd-system.conf.html