MySQL二进制安装脚本
安装版本:MySQL8.0.20
#!/bin/bash
# 设置安装版本
MYSQL_VERSION=mysql-8.0.20-linux-glibc2.12-x86_64
# 设置下载地址
DOWNLOAD_URL=https://downloads.mysql.com/archives/get/p/23/file
# DOWNLOAD_URL=http://192.168.3.200/Software
# 设置MySQL的root密码
MYSQL_PASSWORD=root123
# 设置安装路径
MYSQL_PATH=/usr/local/mysql
# 设置数据存放路径
MYSQL_DATA=$MYSQL_PATH/data
# 设置日志存放路径
MYSQL_LOGS=$MYSQL_PATH/logs
# 检测操作系统
if [ -f /etc/redhat-release ]; then
# CentOS 或 RHEL
OS=centos
PKG_MANAGER="yum"
elif [ -f /etc/debian_version ]; then
# Ubuntu 或 Debian
OS=ubuntu
PKG_MANAGER="apt"
else
echo "不支持的操作系统"
exit 1
fi
# 检测脚本执行是否成功
ck_ok()
{
if [ $? -ne 0 ]
then
echo "$1 error."
exit 1
fi
}
# 更新包列表并安装依赖(根据操作系统选择包管理器)
install_dependencies() {
if [ "$OS" == "centos" ]; then
$PKG_MANAGER install -y libaio libaio-devel ncurses ncurses-devel openssl openssl-devel
elif [ "$OS" == "ubuntu" ]; then
$PKG_MANAGER update
$PKG_MANAGER install -y libaio1 libaio-dev libtinfo5 libncurses-dev libncurses5 libncurses5-dev libncursesw5 libncursesw5-dev libssl-dev
fi
ck_ok "安装依赖失败!"
}
download_mysql(){
cd /usr/local/src
# 下载安装包
if [ -f $MYSQL_VERSION.tar.xz ]
then
echo "当前目录已经存在$MYSQL_VERSION.tar.xz"
else
wget $DOWNLOAD_URL/$MYSQL_VERSION.tar.xz
ck_ok "下载失败!"
echo "下载成功!"
fi
}
create_mysql_path(){
cd /usr/local/src
# 解压缩安装包
tar -xvf $MYSQL_VERSION.tar.xz
ck_ok "解压失败!"
echo "解压缩成功! "
# 移动文件夹至安装目录
mv $MYSQL_VERSION $MYSQL_PATH
echo "安装目录创建成功!"
# 创建MySQL用户和用户组
useradd -s /bin/nologin mysql
# ck_ok "创建失败!"
# echo "创建MySQL用户成功!"
# 更改安装目录权限
mkdir -p $MYSQL_DATA
mkdir -p $MYSQL_LOGS
chown -R mysql:mysql $MYSQL_PATH
echo "chown -R mysql:mysql $MYSQL_PATH"
# 设置MySQL环境变量
echo "export MYSQL_PATH=$MYSQL_PATH" >> ~/.bashrc
echo 'export PATH="$MYSQL_PATH/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
echo "设置MySQL环境变量成功!"
}
set_my_cnf(){
# 配置/etc/my.cnf
tee /etc/my.cnf > /dev/null <<EOF
[mysqld]
port = 3306
basedir = $MYSQL_PATH
datadir = $MYSQL_DATA
log-error = $MYSQL_LOGS/error.log
pid-file = $MYSQL_LOGS/mysql.pid
socket = /tmp/mysql.sock
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default-storage-engine=InnoDB
default-authentication-plugin=mysql_native_password
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysql]
socket = /tmp/mysql.sock
EOF
ck_ok "配置/etc/my.cnf失败!"
echo "配置/etc/my.cnf成功"
}
install_mysql(){
# 初始化MySQL数据目录
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
ck_ok "初始化MySQL数据目录!"
echo "MySQL初始化成功"
}
start_mysql(){
# 启动MySQL服务
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysqld restart
systemctl enable mysqld
ck_ok "启动MySQL"
echo "启动MySQL"
# 获取初始密码
initial_password=$(grep 'temporary password' $MYSQL_LOGS/error.log | awk '{print $NF}')
echo "获取初始密码为:$initial_password"
# 修改root用户密码
/usr/local/mysql/bin/mysql -uroot -p"$initial_password" --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';"
echo "修改MySQL初始密码为:$MYSQL_PASSWORD"
# 重启MySQL服务
pkill -9 mysqld
systemctl restart mysql
ck_ok "启动MySQL"
echo "安装完成,请您放心使用,登录密码为:$MYSQL_PASSWORD"
}
install_dependencies
download_mysql
create_mysql_path
set_my_cnf
install_mysql
start_mysql