Files
tms-mysql-pkg/smarttms-mysql.install
2025-12-12 23:36:00 +08:00

160 lines
6.6 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 启动的超时时间
echo "Reloading systemd daemon..."
systemctl daemon-reload
# 仅在首次安装时初始化数据库、设置密码和导入数据
if [ ! -d "${mysql_data_dir}/mysql" ]; then
echo "Performing initial MySQL database setup..."
cd "${mysql_install_dir}" || { echo "Error: MySQL install directory not found."; exit 1; }
echo "Initializing MySQL database in '${mysql_data_dir}'..."
"${mysql_install_dir}/scripts/mysql_install_db" --datadir="${mysql_data_dir}" --user="${mysql_user}"
chown -R "${mysql_user}":"${mysql_group}" "${mysql_data_dir}"
# 1. 启动 MySQL 服务 (保持 --skip-name-resolve 启用)
echo "Starting MySQL service..."
systemctl start "${mysql_service}"
# 2. 等待 MySQL 服务启动并可用
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 "Error: MySQL server did not start in time for initial setup." >&2
exit 1
fi
echo "MySQL server is running."
# 3. 设置 root@localhost 的初始密码
# 这是 mysql_install_db 后最可靠的设置初始 root 密码的方式
echo "Setting initial root password for 'root@localhost' using mysqladmin..."
"${mysql_install_dir}/bin/mysqladmin" -uroot password "${mysql_root_password}"
echo "Initial root@localhost password set."
# 4. 更新所有 root 条目的密码 (包括 127.0.0.1, ::1, gg-3b8l1hmcqanykaan)
# 此时 --skip-name-resolve 保持启用,但我们更新的是 mysql.user 表中的条目。
# 连接时使用 'localhost',它通常可以通过 Unix socket 或特殊处理连接。
echo "Updating password for all root user entries (localhost, 127.0.0.1, ::1, gg-3b8l1hmcqanykaan)..."
"${mysql_install_dir}/bin/mysql" -uroot -p"${mysql_root_password}" -e "
UPDATE mysql.user SET Password = PASSWORD('${mysql_root_password}') WHERE User = 'root' AND Host IN ('localhost', '127.0.0.1', '::1', 'gg-3b8l1hmcqanykaan');
FLUSH PRIVILEGES;
"
echo "All relevant root user passwords updated."
# 5. 删除匿名用户和 test 数据库,进一步安全加固
echo "Securing MySQL installation (dropping anonymous users and test database)..."
"${mysql_install_dir}/bin/mysql" -uroot -p"${mysql_root_password}" -e "
DELETE FROM mysql.user WHERE User='';
DROP DATABASE IF EXISTS test;
FLUSH PRIVILEGES;
"
echo "Anonymous users and test database removed."
# 6. 导入初始数据库
echo "Importing initial database schema from '${mysql_sql_file}'..."
if [ -f "${mysql_sql_file}" ]; then
"${mysql_install_dir}/bin/mysql" -uroot -p"${mysql_root_password}" < "${mysql_sql_file}"
echo "Database initialization and import complete."
else
echo "Error: ${mysql_sql_file} not found. Database import failed." >&2
exit 1
fi
else # 数据目录已存在,说明不是首次安装,可能是升级或者之前安装过
echo "MySQL data directory already exists. Skipping database initialization."
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
}