本文章主要介绍瀚高数据库的rpm安装步骤(单机安全版)
1、目录结构
/
├─momo 安装包路径
│ ├─hgdb-see-4.5.8-db43858.aarch64.rpm 安装包
│ ├─hgdb-install.sh 安装脚本
│ ├─hgdb-back-run.sh 瀚高数据库备份脚本
│ ├─hgdb-back.sh 瀚高数据库备份脚本
│ ├─app 应用安装路径
2、部署脚本
hgdb-install.sh(按需更改安装路径)
#!/bin/bash
#安装包路径
package_path="/momo"
#应用安装路径
install_path="/momo/app"
#瀚高数据库安装包名
hgdb_inatall_name=$(find . -name "*hgdb*.rpm" | grep -oE '[^/]+\.rpm$')
# 版本号
hgdb_version="4.5.8"
# 端口号
hgdb_port="5866"
# 密码(需要与初始化数据库时设置的密码相同)
hgdb_password="1qaz@WSX"
# 业务库账号密码以及数据库名称
# 账号
initial_mysql_user_name="root"
# 密码
initial_mysql_user_pwd="1qaz@WSX"
# 数据库名称
initial_mysql_db_name="momo"
# 数据库服务端安装路径
hg_data_path=$install_path/highgo/data
# 数据库备份路径
hg_backup_path=$install_path/dbbackup
# 获取本机的IP地址和主机名
IP_ADDRESS=$(hostname -I | awk '{print $1}')
HOST_NAME=$(hostname)
# 检查是否有root权限
if [ "$EUID" -ne 0 ]
then echo "请使用root权限运行此脚本"
exit
fi
cd $package_path
echo "----------开始安装瀚高数据库----------"
echo "----------1、配置hosts----------"
# 检查是否已经存在该主机名
if grep -q $HOST_NAME /etc/hosts; then
echo "$HOST_NAME already exists in /etc/hosts"
else
# 将新的IP地址和主机名添加到/etc/hosts文件末尾
echo "$IP_ADDRESS $HOST_NAME" >> /etc/hosts
echo "Added $HOST_NAME to /etc/hosts"
fi
echo "----------2、配置操作系统 limits----------"
# 设置系统参数
if grep -q -v highgo /etc/security/limits.conf; then
# 将新的IP地址和主机名添加到/etc/hosts文件末尾
echo "highgo soft core unlimited" >> /etc/security/limits.conf
echo "highgo hard nproc unlimited" >> /etc/security/limits.conf
echo "highgo soft nproc unlimited" >> /etc/security/limits.conf
echo "highgo hard memlock unlimited" >> /etc/security/limits.conf
echo "highgo hard nofile 1024000" >> /etc/security/limits.conf
echo "highgo soft memlock unlimited" >> /etc/security/limits.conf
echo "highgo soft nofile 1024000" >> /etc/security/limits.conf
echo "highgo hard stack 65536" >> /etc/security/limits.conf
echo "highgo soft stack 65536" >> /etc/security/limits.conf
fi
echo "----------3、安装----------"
mkdir -p $hg_data_path
rpm -ivh $hgdb_inatall_name
echo "----------4、配置瀚高数据库环境变量----------"
# 设置环境变量
if grep -q -v HG_BASE ~/.bash_profile;
then
echo "export HG_BASE=/opt/highgo" >> ~/.bash_profile
fi
if grep -q -v HGDB_HOME ~/.bash_profile;
then
echo "export HGDB_HOME=/opt/highgo/hgdb-see-$hgdb_version" >> ~/.bash_profile
fi
if grep -q -v PGPORT ~/.bash_profile;
then
echo "export PGPORT=$hgdb_port" >> ~/.bash_profile
fi
if grep -q -v PATH=$HGDB_HOME ~/.bash_profile;
then
echo "export PATH=\$HGDB_HOME/bin:\$PATH" >> ~/.bash_profile
fi
if grep -q -v PGDATA ~/.bash_profile;
then
echo "export PGDATA=$hg_data_path" >> ~/.bash_profile
fi
source ~/.bash_profile
echo "----------5、请按照提示信息初始化瀚高数据库----------"
# 初始化数据库
cd /opt/highgo/hgdb-see-$hgdb_version/bin
./initdb -D $hg_data_path -e sm4 -c "echo 12345678" > /opt/highgo/hgdb-see-$hgdb_version/bin/initdb.log
# 设置数据库初始化设置
if grep -q char $hg_data_path/postgresql.auto.conf; then
echo "数据库初始化参数已设置"
else
echo "nls_length_semantics = 'char'" >> $hg_data_path/postgresql.auto.conf
echo "listen_addresses = '*'" >> $hg_data_path/postgresql.auto.conf
echo "max_connections = '2000'" >> $hg_data_path/postgresql.auto.conf
echo "checkpoint_completion_target = '0.8'" >> $hg_data_path/postgresql.auto.conf
echo "log_destination = 'csvlog'" >> $hg_data_path/postgresql.auto.conf
echo "logging_collector = 'on'" >> $hg_data_path/postgresql.auto.conf
echo "log_directory = 'hgdb_log'" >> $hg_data_path/postgresql.auto.conf
echo "log_filename = 'highgodb_%d.log'" >> $hg_data_path/postgresql.auto.conf
echo "log_rotation_age = '1d'" >> $hg_data_path/postgresql.auto.conf
echo "log_rotation_size = '0'" >> $hg_data_path/postgresql.auto.conf
echo "log_truncate_on_rotation = 'on'" >> $hg_data_path/postgresql.auto.conf
echo "log_statement = 'ddl'" >> $hg_data_path/postgresql.auto.conf
echo "archive_mode = 'on'" >> $hg_data_path/postgresql.auto.conf
echo "compatible_db = 'mysql'" >> $hg_data_path/postgresql.auto.conf
fi
echo "----------6、拷贝 ssl 证书并赋权----------"
# 拷贝 ssl 证书并赋权
./hg_sslkeygen.sh $hg_data_path
chmod 0600 $hg_data_path/server.*
echo "----------7、启动瀚高数据库----------"
# 启动数据库
pg_ctl start
echo "----------8、创建瀚高数据库连接配置----------"
# 配置连接设置(用于其他服务器连接)
if grep -q 0.0.0.0 $hg_data_path/pg_hba.conf; then
echo "连接参数已设置"
else
echo "host all all 0.0.0.0/0 sm3" >> $hg_data_path/pg_hba.conf
fi
# 添加密码验证
rm -rf ~/.pgpass
echo "localhost:$hgdb_port:highgo:sysdba:$hgdb_password" >> ~/.pgpass
echo "localhost:$hgdb_port:highgo:syssso:$hgdb_password" >> ~/.pgpass
echo "localhost:$hgdb_port:$initial_mysql_db_name:sysdba:$hgdb_password" >> ~/.pgpass
chmod 0600 ~/.pgpass
echo "----------9、创建初始化SQL文件(可根据自己需要初始化SQL文件执行即可)----------"
rm -rf $package_path/initial_db_dba.sql
rm -rf $package_path/initial_dba.sql
rm -rf $package_path/initial_sso.sql
# 初始化账号
echo "create user $initial_mysql_user_name password '$initial_mysql_user_pwd';" >> $package_path/initial_db_dba.sql
# 创建数据库实例
echo "create database $initial_mysql_db_name owner $initial_mysql_user_name;" >> $package_path/initial_db_dba.sql
# 创建隐式转换
echo "create or replace function boo_cast_smallint(boolean) returns smallint as \$\$ begin if \$1=true then return 1; else return 0; end if; end; \$\$ language plpgsql;" >> $package_path/initial_dba.sql
echo "create cast(boolean as smallint) with function boo_cast_smallint as implicit;" >> $package_path/initial_dba.sql
echo "create cast(VARCHAR as SMALLINT) with inout as implicit;" >> $package_path/initial_dba.sql
# 创建函数
echo "CREATE OR REPLACE FUNCTION ifnull(invalue1 varchar,invalue2 varchar) RETURNS text AS \$BODY\$ begin if inValue1 is null then return inValue2::text; else return inValue1::text; end if; end; \$BODY\$ LANGUAGE plpgsql;" >> $package_path/initial_db_dba.sql
# 设置密码永不过期
echo "select set_secure_param('hg_idcheck.pwdvaliduntil','0');" >> $package_path/initial_sso.sql
echo "----------10、执行初始化SQL文件----------"
# 创建数据库
psql -U sysdba -d highgo -f $package_path/initial_db_dba.sql
# 设置密码永不过期
psql -U syssso -d highgo -f $package_path/initial_sso.sql
# 创建隐式转换和内置函数
psql -U sysdba -d $initial_mysql_db_name -f $package_path/initial_dba.sql
# 删除密码
sed -i '/syssso/d' ~/.pgpass
source ~/.bash_profile
pg_ctl restart
rm -rf $package_path/initial_db_dba.sql
rm -rf $package_path/initial_dba.sql
rm -rf $package_path/initial_sso.sql
echo "----------瀚高数据库安装完毕----------"
echo "----------瀚高数据库备份设置----------"
mkdir -p $hg_backup_path/sql
mkdir -p $hg_backup_path/log
cp -rf $package_path/hgdb-back-run.sh $hg_backup_path
cp -rf $package_path/hgdb-back.sh $hg_backup_path
(crontab -l; echo "00 05 * * * cd $hg_backup_path/;sh hgdb-back-run.sh > $hg_backup_path/log/sh.log 2>&1") | crontab -
echo "----------瀚高数据库备份设置完成----------"
hgdb-back-run.sh(按需更改日志保存路径)
echo "************************************"
echo "----------Task Start--------------"
echo "************************************"
#日志保存路径
backup_dir="/momo/app/dbbackup/log"
#当前时间
NOW_TIME="$(date +"%Y%m%d")"
mkdir -p $backup_dir
sh hgdb-back.sh >> $backup_dir/$NOW_TIME.log 2>&1
#删除多余的文件,只保留30个文件
#保留文件数
#declare -i 将变量声明为整数
declare -i ReservedNum=30
#查询当前文件夹下文件数量
declare -i FileNum=$(ls -l $backup_dir/* |grep ^- |wc -l)
#旧数据在上,从head开始删除
while(($FileNum > $ReservedNum))
do
OldFile=$backup_dir/$(ls -rt $backup_dir/| head -1)
echo "Delete File:" $OldFile
rm -rf $OldFile
let "FileNum--"
done
echo "************************************"
echo "----------Task End--------------"
echo "************************************"
hgdb-back.sh(按需更改备份存储路径、数据库端口号、数据库名称)
echo "************************************"
echo "----------Backup Start--------------"
echo "************************************"
#数据库信息
#use不要更改
db_use="sysdba"
#host不要更改
db_host="localhost"
db_prot="5866"
db_key="supplydemand_refactor"
#命令
DBDUMP_METHODB=/opt/highgo/hgdb-see-4.5.8/bin/pg_dump
#备份存储路径
backup_dir="/momo/app/dbbackup/sql"
#当前时间
NOW_TIME="$(date +"%Y%m%d_%H-%M-%S")"
mkdir -p $backup_dir
#要保存的数据库
$DBDUMP_METHODB -h $db_host -p $db_prot -U $db_use -d $db_key -v -Fc > $backup_dir/$db_key-$NOW_TIME.hgdmp
#删除多余的文件,只保留30个文件
#保留文件数
#declare -i 将变量声明为整数
declare -i ReservedNum=30
#查询当前文件夹下文件数量
declare -i FileNum=$(ls -l $backup_dir/* |grep ^- |wc -l)
#旧数据在上,从head开始删除
while(($FileNum > $ReservedNum))
do
OldFile=$backup_dir/$(ls -rt $backup_dir/| head -1)
echo "Delete File:" $OldFile
rm -rf $OldFile
let "FileNum--"
done
echo "************************************"
echo "----------Backup End--------------"
echo "************************************"
3、执行命令
cd /momo
sh -x hgdb-install.sh