#/bin/bash
DIR=/usr/local/src
NAME="mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz"
FILE=$(echo $NAME|sed -r 's/(.*).tar.gz/\1/')
FULL_NAME=${DIR}/${NAME}
DATA_DIR="/data/mysql"
LINK="https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz"
. /etc/init.d/functions
echo -e "\e[1;31m 下载依赖\e[0m"
yum -y install autoconf libaio-devel net-tools vim wget gcc gcc-c++ lrzsz &>/dev/null
if [ -h /usr/local/mysql ];then
action "mysql已经安装" false
exit 3
elif [ ! -f $FULL_NAME ];then
cd $DIR
echo -e "\e[1;31m下载软件包...\e[0m"
wget $LINK
echo -e "\e[1;31m解压中...\e[0m"
tar xf $FULL_NAME -C /usr/local/src &>/dev/null
else
echo -e "\e[1;31m解压中...\e[0m"
tar xf $FULL_NAME -C /usr/local/src &>/dev/null
ln -s /usr/local/src/$FILE /usr/local/mysql
fi
echo -e "\e[1;31m安装中...\e[0m"
#创建用户mysql
id mysql &>/dev/null && usermod -u306 -md /data/mysql -s /sbin/nologin mysql &>/dev/null || useradd -r -u306 -d /data/mysql -s /sbin/nologin mysql
chown -R mysql.mysql /usr/local/mysql/*
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
cat > /etc/my.cnf <
[client]
socket=/data/mysql/mysql.sock
[mysql]
prompt=(\\u@\\h \\p) [\\d]>\\_
[mysqld]
user=mysql
datadir=/data/mysql
basedir=/usr/local/mysql
port=3306
socket=/data/mysql/mysql.sock
symbolic-links=0
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
EOF
cat >> /etc/profile.d/path.sh <
#mysql
export PATH=/usr/local/mysql/bin:\$PATH
EOF
. /etc/profile.d/path.sh
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql &>/dev/null
cat >/lib/systemd/system/mysqld.service <
[Unit]
Description=mysql database server
Documentation=man:mysqld(8)
Documentation=https://mariadb.com/kb/en/library/systemd/
After=network.target
[Install]
WantedBy=multi-user.target
Alias=mysqld.service
[Service]
Type=notify
PrivateNetwork=false
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/rc.d/init.d/mysqld start
ExecStop=/etc/rc.d/init.d/mysqld stop
ExecReload=/etc/rc.d/init.d/mysqld reload
EOF
action "安装完成"
systemctl enable mysqld &>/dev/null
systemctl restart mysqld && ss -lnt|awk -F":| +" '/3306/{print $5}' && action "启动成功" || action "失败" false
#安全加固初始化 设置root密码123456 不允许远程登录
#echo -e "\ny\n123456\n123456\ny\ny\ny\ny\n"|/usr/local/mysql/bin/mysql_secure_installation &>/dev/null