You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shell-project/database/yum-mysql.sh

81 lines
2.4 KiB

#!/usr/bin/bash
default_password="QianFeng@123"
default_bakdir=/var/lib/mysql.bak
checkos(){
[[ -d /var/lib/mysql ]] && \
echo "当前环境已安装mysql"
#exit 1
ping -W1 -c1 www.baidu.com &>/dev/null || \
echo "网络异常,请检查" || exit 1
[[ $UID -ne 0 ]] && echo "请使用root用户执行" && exit 1
}
uninstall() {
pkill -9 mysql &>/dev/null
mkdir -p ${default_bakdir}/`date +"%Y-%m-%d"`/$1 &>/dev/null
mv /etc/my.cnf ${default_bakdir}/`date +"%Y-%m-%d"`/$1 &>/dev/null
mv ~/.mysql_history ${default_bakdir}/`date +"%Y-%m-%d"`/$1 &>/dev/null
mv /var/lib/mysql* ${default_bakdir}/`date +"%Y-%m-%d"`/$1 &>/dev/null
yum erase -y `rpm -qa |grep mysql` && \
rm -rf /var/log/mysqld.log
echo "mysql已卸载,历史数据备份在${default_bakdir}/`date +"%Y-%m-%d"`"
}
install(){
yum install -y yum-utils https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm || exit 2
yum-config-manager --disable mysql{80,55,56,57}-community &>/dev/null
case $1 in
5.7)
yum-config-manager --enable mysql57-community
;;
5.5)
yum-config-manager --enable mysql55-community
;;
5.6)
yum-config-manager --enable mysql56-community
;;
8.0)
yum-config-manager --enable mysql80-community
;;
*)
echo "无效版本"
exit 2
esac
yum install -y mysql-community-server && \
echo "mysql 安装完成,即将开始启动"
}
init(){
systemctl start mysqld && \
systemctl enable mysqld
password=`grep "temporary password" /var/log/mysqld.log |awk '{password=$NF}END{print password}'`
cat <<EOF
数据库目录安装位置 /var/lib/mysql
pid 文件位置 /var/run/mysqld/mysqld.pid
错误日志位置 /var/log/mysqld.log
socket文件位置 /var/lib/mysql/mysql.sock
初始密码为 $password # mysql 5.5 5.6版本无初始密码
最新密码为 "$default_password" #如果无法验证,请使用初始密码
历史数据备份目录 ${default_bakdir}
EOF
password=${password:+-p\"$password\"}
mysqladmin -uroot $password password "$default_password"
}
case $1 in
Remove)
uninstall
;;
Install)
level=$2
level=${level:=5.7}
checkos
uninstall $level
install $level
init $level
;;
*)
echo -e "\033[31mUsage: \nsh $0 [Remove|Install]\nsh $0 Install [5.5|5.6|5.7|8.0] default 5.7\033[0m"
esac