安装脚本

#!/bin/bash  
  
# if /usr/local/mysql  exists  
d=`date +%Y%m%d`  
p1=$(pwd)
if [ -e /usr/local/mysql ]  
    then  
    #mv /usr/local/mysql mysql'$d'.bak
    #删除原有mysql文件夹  
    rm -rf /usr/local/mysql*  
fi
  
# 创建 /opt/mysql 文件夹
if [ -d "${p1}"/mysql ]  
    then  
    echo "mysql is exists!"         
    mkdir -p /opt/mysql
else
    mkdir -p /opt/mysql
fi  
  
# 解压缩mysql,这里使用的是5.7.17版本
if [ -f ./mysql*tar.gz ]  
   then  
     cd /opt/mysql/  
     tar -zxvf ./mysql*tar.gz
     #echo $mysqlname
     mysqlname=`ls`
     mv $mysqlname /opt/mysql/mysql-5.7.17
   else  
      echo "the mysql install package is not exists"  
fi  
 
cd /usr/local/  
# 创建软连接
ln -s /opt/mysql/mysql-5.7.17 /usr/local/mysql  
    
# create mysql user  
user=mysql  
group=mysql  
  
# 判断是否存在mysql组别 create group if not exists    
egrep "^$group" /etc/group >& /dev/null  
if [ $? -ne 0 ]  
then  
    groupadd $group  
    echo "$group create successful!!"  
else  
    echo "$group  group is exists!"       
fi  
  
# 判断是否存在mysql用户 create user if not exists    
egrep "^$user" /etc/passwd >& /dev/null  
if [ $? -ne 0 ]  
then  
    useradd -M -g $group -s /sbin/nologin -d /usr/local/mysql $user  
    echo "$user create successful!!"  
    mysqlpath='export PATH=/usr/local/mysql/bin:$PATH'  
    echo $mysqlpath  >> /root/.bash_profile  
    source /root/.bash_profile  
else  
    echo "$user user is exists!"  
fi  
  
#create dirctory  
#d=`date +%Y%m%d`  
if [ -d /data/mysql ]  
then  
    echo "data is exists"  
    cd  /data  
    mv  mysql mysql"$d".bak  
    mkdir -p /data/mysql  
    mkdir -p /data/mysql/mysql_3306  
    cd /data/mysql/mysql_3306/  
    mkdir data  
    mkdir logs  
    mkdir tmp  
    chown -R mysql:mysql /data/mysql/mysql_3306  
else  
    mkdir -p /data/mysql  
    mkdir -p /data/mysql/mysql_3306  
    cd /data/mysql/mysql_3306/  
    mkdir data  
    mkdir logs  
    mkdir tmp  
    chown -R mysql:mysql /data/mysql/mysql_3306  
  
fi  
  
#my.cnf  
  
cd /usr/local/mysql/  
./bin/mysqld --initialize  
  
  
if [ -f /usr/local/mysql/support-files/mysql.server ]  
   then  
   rm -f /etc/init.d/mysql/mysql.server  
   cp  /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql  
else  
   cp  /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql  
  
fi  
  
if [ -f /usr/local/mysql/support-files/mysql.server ]  
   then  
    service mysql restart  
    echo "mysql start successful"  
else  
    echo "mysql start failed"  
fi  
  
# 打印初始化密码 print password  
  
p1=`sed -n '/temporary password/p'  /data/mysql/mysql_3306/data/error.log|awk -F ':' '{print $4}'`  
echo "your password is:$p1" 

卸载脚本


# return good tip,正确提示
function gtip() {
  echo -e "\033[32m${1}\033[0m"
}

# return error tip,错误提示
function etip() {
  echo -e "\033[31m${1}\033[0m"
}

#delete mysql
function uninstallMysql() {
    gtip "Check whether exist mysql service"
    ps aux | grep mysql 
    # 停止mysql服务
    service mysql stop
    gtip "[delete dirs ... ]"
    # 删除mysql相关文件夹和文件
    if [[ -e /usr/bin/mysql ]] || [[ -d  /usr/bin/mysql ]];then
        gtip "[delete  /usr/bin/mysql]";
	rm -rf /usr/local/mysql;
    fi
    if [[ -e /usr/local/mysql ]] || [[ -d /usr/local/mysql ]];then
        gtip "[delete  /usr/local/mysql]";
	rm -rf /usr/local/mysql;
    fi
    if [[ -e /data/mysql ]] || [[ -d /data/mysql ]];then
        gtip "[delete /data/mysql]";
        rm -rf /data/mysql;
    fi
    if [[ -e /var/spool/mail/mysql ]] || [[ -d /var/spool/mail/mysql ]];then
        gtip "[delete /var/spool/mail/mysql]";
        rm -rf /var/spool/mail/mysql;
    fi
    if [[ -e /var/lib/mysql ]] || [[ -d /var/lib/mysql ]];then
        gtip "[delete /var/lib/mysql]";
        rm -rf /var/lib/mysql;
    fi
    if [[ -e /etc/init.d/mysql ]] || [[ -d /etc/init.d/mysql ]];then
        gtip "[delete /etc/init.d/mysql]";
        rm -rf /etc/init.d/mysql;
    fi
    if [[ -e /opt/mysql ]] || [[ -d /opt/mysql ]];then
        gtip "[delete /opt/mysql]";
        rm -rf /opt/mysql;
    fi
    gtip "[delete user mysql]";
    # 删除mysql用户
    if [ $(id -u mysql) > /dev/null 2>&1 ];then
        echo "delete user : mysql";
        userdel mysql;         
    fi
    gtip "[uninstall finish...]";
}

uninstallMysql;

Q.E.D.


每一个平凡的日常都是连续的奇迹