#!/bin/bash
#
#********************************************************************
#Author: Wuvikr
#QQ: 744123155
#Date: 2020-12-01
#FileName Mysql5.7_src_install.sh
#URL: http://www.wuvikr.top
#Description The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
CPU=`lscpu | sed -rn 's/^CPU(s)[^0-9]+([0-9]+)/1/p'`
Rpcsvc_Package=rpcsvc-proto-1.4.2.tar.xz
Boost_Package=boost_1_59_0.tar.bz2
Mysql_Package=mysql-5.7.30.tar.gz
Rpcsvc_Dirname=${Rpcsvc_Package%.tar*}
Boost_Dirname=${Boost_Package%.tar*}
Mysql_Dirname=${Mysql_Package%.tar*}
Boost_Install_Dir=/usr/local
Mysql_Install_Dir=/apps/mysql
Mysql_Data_Dir=/data/mysql
# 编译安装rpcsvc
install_rpcsvc() {
[ -f $Rpcsvc_Package ] && tar xvf $Rpcsvc_Package &> /dev/null || echo -e "e[1;31m当前目录没有检测到${Rpcsvc_Package}包,请提前准备好安装包!e[0m"
cd $Rpcsvc_Dirname
./configure
make -j $CPU && make install
}
# 编译安装mysql
install_mysql() {
# 切换回上一级目录
cd ..
# 检测安装包
[ -f $Boost_Package ] && tar xvf $Boost_Package -C $Boost_Install_Dir &> /dev/null || echo -e "e[1;31m当前目录没有检测到${Boost_Package}包,请提前准备好安装包!e[0m"
[ -f $Mysql_Package ] && tar xvf $Mysql_Package &> /dev/null || echo -e "e[1;31m当前目录没有检测到${Mysql_Package}包,请提前准备好安装包!e[0m"
# 安装相关依赖包
yum -y install make cmake gcc gcc-c++ ncurses ncurses-devel bison openssl-devel
# 创建用户及用户组
groupadd -r mysql
useradd -r -g mysql -s /sbin/nologin mysql
#准备mysql目录
mkdir $Mysql_Data_Dir
chown mysql.mysql $Mysql_Data_Dir
# 编译安装
cd $Mysql_Dirname
cmake .
-DCMAKE_INSTALL_PREFIX=$Mysql_Install_Dir
-DMYSQL_DATADIR=$Mysql_Data_Dir
-DSYSCONFDIR=/etc
-DMYSQL_UNIX_ADDR=$Mysql_Data_Dir/mysqld.sock
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8mb4
-DDEFAULT_COLLATION=utf8mb4_unicode_ci
-DWITH_MYISAM_STORAGE_ENGINE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DENABLED_LOCAL_INFILE=1
-DENABLED_PROFILING=1
-DMYSQL_TCP_PORT=3306
-DWITH_DEBUG=0
-DWITH_BOOST=${Boost_Install_Dir}/${Boost_Dirname}
make -j $CPU && make install
}
# 配置和初始化Mysql
configuration() {
# 设置环境变量
echo "PATH=${Mysql_Install_Dir}/bin:$PATH" > /etc/profile.d/mysql.sh
# 准备配置文件
cat > /etc/my.cnf << EOF
[mysqld]
datadir=$Mysql_Data_Dir
skip_name_resolve=1
socket=${Mysql_Data_Dir}/mysql.sock
log-error=${Mysql_Data_Dir}/mysql.log
pid-file=${Mysql_Data_Dir}/mysql.pid
[client]
default-character-set=utf8mb4
socket=${Mysql_Data_Dir}/mysql.sock
[mysql]
default-character-set=utf8mb4
socket=${Mysql_Data_Dir}/mysql.sock
[mysql]
auto-rehash
prompt="\u@\h [\d]>"
EOF
# 初始化数据库
${Mysql_Install_Dir}/bin/mysqld --initialize-insecure --user=mysql --basedir=${Mysql_Install_Dir} --datadir=${Mysql_Data_Dir}
# 准备服务脚本
cp ${Mysql_Install_Dir}/support-files/mysql.server /etc/init.d/mysqld
# 启动mysql
chkconfig --add mysqld
service mysqld start
}
install_rpcsvc
install_mysql
configuration
echo -e "e[1;31m${Mysql_Dirname}安装成功!e[0m"