Files
tms-mysql-pkg/smarttms-mysql.install
2025-12-14 16:38:21 +08:00

121 lines
4.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# smarttms-mysql.install
#
# 这个脚本处理 smarttms-mysql 包的安装、升级和卸载时的操作。
pkgname="smarttms-mysql"
mysql_install_dir="/usr/local/mysql"
mysql_data_dir="/data/mysql"
mysql_user="mysql"
mysql_group="mysql"
mysql_uid="306" # 与 RPM spec 保持一致
mysql_gid="306" # 与 RPM spec 保持一致
mysql_root_password="PythA90ra5"
mysql_sql_file="/usr/share/${pkgname}/tms_db_s3.sql"
mysql_service="mysqld.service"
# pre_install 钩子在安装文件复制到系统之前运行
pre_install() {
# 创建 mysql 用户和组,如果不存在的话
if ! getent group "${mysql_group}" >/dev/null; then
echo "Creating group '${mysql_group}' with GID ${mysql_gid}..."
groupadd -r -g "${mysql_gid}" "${mysql_group}"
fi
if ! getent passwd "${mysql_user}" >/dev/null; then
echo "Creating user '${mysql_user}' with UID ${mysql_uid}..."
useradd -r -g "${mysql_gid}" -u "${mysql_uid}" -d "${mysql_data_dir}" -s /sbin/nologin "${mysql_user}"
fi
# 创建数据目录并设置权限
if [ ! -d "${mysql_data_dir}" ]; then
echo "Creating MySQL data directory '${mysql_data_dir}'..."
mkdir -p "${mysql_data_dir}"
chown "${mysql_user}":"${mysql_group}" "${mysql_data_dir}"
chmod 0750 "${mysql_data_dir}"
fi
}
# post_install 钩子在安装文件复制到系统之后运行
post_install() {
local install_type="$1"
local timeout=60 # 等待 MySQL 启动的超时时间
# 由于数据库初始化和导入已在 PKGBUILD 的 package() 函数中完成,
# post_install 钩子现在只负责在目标系统上启动和启用服务。
echo "Reloading systemd daemon..."
systemctl daemon-reload
# 确保数据目录的权限正确,即使在 PKGBUILD 中已设置 root:root
# 这里再次设置确保在目标系统上是 mysql 用户拥有
echo "Setting ownership for MySQL data directory '${mysql_data_dir}' to ${mysql_user}:${mysql_group}..."
chown -R "${mysql_user}":"${mysql_group}" "${mysql_data_dir}"
echo "Enabling MySQL service..."
systemctl enable "${mysql_service}"
echo "Starting MySQL service..."
systemctl start "${mysql_service}"
echo "Waiting for MySQL server to start (up to ${timeout} seconds)..."
for i in $(seq 1 "${timeout}"); do
"${mysql_install_dir}/bin/mysqladmin" ping -h 127.0.0.1 -P 3306 &>/dev/null && break
sleep 1
done
if ! "${mysql_install_dir}/bin/mysqladmin" ping -h 127.0.0.1 -P 3306 &>/dev/null; then
echo "Warning: MySQL server did not start in time. Please check system logs for '${mysql_service}'." >&2
# 不再强制退出,因为数据库已预初始化,服务启动失败可能是其他配置问题
else
echo "MySQL server is running."
fi
echo "SmartTMS MySQL installation/upgrade complete."
}
# pre_remove 钩子在卸载文件被删除之前运行
pre_remove() {
echo "Stopping MySQL service before removal..."
systemctl stop "${mysql_service}"
}
# post_remove 钩子在卸载文件被删除之后运行
post_remove() {
local remove_type="0" # 0 表示完全卸载1 表示升级
echo "Disabling MySQL service..."
systemctl disable "${mysql_service}"
echo "Reloading systemd daemon..."
systemctl daemon-reload
# 如果是完全卸载 (即 $1 为 0),则清理用户、组和数据目录
if [ "${remove_type}" -eq 0 ]; then
echo "Performing full uninstallation cleanup..."
rm -rf /usr/local/mysql-5.6.47-linux-glibc2.12-x86_64
rm -rf /usr/local/mysql
# 警告用户数据目录将被删除
echo "WARNING: This will remove the MySQL data directory '${mysql_data_dir}' and all its data."
echo "Removing MySQL data directory '${mysql_data_dir}'..."
rm -rf "${mysql_data_dir}"
# 删除用户和组
if getent passwd "${mysql_user}" >/dev/null; then
echo "Removing user '${mysql_user}'..."
userdel "${mysql_user}"
fi
if getent group "${mysql_group}" >/dev/null; then
echo "Removing group '${mysql_group}'..."
groupdel "${mysql_group}"
fi
# 删除 PATH 环境变量配置脚本
echo "Removing PATH configuration script '/etc/profile.d/mysql.sh'..."
rm -f /etc/profile.d/mysql.sh
echo "Full uninstallation cleanup complete."
else # remove_type is 1 (upgrade)
echo "MySQL package upgraded. Data directory and user/group are retained."
fi
}