181 lines
5.4 KiB
Bash
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.

#!/bin/bash
set -u
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_MARIADB="$(cd "${SCRIPT_DIR}/../../../../SDK/mariadb" 2>/dev/null && pwd || true)"
DEB_DIR="${1:-}"
log() {
echo "[ParkingSpaceGuide][MariaDB] $*"
}
wait_for_dpkg_lock() {
local i=0
while [ "$i" -lt 60 ]; do
if ! fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1 \
&& ! fuser /var/lib/dpkg/lock >/dev/null 2>&1; then
return 0
fi
sleep 2
i=$((i + 1))
done
log "等待 dpkg 锁超时"
return 1
}
if [ -z "${DEB_DIR}" ]; then
if [ -d /opt/parkingspaceguide/mariadb-offline ]; then
DEB_DIR="/opt/parkingspaceguide/mariadb-offline"
elif [ -n "${REPO_MARIADB}" ] && [ -d "${REPO_MARIADB}" ]; then
DEB_DIR="${REPO_MARIADB}"
else
DEB_DIR="${SCRIPT_DIR}/mariadb-offline"
fi
fi
if [ ! -d "${DEB_DIR}" ]; then
log "找不到 MariaDB 离线包目录: ${DEB_DIR}"
exit 1
fi
shopt -s nullglob
DEB_FILES=( "${DEB_DIR}"/*.deb )
shopt -u nullglob
if [ ${#DEB_FILES[@]} -eq 0 ]; then
log "目录中没有 .deb 文件: ${DEB_DIR}"
exit 1
fi
libc_has_glibc_233() {
if [ -e /lib/aarch64-linux-gnu/libc.so.6 ] && command -v objdump >/dev/null 2>&1; then
objdump -T /lib/aarch64-linux-gnu/libc.so.6 2>/dev/null | grep -q 'GLIBC_2\.33'
return $?
fi
return 1
}
# Ubuntu 22.04 / MariaDB 10.6 的包在 20.04 上不可装(要 libc6>=2.34、libssl3)。
# 20.04 的 libaio1 / gawk / rsync / socat / libnuma1_2.0.12 必须装。
should_skip_system_deb() {
local name
name="$(basename "$1")"
case "${name}" in
libnuma1_2.0.14*|libnuma1_*ubuntu2*|liburing2_*|libpmem1_*|galera-4_*|libdaxctl1_*|libndctl6_*|*10.6*|libssl3_*)
return 0
;;
esac
return 1
}
FILTERED_DEBS=()
for deb in "${DEB_FILES[@]}"; do
if should_skip_system_deb "${deb}"; then
log "跳过系统库,避免覆盖板子 glibc 不兼容的文件: $(basename "${deb}")"
continue
fi
FILTERED_DEBS+=("${deb}")
done
DEB_FILES=("${FILTERED_DEBS[@]}")
if [ ${#DEB_FILES[@]} -eq 0 ]; then
log "过滤后没有可安装的 MariaDB 包"
exit 1
fi
if [ -e /lib/aarch64-linux-gnu/libnuma.so.1 ] &&
command -v objdump >/dev/null 2>&1 &&
objdump -T /lib/aarch64-linux-gnu/libnuma.so.1 2>/dev/null | grep -q 'GLIBC_2\.33' &&
! libc_has_glibc_233; then
log "当前 libnuma.so.1 需要 GLIBC_2.33,本机 libc 没有该版本。"
log "请安装 Ubuntu 20.04 的 libnuma1_2.0.12-1_arm64.deb 后再启动应用。"
log "下载:http://ports.ubuntu.com/pool/main/n/numactl/libnuma1_2.0.12-1_arm64.deb"
fi
if ! wait_for_dpkg_lock; then
exit 1
fi
export DEBIAN_FRONTEND=noninteractive
# 上次若已解开 22.04/10.6 包,dpkg --configure -a 仍会去配它们,先卸掉。
log "清除未配置完成的 Ubuntu 22.04 / MariaDB 10.6 残留包"
dpkg --remove --force-depends \
libdaxctl1 libndctl6 libpmem1 liburing2 galera-4 libssl3 \
mariadb-server-10.6 mariadb-server-core-10.6 \
mariadb-client-10.6 mariadb-client-core-10.6 \
>/dev/null 2>&1 || true
log "安装离线 MariaDB: ${DEB_DIR}(共 ${#DEB_FILES[@]} 个包)"
for deb in "${DEB_FILES[@]}"; do
log " $(basename "$deb")"
done
# 一次传入全部离线包,dpkg 才能解析 mariadb-server 对 mariadb-server-core 等依赖。
install_offline_debs() {
dpkg -i "${DEB_FILES[@]}"
}
if ! install_offline_debs; then
log "第一次安装未完全成功,配置已解开的包后再装一次"
dpkg --configure -a || true
install_offline_debs || true
fi
dpkg --configure -a || true
if ! dpkg -s mariadb-server-10.3 >/dev/null 2>&1 &&
! dpkg -s mariadb-server >/dev/null 2>&1; then
log "mariadb-server 仍未安装。离线目录可能缺依赖包,或包版本与板子系统不一致。"
log "当前 MariaDB/MySQL 包状态:"
dpkg -l 'mariadb*' 'mysql-common' 2>/dev/null || true
dpkg --audit || true
exit 1
fi
systemctl enable mariadb >/dev/null 2>&1 || systemctl enable mysql >/dev/null 2>&1 || true
systemctl start mariadb >/dev/null 2>&1 || systemctl start mysql >/dev/null 2>&1 || true
CONF_DIR="/etc/mysql/mariadb.conf.d"
if [ -d "${CONF_DIR}" ]; then
cat > "${CONF_DIR}/99-parkingspaceguide.cnf" <<'EOF'
[mysqld]
bind-address = 0.0.0.0
EOF
systemctl restart mariadb >/dev/null 2>&1 || systemctl restart mysql >/dev/null 2>&1 || true
fi
MYSQL_BIN=""
for candidate in mysql mariadb; do
if command -v "${candidate}" >/dev/null 2>&1; then
MYSQL_BIN="${candidate}"
break
fi
done
if [ -z "${MYSQL_BIN}" ]; then
log "未找到 mysql/mariadb 客户端,跳过建库"
exit 1
fi
ready=0
for _ in $(seq 1 30); do
if "${MYSQL_BIN}" --protocol=socket -e "SELECT 1" >/dev/null 2>&1; then
ready=1
break
fi
sleep 1
done
if [ "${ready}" -ne 1 ]; then
log "MariaDB 服务未就绪,跳过建库"
exit 1
fi
"${MYSQL_BIN}" --protocol=socket <<'SQL'
CREATE DATABASE IF NOT EXISTS parking_guide DEFAULT CHARACTER SET utf8mb4;
CREATE USER IF NOT EXISTS 'parking'@'localhost' IDENTIFIED BY 'parking';
CREATE USER IF NOT EXISTS 'parking'@'%' IDENTIFIED BY 'parking';
GRANT ALL PRIVILEGES ON parking_guide.* TO 'parking'@'localhost';
GRANT ALL PRIVILEGES ON parking_guide.* TO 'parking'@'%';
FLUSH PRIVILEGES;
SQL
log "MariaDB 安装完成。默认库 parking_guide,账号 parking/parking。"
log "局域网可用只读工具连接板子 3306 端口。"