复制运行即可,可以输入修改的数据库密码,若跳过设置,默认将密码修改为123456。
#!/bin/bash
echo "this script is for install mysql5.7-server and change passwd"
function yum_repo(){
cat >/etc/yum.repos.d/mysql-community.repo<<EOF
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7-\$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7-\$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-5.6-community]
name=MySQL 5.6 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-\$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-5.7-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-\$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-8.0-community]
name=MySQL 8.0 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-\$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
EOF
yum makecache fast
}
function install_mysql57(){
yum install -y mysql-server
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld
}
function get_new_passwd(){
while :
do
read -s -p $'Please input new mysql password : \n' passwd1
if [ x"$passwd1" = "x" ];then
echo 'use default passwd 123456'
newpasswd=123456
break
fi
read -s -p $'Please re input mysql password : \n' passwd2
if [ $passwd1 != $passwd2 ];then
echo "The password is inconsistent, please retry"
else
newpasswd=$passwd1
break
fi
done
}
function init_sql(){
get_new_passwd
cat >~/.init.sql<<EOF
use mysql;
update user set host = '%' where user = 'root';
ALTER USER 'root'@'%' IDENTIFIED BY '$newpasswd';
flush privileges;
EOF
}
function passwd_policy(){
cat >>/etc/my.cnf<<EOF
plugin-load=validate_password.so
validate-password=OFF
EOF
systemctl restart mysqld
}
function get_init_passwd(){
init_passwd=`grep 'temporary password' /var/log/mysqld.log | awk '{ print $11 }'`
}
function init_mysql(){
passwd_policy
get_init_passwd
/usr/bin/mysql -uroot -p$init_passwd < ~/.init.sql
echo "mysql initialization is completed new passwd is $newpaaswd"
}
function main(){
yum_repo
install_mysql57
if [ $? != 0 ];then
echo "download or install mysql failed"
exit 1
else
init_mysql
fi
}
touch ~/.install_mysql57.log
main | tee -a ~/.install_mysql57.log