#!/bin/bash
ck_ok()
{
if [ $? -ne 0 ]
then
echo "$1 error."
exit 1
fi
}
download_ng()
{
if [ -f mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz ]
then
echo "当前目录已经存在mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz"
echo "检测md5"
mq_md5=`md5sum mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz|awk '{print $1}'`
if [ ${mq_md5} == '2acb6d9f1e590ff66cc5bae2378a964b' ]
then
return 0
else
sudo /bin/mv mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz.old
fi
fi
#wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
wget http://192.168.3.200/Software/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
ck_ok "下载mysql"
}
install_ng()
{
if [ -d mysql-8.0.20-linux-glibc2.12-x86_64 ]
then
echo "mysql已存在,无需解压"
else
tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
ck_ok "解压mysql"
# 移动文件夹至安装目录
mv mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local/mysql
ck_ok "安装目录创建成功!"
fi
# 创建MySQL用户和用户组
id mysql &>/dev/null
if [ $? -eq 0 ]
then
echo “用户 mysql已存在。”
else
useradd -s /bin/nologin mysql
echo "创建MySQL用户成功!"
fi
# 更改安装目录权限
mkdir -p /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql
echo "chown -R mysql:mysql /usr/local/mysql"
# 设置MySQL环境变量
sco=`grep 'export PATH="/usr/local/mysql/bin:$PATH"' ~/.bashrc `
if [ "${sco}" == 'export PATH="/usr/local/mysql/bin:$PATH"' ]
then
echo "环境变量已存在"
else
echo 'export PATH="/usr/local/mysql/bin:$PATH"' >> ~/.bashrc
fi
source ~/.bashrc
ck_ok "设置MySQL环境变量成功!"
# 配置/etc/my.cnf
tee /etc/my.cnf > /dev/null <<EOF
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/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
EOF
ck_ok "配置/etc/my.cnf成功"
# 安装依赖
echo "安装依赖"
if which yum >/dev/null 2>&1
then
for pkg in libaio libaio-devel ncurses ncurses-devel openssl openssl-devel
do
if ! rpm -q $pkg >/dev/null 2>&1
then
sudo yum install -y $pkg
ck_ok "yum 安装$pkg"
else
echo "$pkg已经安装"
fi
done
fi
# 初始化MySQL数据目录
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
"MySQL初始化成功"
# 启动MySQL服务
cd /etc/init.d/
if [ -f mysqld ]
then
echo "已存在"
else
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
echo "复制成功"
fi
/etc/init.d/mysqld restart
systemctl enable mysqld
ck_ok "启动MySQL"
# 获取初始密码
initial_password=$(grep 'temporary password' /usr/local/mysql/data/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 '123456';"
echo "修改MySQL初始密码为:123456"
# 重启MySQL服务
pkill -9 mysqld
service mysql start
echo "安装完成,请您放心使用,登录密码为:123456"
}
download_ng
install_ng
source ~/.bashrc