#!/bin/bash

#Function:用于安装mysql-5.7.16二进制分发版本,脚本共有2个文件,一个脚本执行文件installMysql.sh,

#另一个mysql配置文件:my.cnf,注意要将该配置文件放置于/data目录下

#使用该脚本安装mysql需要能连接外网以便能从mysql官网下载mysql二进制安装包

#或者将事先下载好的安装包放置于/data/mysqlSoftware目录下,将download 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

tar -xzf mysql*.tar.gz 

rm -rf $installDir/mysql*.tar.gz

mv $installDir/mysql* mysql

#新建mysql用户、组及目录

groupadd mysql

useradd -r -s /sbin/nologin -g mysql mysql -d $installDir/mysql

#更改目录属主

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 .

#配置参数

cd $installDir/mysql

bin/mysqld --initialize-insecure --user=mysql --basedir=$installDir/mysql --datadir=$databaseDir

bin/mysql_ssl_rsa_setup --datadir=$databaseDir

#修改系统配置文件

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

cd $installDir/mysql

bin/mysqld_safe --user=mysql &

sleep 10 

echo

#修改root密码

bin/mysqladmin -u root password ''"$mypasswd"''

#添加系统路径

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

source ~/.bashrc

#配置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

whereis mysql | xargs rm -rf 2>/dev/null

find / -iname 'mysql' -print0 | xargs -0 rm -rf

rm -rf /etc/my.cnf 2>/dev/null

userdel mysql 2>/dev/null

groupdel mysql 2>/dev/null

chkconfig --del mysql 2>/dev/null

rm -rf /root/.my.cnf 2>/dev/null

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

}


if [ ! $UID -eq 0 ]; then 

  echo "You must be root to run this script!" 

  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

  removeMysql

  exit 0

elif [ $choice = "q" ]; then

  echo "Give up installation,exit!"

  exit 0

else

  echo "Wrong choice,exit!"

  exit 11

fi