#!/bin/bash
# 配置mysql yum源
wget https: //dev .mysql.com /get/mysql57-community-release-el7-7 .noarch.rpm
rpm -ivh mysql57-community-release-el7-7.noarch.rpm
# 更新GPG验证密钥,并安装mysql,如果不更新密钥,安装则会失败
# 如果不更新最后两行可能报以下错误
# Failing package is: mysql-community-client-5.7.38-1.el7.x86_64
# GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
rpm -- import https: //repo .mysql.com /RPM-GPG-KEY-mysql-2022
yum install mysql-server -y
# 启动mysql
echo '正在启动MySQL,请稍等......'
systemctl start mysqld.service
if [ $? - ne 0 ]; then
echo 'MySQL启动失败!!!'
exit
else
echo 'MySQL启动成功!!!'
echo '===================================='
fi
# 获取初始密码
initpasswd=` cat /var/log/mysqld .log | grep password | awk '{print $NF}' `
echo "初始密码为:${initpasswd}"
# 用户设置密码
flag=1
while [ $flag - eq 1 ]
do
read -p "请输入新密码:" newpasswd
read -p "请确认密码:" secondpasswd
if [ $newpasswd - eq $secondpasswd ]
then
flag=2
else
echo "两次密码不一致,请重新输入"
echo "===================================="
fi
done
echo "设置的新密码为:${newpasswd}"
mysql --connect-expired-password -uroot -p "${initpasswd}" -e "set global validate_password_policy=0;set global validate_password_length=1;alter user 'root'@'localhost' identified by '${newpasswd}';"
if [ $? - ne 0 ]; then
echo '新密码设置失败!!!'
exit
else
echo '===================================='
echo "新密码设置成功!,新密码为:${newpasswd}"
echo '===================================='
fi
echo "正在开启远程登录......"
mysql --connect-expired-password -uroot -p "${newpasswd}" -e "update mysql.user set Host = '%' where Host = 'localhost' and User='root';flush privileges;"
if [ $? - ne 0 ]; then
echo '远程登录开启失败!!!'
exit
else
echo '===================================='
echo '远程登录开启成功!'
echo '===================================='
fi
|