mysql安装-centos7

执行脚本:

# 语法:bash $0 $password
[root@localhost ~]# bash mysql_5.7.30_install.sh 123456

cat mysql_5.7.30_install.sh

#!/bin/bash
if [ -e $1 ]; then
    green "Usage: bash $0 password"
	exit
fi

pass=$1
var=\'

function green(){
    echo -e "\033[34m$1\033[0m"
}

green "--------start unzip software......"
unzip mysql-5.7.30-el7.zip -d mysql_soft

if rpm -q mariadb-libs &>/dev/null; then
	green "--------start remove mariadb-libs......"
	rpm -e mariadb-libs --nodeps
fi

green "--------start install mysql-server......"
rpm -ivh mysql_soft/*.rpm

if type mysqld &>/dev/null; then
	green "--------start config......"
else
	echo "software install fail."
	exit
fi

mysqld --initialize --console

chown -R mysql.mysql /var/lib/mysql /etc/my.cnf

tmp_pass=$(cat /var/log/mysqld.log | grep 'temporary password' | awk '{print $NF}')

cat >> /etc/my.cnf << EOF
[mysql]
password=${tmp_pass}
EOF

cat >> tmp.sql << EOF
alter user root@localhost identified by $var$pass$var;
flush privileges;
EOF
green "--------start mysqld......"
systemctl start mysqld

if [ $? -eq 0 ]; then
	green "--------start modify password......"
	mysql -u root --connect-expired-password < tmp.sql
	if [ $? -ne 0 ]; then
		echo "exec sql fail."
		exit
	fi
else
	echo "mysqld start fail."
	exit
fi

green "--------start remove temp file......"
sed -i '/\[mysql\]/,$d' /etc/my.cnf
rm -rf mysql_soft mysql-5.7.30-el7.zip tmp.sql
green "--------install complete."
posted @ 2021-01-04 21:44  那就这样吧~  阅读(71)  评论(0编辑  收藏  举报