#!/bin/bash
: '
Copyright (C) 2022 - now HaoZi Technology Co., Ltd.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
'
source <(curl -f -s --connect-timeout 10 --retry 3 https://dl.cdn.haozi.net/panel/public.sh)
if [ $? -ne 0 ]; then
echo "下载 public.sh 失败,请检查网络或稍后重试。"
echo "Download public.sh failed, please check the network or try again later."
exit 1
fi
channel=${1}
version=${2}
mysql_path="${setup_path}/server/mysql"
j=$(calculate_j)
# 安装依赖
if [ ${OS} == "rhel" ]; then
dnf makecache -y
dnf groupinstall "Development Tools" -y
dnf install cmake bison ncurses-devel libtirpc-devel openssl-devel pkg-config systemd-devel openldap-devel libudev-devel cyrus-sasl-devel cyrus-sasl-scram patchelf rpcgen rpcsvc-proto-devel krb5-devel zlib-devel readline-devel libcurl-devel -y
elif [ ${OS} == "debian" ] || [ ${OS} == "ubuntu" ]; then
apt-get update
apt-get install build-essential cmake bison libncurses5-dev libtirpc-dev libssl-dev pkg-config libsystemd-dev libldap2-dev libudev-dev libsasl2-dev libsasl2-modules-gssapi-mit patchelf libkrb5-dev zlib1g-dev libreadline-dev libcurl4-openssl-dev -y
else
error "不支持的操作系统"
fi
if [ "$?" != "0" ]; then
error "安装依赖软件失败"
fi
mysql_user_check=$(cat /etc/passwd | grep mysql)
if [ "${mysql_user_check}" == "" ]; then
groupadd mysql
useradd -s /sbin/nologin -g mysql mysql
fi
# 准备目录
rm -rf ${mysql_path}
mkdir -p ${mysql_path}
cd ${mysql_path}
# 下载源码
wget --retry-connrefused --retry-on-host-error --retry-on-http-error=429,500,502,503,504 -t 10 -T 120 -O ${mysql_path}/percona-server-${version}.7z ${download_url}/mysql/percona-server-${version}.7z
wget --retry-connrefused --retry-on-host-error --retry-on-http-error=429,500,502,503,504 -t 10 -T 120 -O ${mysql_path}/percona-server-${version}.7z.sha256 ${download_url}/mysql/percona-server-${version}.7z.sha256
if ! sha256sum --status -c percona-server-${version}.7z.sha256; then
rm -rf ${mysql_path}
error "mysql 校验失败"
fi
7z x percona-server-${version}.7z
rm -f percona-server-${version}.7z
rm -f percona-server-${version}.7z.sha256
# 编译
mv percona-server-${version} src
chmod -R 700 src
cd src
mkdir dist
cd dist
# 57 和 80 需要 boost 和禁用 TOKUDB
if [[ ${channel} == "percona_57" ]] || [[ ${channel} == "percona_80" ]]; then
WITH_BOOST="-DWITH_BOOST=../boost"
WITHOUT_TOKUDB="-DWITH_TOKUDB=0"
fi
# 80+ 禁用 MYSQLX 和 ROUTER
WITHOUT_MYSQLX="-DWITH_MYSQLX=0"
WITHOUT_ROUTER="-DWITH_ROUTER=0"
if [[ ${channel} == "percona_57" ]]; then
WITHOUT_MYSQLX=""
WITHOUT_ROUTER=""
fi
# 内存小于 8G 禁用 RocksDB
WITH_ROCKSDB=1
if [ ${MEM} -lt 7900 ]; then
WITH_ROCKSDB=0
fi
cmake .. -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_INSTALL_PREFIX=${mysql_path} -DMYSQL_DATADIR=${mysql_path}/data -DSYSCONFDIR=${mysql_path}/conf -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 ${WITHOUT_TOKUDB} -DWITH_ROCKSDB=${WITH_ROCKSDB} -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci ${WITHOUT_ROUTER} ${WITHOUT_MYSQLX} -DWITH_RAPID=0 -DENABLED_LOCAL_INFILE=1 -DWITH_DEBUG=0 -DWITH_UNIT_TESTS=OFF -DINSTALL_MYSQLTESTDIR= -DCMAKE_BUILD_TYPE=Release -DWITH_SYSTEMD=1 -DSYSTEMD_PID_DIR=${mysql_path} ${WITH_BOOST}
if [ "$?" != "0" ]; then
rm -rf ${mysql_path}
error "编译初始化失败"
fi
make "-j${j}"
if [ "$?" != "0" ]; then
rm -rf ${mysql_path}
error "编译失败"
fi
# 安装
make install
if [ "$?" != "0" ]; then
rm -rf ${mysql_path}
error "安装失败"
fi
# 配置
mkdir ${mysql_path}/conf
cat >${mysql_path}/conf/my.cnf </etc/systemd/system/mysqld.service </dev/null; then
echo "MySQL 服务已启动"
break
fi
echo "等待 MySQL 启动..."
sleep 1
done
if ! mysqladmin ping &>/dev/null; then
error "MySQL 启动超时"
fi
root_password=$(cat /dev/urandom | head -n 16 | sha256sum | head -c 16)
${mysql_path}/bin/mysqladmin -u root password ${root_password}
if [ "$?" != "0" ]; then
error "初始化密码失败"
fi
${mysql_path}/bin/mysql -uroot -p${root_password} -e "DROP DATABASE test;"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "DELETE FROM mysql.user WHERE user='';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "FLUSH PRIVILEGES;"
# 启用 RocksDB
if [ ${WITH_ROCKSDB} -eq 1 ]; then
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_CFSTATS SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_DBSTATS SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_PERF_CONTEXT SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_PERF_CONTEXT_GLOBAL SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_CF_OPTIONS SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_GLOBAL_INFO SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_COMPACTION_HISTORY SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_COMPACTION_STATS SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_ACTIVE_COMPACTION_STATS SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_DDL SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_INDEX_FILE_MAP SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_LOCKS SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_TRX SONAME 'ha_rocksdb.so';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "INSTALL PLUGIN ROCKSDB_DEADLOCK SONAME 'ha_rocksdb.so';"
fi
panel-cli app write mysql ${channel} ${version}
panel-cli setting write mysql_root_password ${root_password}
panel-cli database add-server --type=mysql --name=local_mysql --host=127.0.0.1 --port=3306 --username=root --password=${root_password}
echo -e $HR
echo "安装完成"
echo -e $HR