jianshu文章测试
2017-11-16 本文已影响0人
PandaV587
测试
#!/bin/bash
printfHead(){
clear
printf "***********************************************************\n\n"
printf "\t\tCmPanda MySQL InstallScript\n"
printf "\t\t Centos 6.X x64\n\n"
printf "***********************************************************\n\n"
}
printfFoot(){
printf "***********************************************************\n\n"
printf "\t\tEnd Of Script - CmPanda\n\n"
printf "***********************************************************\n\n"
}
checkYum(){
printf "Checking yum configured ...... "
yum list *mysql* >& /dev/null
if [ $? -eq 0 ]
then
yumRes=$(yum list *mysql*)
if [[ "$yumRes" =~ .*mysql.*\..*64.* ]] && [[ "$yumRes" =~ .*mysql.*server.*\..*64.* ]]
then
echo -e "\033[32mSuccess\033[0m\nStart update yum"
yum -y update all
checkMySQL
else
echo -e "\033[31mFail\033[0m"
installAliYum
fi
else
echo -e "\033[31mFail\033[0m"
installAliYum
fi
}
installAliYum(){
printf "Check wget com ...... "
wget --help >& /dev/null
if [ $? -ne 0 ]
then
echo -e "\033[31mFail\nNot find wget com\n\033[0m"
printfFoot
exit 0
fi
echo -e "\033[32mSuccess\033[0m"
while true
do
printf "Whether to install aliMirrors (Y/N)? "
read res
if [ "$res" = "Y" -o "$res" = "y" ]
then
break
elif [ "$res" = "N" -o "$res" = "n" ]
then
printf "\n"
printfFoot
exit 0
fi
done
echo -ne "Download aliyun yum.repos ...... "
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo >& /dev/null
echo -ne "\033[32mSuccess\033[0m\nClean yum ...... "
yum clean all > /dev/null
echo -e "\033[32mSuccess\033[0m\nMakeCache yum"
yum makecache
echo -e "Yum Update Done"
checkYum
}
checkMySQL(){
echo "正在判断是否已安装 MySQL"
mysqlres=$(yum list installed *mysql*)
if [[ "$mysqlres" =~ .*mysql.*\..* ]]
then
echo -e "\033[33m检测到有MySQL相关组件\033[0m"
while true
do
printf "是否清除 (Y/N)? "
read res
if [ "$res" = "Y" -o "$res" = "y" ]
then
printf "Start remove MySQL ...... "
removeMySQL
break
elif [ "$res" = "N" -o "$res" = "n" ]
then
echo
printfFoot
break
fi
done
else
printf "未检测到MySQL相关组件\n"
installMySQL
fi
}
removeMySQL(){
#yum remove *mysql* > /dev/null
yum remove -y *mysql* >& /dev/null
echo -e "\033[32mSuccess\033[0m"
installMySQL
}
installMySQL(){
printf "Start Install MySQL\n"
yum install -y mysql*server*x86_64
printf "安装完成,启动MySQL服务 ...... "
service mysqld restart >& /dev/null
echo -e "\033[32mSuccess\033[0m"
printf "设置MySQL Root密码: "
read pwd
printf "开始配置MySQL\n修改MySQL密码 ...... "
mysqladmin -u root password $pwd
service mysqld restart >& /dev/null
echo -e "\033[32mSuccess\033[0m"
while true
do
printf "是否配置MySQL远程访问 (Y/N)? "
read res
if [ "$res" = "Y" -o "$res" = "y" ]
then
printf "配置MySQL远程访问 ...... "
cm1="grant all privileges on *.* to 'root'@'%' identified by '"
cm2="' with grant option;"
cm=$cm1${pwd}$cm2
mysql -h127.0.0.1 -uroot -p$pwd --port=3306 <<EOF
use mysql;
$cm
FLUSH PRIVILEGES;
EOF
echo -e "\033[32mSuccess\033[0m"
break
elif [ "$res" = "N" -o "$res" = "n" ]
then
break
fi
done
while true
do
printf "是否配置防火墙开启3306端口 (Y/N)? "
read res
if [ "$res" = "Y" -o "$res" = "y" ]
then
printf "配置防火墙开启3306端口 ...... "
sed -i '/.*22.*/a\-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT' /etc/sysconfig/iptables
echo -ne "\033[32mSuccess\033[0m\n重启防火墙 ...... "
service iptables restart >& /dev/null
echo -e "\033[32mSuccess\033[0m"
break
elif [ "$res" = "N" -o "$res" = "n" ]
then
break
fi
done
while true
do
printf "是否配置MySQL开机自启 (Y/N)? "
read res
if [ "$res" = "Y" -o "$res" = "y" ]
then
printf "配置MySQL开机自启 ...... "
chkconfig mysqld on
echo -e "\033[32mSuccess\033[0m"
break
elif [ "$res" = "N" -o "$res" = "n" ]
then
break
fi
done
printf "MySQL安装配置成功\n\n"
printfFoot
}
printfHead
checkYum