centos 7 安装 mysql 脚本
准备
自备 MySQL 安装包
脚本配置
查看脚本头部内容,自定义安装信息:端口、密码等
创建 bash 脚本(install-mysql.sh)
点击查看代码
#!/bin/bash
# MySQL 安装配置参数
LOG_DIR="/usr/local/mysql/log" # 日志目录
MYSQL_DATA_DIR="/usr/local/mysql/data" # MySQL 数据目录
MYSQL_GROUP="mysql" # MySQL 用户组
MYSQL_USER="mysql" # MySQL 用户
MYSQL_ROOT_PASSWARD="***" # MySQL root 密码
MYSQL_BASE_DIR="/usr/local/mysql/" # MySQL 安装目录
MYSQL_PACKAGE_DIR="/home/bash/download" # MySQL 压缩包所在目录
MYSQL_PACKAGE_NAME="mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz" # MySQL 压缩包文件名
MYSQL_CONFIG="/etc/my.cnf" # MySQL 配置文件
MYSQL_SERVICE="mysqld" # MySQL 服务名
MYSQL_PORT="3306" # MySQL 端口
SQL_FILE="/home/bash/download/deploy.sql" # SQL 文件路径
# 检测文件夹是否存在并创建
check_and_create_folder() {
local folder_path=$1
if [ ! -d "$folder_path" ]; then
echo "文件夹 '$folder_path' 不存在,正在创建..."
mkdir -p "$folder_path"
if [ $? -eq 0 ]; then
echo "文件夹 '$folder_path' 已成功创建。"
else
echo "创建文件夹 '$folder_path' 失败,请检查权限或路径。"
fi
else
echo "文件夹 '$folder_path' 已存在。"
fi
}
# 调用函数检查并创建每个文件夹
check_and_create_folder "$LOG_DIR"
check_and_create_folder "$MYSQL_DATA_DIR"
check_and_create_folder "$MYSQL_BASE_DIR"
# 创建日志文件
log="${LOG_DIR}/mysql_install.log"
# 卸载系统自带的MARIADB
rpm -qa|grep mariadb |xargs yum remove -y > /dev/null
# 解压MYSQL安装包
echo '===================================='
echo 'Unzip....'
tar -zxvf "${MYSQL_PACKAGE_DIR}/${MYSQL_PACKAGE_NAME}" --strip-components=1 -C "${MYSQL_BASE_DIR}"
if [ $? -ne 0 ];then
echo 'MySQL unzip failed!!!'
exit
else
echo '===================================='
echo 'Unpack the success!'
fi
# 创建MYSQL用户和用户组
# 检查用户组是否已经存在
if ! getent group ${MYSQL_GROUP} > /dev/null; then
groupadd ${MYSQL_GROUP}
fi
useradd -g ${MYSQL_GROUP} ${MYSQL_USER}
chown -R ${MYSQL_USER}:${MYSQL_GROUP} "${MYSQL_BASE_DIR}"
echo '===================================='
echo 'User created successfully!'
# MYSQL的配置文件
cat > "${MYSQL_CONFIG}" << EOF
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
port = ${MYSQL_PORT}
socket=/var/lib/mysql/mysql.sock
basedir=${MYSQL_BASE_DIR}
datadir=${MYSQL_DATA_DIR}
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
EOF
if [ $? -ne 0 ];then
echo 'Failed to write the my.cnf configuration file!!!'
exit
else
echo '===================================='
echo 'my.cnf configuration success!'
echo '===================================='
fi
# 创建 /var/lib/mysql 目录,并修改权限
mkdir /var/lib/mysql
chmod 777 /var/lib/mysql
# 开始安装 MYSQL
cd "${MYSQL_BASE_DIR}"
./bin/mysqld --initialize --user=${MYSQL_USER} --basedir=${MYSQL_BASE_DIR} --datadir=${MYSQL_DATA_DIR} 2>&1 | tee "${log}"
# 获取 root 初始密码
InitialPassword=$(tail -1${log} |awk '{print $NF}')
# 复制启动脚本到资源目录,并修改 basedir 和 datadir 为对应目录
cp ./support-files/mysql.server /etc/init.d/${MYSQL_SERVICE}
sed -i "s|^basedir=.*|basedir=${MYSQL_BASE_DIR}|" /etc/init.d/${MYSQL_SERVICE}
sed -i "s|^datadir=.*|datadir=${MYSQL_DATA_DIR}|" /etc/init.d/${MYSQL_SERVICE}
if [ $? -ne 0 ];then
echo 'Failed to replace /etc/init.d/mysqld!!!'
exit
else
echo '===================================='
echo '/etc/init.d/mysqld successfully modified!'
echo '===================================='
fi
# 设置MYSQL系统服务并开启自启
chmod +x /etc/init.d/${MYSQL_SERVICE}
chkconfig --add ${MYSQL_SERVICE}
# 启动MYSQLD
service ${MYSQL_SERVICE} restart
# 检查 MySQL 服务是否启动成功
netstat -ntlp | grep ${MYSQL_PORT}
if [ $? -ne 0 ];then
echo 'MySQL Boot failure!!!'
exit
else
echo '===================================='
echo 'start MySQL successfully!'
fi
# 将 MYSQL 的 BIN 目录加入 PATH 环境变量
echo "export PATH=\$PATH:${MYSQL_BASE_DIR}/bin" >> ~/.bash_profile
source ~/.bash_profile
if [ $? -ne 0 ];then
echo 'Variable write failed!!!'
exit
else
echo '===================================='
echo 'Variable write successful!'
echo '===================================='
fi
# 登陆MYSQL,并执行 SQL 语句
mysql -uroot -p"${InitialPassword}" --connect-expired-password << EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWARD}';
use mysql;
update user set user.Host='%' where user.User='root';
flush privileges;
EOF
if [ $? -ne 0 ];then
echo 'Failed to change the root password!!!'
exit
else
echo '===================================='
echo 'Root password has been changed successfully!'
echo '===================================='
fi
# 再次检查 MySQL 服务是否启动成功
netstat -ntlp | grep ${MYSQL_PORT}
if [ $? -ne 0 ];then
echo 'MySQL Boot failure!!!'
exit
else
echo '===================================='
echo 'MySQL service is running successfully!'
fi
echo 'You can now connect to MySQL with the new password.'
# All operations were successful and can be connected through Navicat!!!
echo '===================================='
echo 'All operations were successful!'
echo 'You can now connect to MySQL using Navicat or any other MySQL client.'
echo '===================================='
执行脚本
source ./install-mysql.sh
注:默认允许远程连接 root 用户,安全期间,确保防火墙已打开