#!/bin/bash
##############################################################
# File Name: mysql_install.sh
# Time: 2020-06-07-21:25:55
# Author: whb
# Organization: www.wanhebin.com
# Description: binary installation of mysql-5.6
##############################################################

#下载二进制包
[ -f /tmp/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz ] && rm -rf /tmp/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
wget -O /tmp/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz

#安装依赖
yum -y install  gcc gcc-c++ cmake ncurses-devel libaio-devel autoconf openssl openssl-devel

#解压
[ -d /tmp/mysql-5.6.46-linux-glibc2.12-x86_64 ] && rm -rf /tmp/mysql-5.6.46-linux-glibc2.12-x86_64
tar xf /tmp/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz -C /tmp/

#移动目录 
[ -d /usr/local/mysql-5.6.46 ] && rm -rf /usr/local/mysql-5.6.46
mv /tmp/mysql-5.6.46-linux-glibc2.12-x86_64 /usr/local/mysql-5.6.46

#创建软连接
rm -rf /usr/local/mysql && ln -s /usr/local/mysql-5.6.46 /usr/local/mysql

#创建用户
useradd -M -s /sbin/nologin mysql 2> /dev/null

#拷贝配置文件、启动脚本
\cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
\cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

#初始化数据库
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/

#配置mysql服务的unit文件
cat>/usr/lib/systemd/system/mysqld.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF

#重载系统中的unit文件
systemctl daemon-reload

#启动、自启mysql服务
systemctl start mysqld.service
systemctl enable mysqld.service

#配置mysql命令的环境变量
echo "export PATH=/usr/local/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh
source /etc/profile