mysql最新通用二进制分发版本5.7.16自动安装shell脚本

#!/bin/bash
#Function:用于安装mysql-5.7.16二进制分发版本,脚本共有2个文件,一个脚本执行文件installMysql.sh,
#另一个mysql配置文件:my.cnf,注意要将该配置文件放置于/data目录下,当然你
#也可以自己重新定义这些目录,但一定要与my.cnf配置文件中相一致,否则安装失败
#使用该脚本安装mysql需要能连接外网以便能从mysql官网下载mysql二进制安装包
#最好将事先下载好的安装包放置于/data/mysqlSoftware目录下,将download mysql部分代码注释掉即可
#Note:如果选择卸载MYSQL,则需要手动删除一些文件.
#Date:2016/10/8
#Author:Jian
#Company:Jian
#Version:1.0
#Usage:bash installMysql.sh
 
#mysql install function
installMysql() {
#约定的相关目录和文件设置
local configFile=/data/my.cnf            #mysql的配置文件my.cnf
local downloadDir=/data/mysqlSoftware   #安装文件下载目录
local installDir=/usr/local            #mysql目录安装位置
local databaseDir=/data/mysql/data    #数据库保存位置
local logDir=/data/mysql/log         #日志保存位置
local tmpDir=/data/mysql/tmp         #tmp目录位置
local bit=$(getconf LONG_BIT)        #本系统是32还是64位?
local mypasswd="123456"              #设置mysql密码
local oldPath=$(grep -v "#" ~/.bashrc | gawk '$0 ~ /(PATH=)/{print $0}')
local newPath="export PATH=$installDir/mysql/bin:$PATH"
#mysql-community-server-5.7.16-x86_64
local url1="http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz"
#mysql-community-server-5.7.16-x86_32
local url2="http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-i686.tar.gz"
mkdir -p $logDir
mkdir -p $downloadDir
mkdir -p $databaseDir
mkdir -p $tmpDir
cd $downloadDir
#如果/data/mysqlSoftware目录下已有下载好的mysql二进制安装包,则下面#download mysql之间的代码可注释掉
#否则需要从官网下载mysql二进制包,由于安装包在600M左右,所以下载较为耗时,整个安装过程就需较长时间

#download mysql
#if [ $bit -eq 64 ];then
#  wget $url1
#elif [ $bit -eq 32 ];then
#  wget $url2
#fi
#download mysql

#解压压缩包到目标位置
ln -s $downloadDir/mysql*.tar.gz $installDir
cd $installDir
echo "Extracting MYSQL file..."
tar -xzf mysql*.tar.gz 
rm -rf $installDir/mysql*.tar.gz
mv $installDir/mysql* mysql
#新建mysql用户、组及目录
echo "Creating MySQL user name and group..."
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql -d $installDir/mysql
#更改目录属主
echo "Changing directory owner..."
cd $installDir/mysql
chown -R mysql .
chgrp -R mysql .
cd $logDir
chown -R mysql .
chgrp -R mysql .
cd $databaseDir
chown -R mysql .
chgrp -R mysql .
cd $tmpDir
chown -R mysql .
chgrp -R mysql .
#配置参数
echo "Configuration MYSQL..."
cd $installDir/mysql
bin/mysqld --initialize-insecure --user=mysql --basedir=$installDir/mysql --datadir=$databaseDir
bin/mysql_ssl_rsa_setup --datadir=$databaseDir
#修改系统配置文件
echo "Modifying system configuration file..."
cp $configFile /etc/my.cnf
cp $installDir/mysql/support-files/mysql.server /etc/init.d/mysql
sed -i 's!^basedir=$!basedir='"$installDir"'/mysql!' /etc/init.d/mysql>/dev/null
sed -i 's!^datadir=$!datadir='"$databaseDir"'!' /etc/init.d/mysql>/dev/null
#启动mysql
echo "Booting MYSQL..."
cd $installDir/mysql
bin/mysqld_safe --user=mysql &
sleep 10 
echo
echo "Modifying MYSQL root password..."
#修改root密码
bin/mysqladmin -u root password ''"$mypasswd"''
echo "Adding system path..."
#添加系统路径
if [ -z $oldPath ]; then
  echo "export PATH=$installDir/mysql/bin:$PATH" >> ~/.bashrc
else
  sed -i 's!'"$oldPath"'!'"$newPath"'!' ~/.bashrc
  sed -i '/^export PATH$/d' ~/.bashrc
fi
. ~/.bashrc
echo "Configuring Mysql to start automatically..."
#配置mysql自动启动
chmod 755 /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 345 mysql on
}

#移除mysql
removeMysql() {
/etc/init.d/mysql status 2>/dev/null
/etc/init.d/mysql stop 2>/dev/null
ps -ef | grep 'mysql' | grep -v 'grep'| awk '{print $2}' | xargs kill -9 2>/dev/null && echo -e "\e[;31mKilled MYSQL process... \e[0m"
echo -e "\e[;31mRemoved MYSQL user and group... \e[0m"
userdel mysql 2>/dev/null
groupdel mysql 2>/dev/null
chkconfig --del mysql 2>/dev/null
echo -e "\e[;31mRemoved MYSQL path... \e[0m"
sed -i '/\/usr\/local\/mysql\/bin:/s/\/usr\/local\/mysql\/bin://g' /root/.bashrc
sed -i '/\/usr\/local\/mysql\/bin:/s/\/usr\/local\/mysql\/bin://g' /home/book/.bashrc
. ~/.bashrc
echo "Have found the following MYSQL files, you need to manually delete them:"
find / \( -iname 'mysql' -o -iname "my.cnf" -o -iname ".my.cnf" \) -print
}


if [ ! $UID -eq 0 ]; then 
  echo -e "\e[;31mYou must be root to run this script! \e[0m" 
  exit 0
fi
read -p "Do you want to install or remove mysql?[i:install r:remove q:quit]:" choice
if [ $choice = "i" ]; then
  installMysql
  exit 0
elif [ $choice = "r" ]; then
  echo -e -n "\e[;31mWarning:are you sure you want to uninstall MYSQL completely and make sure you have a backup?[Y/N]: \e[0m"
  read choice0
  if [ "$choice0" = "Y" -o "$choice0" = "y" ];then
    removeMysql
    exit 0
  elif [ "$choice0" = "N" -o "$choice0" = "n" ];then
    echo "You choose to give up uninstall,exit!"
    exit 0
  else 
    echo -e "\e[;31mWrong choice,exit! \e[0m"
    exit 11
  fi
elif [ $choice = "q" ]; then
  echo "Give up installation,exit!"
  exit 0
else
  echo -e "\e[;31mWrong choice,exit! \e[0m"
  exit 11
fi


你可能感兴趣的:(Shell,Scripts)