#!/bin/bash

#设定mysql目录，端口
echo "---------------------欢迎使用mysql-5.7.33一键部署脚本-------------------------"
sleep 2

dir(){
echo "请输入安装路径,格式:/\$dir/\$dir"
while read my_dir;do
  if [ "$my_dir" = "" ];then
    echo "!!错误,输入请重新输入!!"
  else
    echo "安装路径设置完成"
    sleep 2
    break
  fi
done
}

port(){
echo "请输入安装端口"
while read my_port;do
  n1=`echo "$my_port" | sed s/[0-9]//g`
  port_no=`netstat -antp | awk -F" " '{print $4}' | grep -w $my_port | wc -l`
  if [ "$my_port" = "" ];then
    echo "!!错误,请重新输入!!"
  elif [ ! -z "$n1" ];then
    echo "!!输入的端口号值不符合要求,请重新输入!!" 
  elif [ "$my_port" -lt 1 ] || [ "$my_port" -gt 65535 ];then
    echo "!!输入的端口号超出范围,请重新输入!!"
  elif [ "$port_no" -gt 0 ];then
    echo "!!端口已被占用,请重新输入!!"
  else
    echo "端口设置完毕"
    sleep 2
    break
  fi
done
}

#建立用户、安装目录
user_dir(){
echo "##########################开始建立用户、安装目录##############################"
sleep 2
my_name=`awk -F":" '{ print $1 }' /etc/passwd | grep -wi mysql`
if [  -z "$my_name" ];then
  useradd -s /sbin/nologin mysql 
  sleep 2
  echo "mysql管理用户已设置完成:mysql"
else
  echo "mysql管理用户已存在,继续安装"
fi
if [ -d "$my_dir" ];then
  if [ "`ls $my_dir`" != "" ];then
    echo "!!目录下存在文件,请检查!!"
    sleep 2
    exit 10
  else
    mkdir -p $my_dir/{mysql_install,data,conf,logs,tmp}
    echo "目录建立完成"
    sleep 2
  fi
else
  echo "开始建立目录"
  mkdir -p $my_dir/{mysql_install,data,conf,logs,tmp}
  echo "目录建立完成"
  sleep 2
fi
}


#下载依赖环境
envir(){
echo "############################开始下载编译安装必要程序##########################"
sleep 2
ping -c3 baidu.com &>/dev/null
if [ $? -eq 0 ];then
  echo "网络连通正常"
  sleep 2
else 
  echo "!!网络连通异常,请检查!!"
  sleep 2
  exit 10
fi
if [  -f -a "/etc/yum.repos.d/*.repo" ];then
  echo "yum源存在"
  yum -y install epel-release cmake bison ncurses-devel openssl-devel
  yum -y groupinstall "Development Tools"
  sleep 2
  echo "依赖插件安装完毕"
else
  echo "!!yum源不存在,请检查!!"
  sleep 2
  exit
fi
}

#开始编译、安装
inst(){
echo "###############################开始进行编译###################################"
sleep 2
make clean
cmake -DDEFAULT_CHARSET=utf8 -DDEFAULT_CHARSET=utf8 -DCMAKE_INSTALL_PREFIX=$my_dir/mysql_install -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost
if [ $? -eq 0 ];then
  echo "编译成功,继续安装"
else
  echo "!编译失败,请检查!"
  sleep 2
  exit
fi
echo "###############################开始进行安装###################################"
make  && make install
if [ $? -eq 0 ];then
  echo "安装成功"
else
  echo "!!安装失败,请检查!!"
  exit 10
fi
}

#mysql配置文件
conf(){
echo "###############################开始配置文件###################################"
sleep 2
cat >$my_dir/conf/mysql.cnf <<EOF
[mysqld]
port = $my_port
user = mysql
basedir = $my_dir/mysql_install
datadir = $my_dir/data
pid-file = $my_dir/mysql_install/mysql.pid
log_error = $my_dir/logs/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = $my_dir/logs/mysql-slow.log

# session memory configure.
read_buffer_size = 8M
read_rnd_buffer_size = 4M
sort_buffer_size = 4M
tmp_table_size = 32M
max_heap_table_size = 32M
join_buffer_size = 4M
thread_cache_size = 64

#innodb engine configure.
innodb_file_per_table = 1
innodb_buffer_pool_size = 5G
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_log_file_size = 2G
innodb_log_buffer_size = 32M
innodb_sort_buffer_size = 64M
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_online_alter_log_max_size = 4G
innodb_open_files = 65535
EOF

#设置属主属组
chown -R mysql:mysql $my_dir

echo "配置文件完毕"
sleep 2
}

myinit(){
echo "###############################开始配置初始化################################"
sleep 2
#mysql初始化
$my_dir/mysql_install/bin/mysqld --defaults-file=$my_dir/conf/mysql.cnf --initialize-insecure
if [ $? -eq 0 ];then
  echo "配置初始化成功"
  sleep 2
else
  echo "!!配置初始化失败,请检查!!"
  sleep 2
  exit
fi

#配置环境变量
if [  -z "`grep -w "export PATH=$my_dir/mysql_install/bin:\$\PATH" /etc/profile`" ];then
  echo "export PATH=$my_dir/mysql_install/bin:\$PATH" >> /etc/profile
  source /etc/profile
else
  :
fi

#配置使用systemctl控制
cat >/usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
ExecStart=$my_dir/mysql_install/bin/mysqld --defaults-file=$my_dir/conf/mysql.cnf
EOF

systemctl daemon-reload
}

dir
port
user_dir
envir
inst
conf
myinit


echo "########################安装完毕,请使用systemctl启动mysql###################"
sleep 2
