centos 7 MySQL 离线一键自动安装脚本

#!/bin/bash set -e MYSQL_PACKAGE="/usr/local/autoinstall/lib/mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz" MYSQL_INSTALL_DIR="/usr/local/mysql" MYSQL_DATA_DIR="/dhxdata/mysql" MYSQL_SOCKET_DIR="/usr/local/mysql/socket" MYSQL_SOCKET_FILE="$MYSQL_SOCKET_DIR/mysql13306.sock" MYSQL_PASSWORD="tech@2023#YN" MYSQL_NEW_USER="mysql_fu" MYSQL_NEW_USER_PASSWORD="Fu@2023" MySQL_PORT="13306" DEPENDENCY_DIR="/usr/local/autoinstall/third-lib/" install_dependencies() { echo "安装依赖..." yum localinstall -y "$DEPENDENCY_DIR/ncurses-devel"/*.rpm yum localinstall -y "$DEPENDENCY_DIR/libaio-devel"/*.rpm } setup_mysql_account() { echo "创建MySQL用户和组..." groupadd mysql useradd -r -g mysql -s /bin/false mysql } setup_directories() { echo "创建目录并设置权限..." mkdir -p "$MYSQL_INSTALL_DIR" mkdir -p "$MYSQL_DATA_DIR" mkdir -p "$MYSQL_SOCKET_DIR" chown -R mysql:mysql "$MYSQL_INSTALL_DIR" chown -R mysql:mysql "$MYSQL_DATA_DIR" chown -R mysql:mysql "$MYSQL_SOCKET_DIR" } install_mysql() { echo "解压并安装MySQL..." tar -xf "$MYSQL_PACKAGE" --strip-components=1 -C "$MYSQL_INSTALL_DIR" } configure_mysql() { echo "将MySQL可执行文件添加到系统路径..." echo "export PATH=\$PATH:$MYSQL_INSTALL_DIR/bin" >> /etc/profile source /etc/profile echo "设置MySQL配置文件" cat << EOF > /etc/my.cnf [mysqld] user=mysql basedir=$MYSQL_INSTALL_DIR datadir=$MYSQL_DATA_DIR socket=$MYSQL_SOCKET_FILE port=$MySQL_PORT sql_mode = 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES' character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci EOF } setup_service() { echo "创建MySQL服务并设置为开机自启..." cat << EOF > /etc/systemd/system/mysqld.service [Unit] Description=MySQL Server After=network.target [Service] User=mysql Group=mysql ExecStart=$MYSQL_INSTALL_DIR/bin/mysqld --defaults-file=/etc/my.cnf ExecReload=/bin/kill -s HUP $MAINPID Restart=on-failure [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl start mysqld systemctl enable mysqld } initialize_mysql() { echo "初始化MySQL,并获取初始Root密码..." $MYSQL_INSTALL_DIR/bin/mysqld --initialize --user=mysql --basedir=$MYSQL_INSTALL_DIR --datadir=$MYSQL_DATA_DIR > mysql_init.log 2>&1 } main() { if [[ $EUID -ne 0 ]]; then echo "该脚本必须以root身份运行" exit 1 fi if [ ! -f "$MYSQL_PACKAGE" ]; then echo "MySQL安装包不存在." exit 1 fi install_dependencies setup_mysql_account setup_directories install_mysql configure_mysql setup_service initialize_mysql TEMP_PASSWORD=$(awk '/temporary password/{print $NF}' mysql_init.log) echo "MySQL临时Root密码: $TEMP_PASSWORD" echo "等待MySQL服务启动..." until mysqladmin ping --socket=$MYSQL_SOCKET_FILE --silent; do sleep 1 echo "等待MySQL启动..." done echo "重置MySQL Root密码..." mysql --user=root --password="$TEMP_PASSWORD" --connect-expired-password --socket=$MYSQL_SOCKET_FILE <<EOF ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD'; EOF echo "创建新的MySQL用户并授予所有权限..." mysql --user=root --password="$MYSQL_PASSWORD" --socket=$MYSQL_SOCKET_FILE <<EOF CREATE USER '$MYSQL_NEW_USER'@'%' IDENTIFIED BY '$MYSQL_NEW_USER_PASSWORD'; GRANT ALL PRIVILEGES ON *.* TO '$MYSQL_NEW_USER'@'%'; EOF echo "MySQL已经安装并启动,端口为$MySQL_PORT!" # 在防火墙中添加MySQL端口 firewall-cmd --zone=public --add-port=$MySQL_PORT/tcp --permanent firewall-cmd --reload } main "$@"
上一篇:ARM架构麒麟操作系统安装配置Mariadb数据库