#!/bin/bash

color_red="echo -e \033[31m"

color_green="echo -e \033[32m"

color_yellow="echo -e \033[33m"

colorEnd="\033[0m"

softPath="application"

#mysql 密码

mysqlPassword='qwe123QWE!'

#执行文件所在目录

oldpath=$(pwd)

###############################################################################################

basePath="base"

applicationPath="application"

#linux 防火墙关闭操作

function stopfirewalld(){

 #禁用(disable) libvirtd 服务

 systemctl disable libvirtd

systemctl status firewalld | grep "running" &>/dev/null

if [ $? -eq 0 ]

then

 echo "防火墙处于开启状态,正在进行关闭操作..."

 systemctl stop firewalld &>/dev/null

 #再次检查防火墙状态

 systemctl status firewalld | grep "running" &>/dev/null

 if [ $? -ne 0 ]; then

  $color_green 防火墙已经处于关闭状态,关闭操作成功!$colorEnd

  systemctl disable firewalld.service &>/dev/null]

 else

  $color_red 防火墙已经处于开启状态,关闭操作失败,请手动操作!$colorEnd

 fi

else

 echo -e "防火墙已经处于关闭状态,无需进行关闭操作"

fi

}


#关闭SeLinux 子系统

stopSelinux(){

#判断当前的状态

result=`getenforce`

echo "selinux判断是否开启状态,如果开启则进行关闭操作!"

sleep 2

if [ $result = Enforcing ]; then

 #临时关闭操作

 setenforce 0 &>/dev/null

 #永久关闭操作

 find_key="SELINUX="

 sed -ri "/^$find_key/c${find_key}disabled" /etc/selinux/config

 result_1=`getenforce`

 if [ $result_1 = Enforing ]

 then

  $color_red selinux关闭操作失败,请手台进行操作!$colorEnd

 else

  $color_green selinux关闭成功!$colorEnd

 fi

else

 echo -e "selinux是关闭状态,无需操作!"

fi

}


#安装Mysql

installMysql(){

if rpm -qa | grep mysql-community-server;then

 read -p "mysql 已安装,是否继续安装覆盖,继续操作(y),跳过(任意键): "  operate

 if [[ $operate != 'y' ]];then

  return

 fi

 systemctl stop mysqld

 #卸载mysql

 rpm -aq | grep -i mysql >rmsql.sh

 sed -i -e 's/^/yum remove -y /' rmsql.sh

 chmod +777 rmsql.sh

 sh rmsql.sh

 rm -rf rmsql.sh

 rm -rf /var/lib/mysql

 rm /var/log/mysqld.log

fi

echo '>>>>>>>>>>>>>>>>>>>>>>>>>>解压Mysql压缩包>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'

cd ${basePath}

mkdir -p /home/install

cp  mysql-*.rpm-bundle.tar /home/install

cd /home/install

tar -xvf mysql-*.rpm-bundle.tar


echo '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>安装Mysql>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'

rpm -ivh mysql-community-common-8.0.30-1.el8.x86_64.rpm

rpm -ivh mysql-community-client-plugins-8.0.30-1.el8.x86_64.rpm

rpm -ivh mysql-community-libs-8.0.30-1.el8.x86_64.rpm

rpm -ivh mysql-community-client-8.0.30-1.el8.x86_64.rpm

rpm -ivh mysql-community-icu-data-files-8.0.30-1.el8.x86_64.rpm

rpm -ivh mysql-community-server-8.0.30-1.el8.x86_64.rpm

mysqld --initialize


chown -R mysql:mysql /var/lib/mysql

if rpm -qa | grep mysql-community-server;then

 systemctl start mysqld.service

 systemctl enable mysqld

 proc=`ps -ef | grep mysql | grep -v 'grep' | wc -l`

 if [ $proc -ge 1 ];then

  $color_green mysql 启动成功 $colorEnd

 else

  $color_red mysql 启动失败 $colorEnd

  exit 1

 fi

else

 $color_red mysql 安装失败 $colorEnd

 exit 1

fi



echo '>>>>>>>>>>>>>>>>>>>>>配置mysql用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'

inintPassword=`grep "password is generated" /var/log/mysqld.log | awk '{print $NF}'`

mysqladmin -uroot -p$inintPassword password $mysqlPassword

mysql -uroot -p$mysqlPassword -e "use mysql;ALTER USER 'root'@'localhost' IDENTIFIED BY '${mysqlPassword}';flush privileges;create user 'admin'@'%' identified by '${mysqlPassword}';GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%';ALTER USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY '${mysqlPassword}';flush privileges;" --connect-expired-password

cd ${oldpath}

}


#备份和添加工具文件

startBackupsAndAddTools(){

#备份数据库文件

mkdir -p /opt/.backup/script/

cp -r config/mysql/sql/. /opt/.backup/script/

}


#基础应用安装

function baseInstall(){

$color_green 基础服务安装开始...$colorEnd

#关闭防火墙

stopfirewalld

#安装mysql

installMysql

startBackupsAndAddTools

$color_green 基础服务安装结束!$colorEnd

}

###########################执行函数################################

baseInstall