#!/bin/bash
#Author Mr.Wu
#初始化系统并安装mysql数据库
#2023年1月12日16:23:40
####################################################
#定义变量
mysql_log="$(pwd)/mysql_install.log"
repo="/etc/yum.repos.d/"
package="bzip2 gzip cmake gcc gcc-c++ bison ncurses ncurses-devel openssl openssl-devel"
mysql_rpm="$(pwd)/mysql-community-5.7.38-1.el7.src.rpm"
boost_bz="boost_1_59_0.tar.bz2"
mysql_gz="mysql-5.7.38.tar.gz"
mysql_src="/usr/local/src"
mysql_dir="/data/mysql"
mysql_usr="/usr/local/mysql"
init="/etc/init.d"
messages=("永久关闭firewalld" "永久关闭selinux" "永久关闭iptables" "配置阿里源和本地yum源" "设置镜像自动挂载" "安装依赖包" "下载解压并安装软件包" "预编译" "编译" "编译安装" "修改主配置文件" "启动mysql并设置开机自启")
#messages=("配置阿里源和本地yum源" "设置镜像自动挂载" "安装依赖包" "安装并解压软件包" "预编译" "编译" "编译安装" "修改主配置文件" "启动mysql并设置开机自启")
colour=(31 34)
result=("失败!!!" "成功!!!")
####################################################
#定义函数
delimiter(){
echo -e "\033[32;1m**************************\033[0m"
}
echoinfo(){
echo -e "\033[35;1m#$1\033[0m"
}
echoresult(){
echo -e "\033[$1;1m$2$3\033[0m"
}
if_test(){
if [ $? -eq 0 ];then
echoresult ${colour[1]} $1 ${result[1]}
else
echoresult ${colour[0]} $1 ${result[0]}
exit
fi
}
firewalld_off(){#关闭firewalld
delimiter | tee -a $mysql_log
echoinfo ${messages[0]} | tee -a $mysql_log
systemctl stop firewalld &>> $mysql_log
systemctl disable firewalld &>> $mysql_log
sleep 3
num1="$(systemctl status firewalld | expr length "`awk '/Active/{print $2}'`")"
if [ $num1 -eq 8 ];then
echo -e "\033[44;37;1m firewalld已经永久关闭!!!\033[0m" | tee -a $mysql_log
else
echo -e "\033[43;37;1m firewalld关闭失败!!!\033[0m" | tee -a $mysql_log
fi
}
selinux_off(){#关闭selinux
delimiter | tee -a $mysql_log
echoinfo ${messages[1]} | tee -a $mysql_log
sed -i '/SELINUX=enforcing/c \SELINUX=disabled' /etc/selinux/config &>> $mysql_log
sleep 3
num2="$(expr length "`getenforce`")"
if [ $num2 -eq 8 ];then
echo -e "\033[44;37;1m selinux已经永久关闭!!!\033[0m" | tee -a $mysql_log
else
echo -e "\033[43;37;1m selinux关闭失败!!!\033[0m" | tee -a $mysql_log
fi
}
iptable_off(){#关闭iptables
delimiter | tee -a $mysql_log
echoinfo ${messages[2]} | tee -a $mysql_log
yum install -y iptables-services &>> $mysql_log
systemctl daemon-reload &>> $mysql_log
systemctl stop iptables.service &>> $mysql_log
systemctl disable iptables.service &>> $mysql_log
#若要重新启动,需yum安装iptables-services使用systemctl unmask/restart iptables启动即可
sleep 3
num3="$(systemctl status iptables.service | expr length "`awk '/Active/{print $2}'`")"
if [ $num3 -eq 8 ];then
echo -e "\033[44;37;1m iptables已经永久关闭!!!\033[0m" | tee -a $mysql_log
else
echo -e "\033[43;37;1m iptables关闭失败!!!\033[0m" | tee -a $mysql_log
fi
}
set_repo(){#配置阿里源和本地yum源
delimiter | tee -a $mysql_log
echoinfo ${messages[3]} | tee -a $mysql_log
mv -f $repo/* /opt/ &>> $mysql_log
echo "[centos7]
name=centos_7.9
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0" &> $repo/local.repo | tee -a $mysql_log
wget -O $repo/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>> $mysql_log
yum clean all &>> $mysql_log
yum repolist &>> $mysql_log
sleep 3
num4="$(ls /etc/yum.repos.d/ | wc -l)"
if [ $num4 -eq 2 ];then
echo -e "\033[44;37;1m 阿里源和本地yum源已配置成功!!! \033[0m" | tee -a $mysql_log
ls $repo/ | tee -a $mysql_log
else
echo -e "\033[43;37;1m 阿里源和本地yum源配置失败!!! \033[0m" | tee -a $mysql_log
ls $repo/ | tee -a $mysql_log
fi
}
set_iso(){#设置镜像自动挂载
delimiter | tee -a $mysql_log
echoinfo ${messages[4]} | tee -a $mysql_log
mkdir /mnt/cdrom &>> $mysql_log
echo "/dev/sr0/mnt/cdromiso9660defaults0 0" &>> /etc/fstab
umount -a &>> $mysql_log
sleep 3
mount -a &>> $mysql_log
sleep 5
num5="$(df -h | awk '/sr0/{printf "%.f\n",$5}')"
if [ $num5 -eq 100 ];then
echo -e "\033[44;37;1m 本地镜像挂载成功!!!\033[0m" | tee -a $mysql_log
elif [ -z "$num5" ];then
umount -a &>> $mysql_log
sleep 3
mount -a &>> $mysql_log
sleep 5
num6="$(df -h | awk '/sr0/{printf "%.f\n",$5}')"
if [ $num6 -eq 100 ];then
echo -e "\033[44;37;1m 本地镜像挂载成功!!!\033[0m" | tee -a $mysql_log
else
echo -e "\033[43;37;1m WARNING!!! WARNING!!! WARNING!!! \033[0m" | tee -a $mysql_log
fi
fi
}
ins_dep(){#安装依赖包
delimiter | tee -a $mysql_log
echoinfo ${messages[5]} | tee -a $mysql_log
yum clean all &>> $mysql_log
yum makecache &>> $mysql_log
yum remove -y boost-* &>> $mysql_log
yum install -y $package &>> $mysql_log
sleep 5
if_test ${messages[5]}
}
check_rpm(){#检查系统中是否存在MySQL源码包
if [ ! -f $mysql_rpm ];then
wget https://mirrors.aliyun.com/mysql/MySQL-5.7/mysql-community-5.7.38-1.el7.src.rpm &> /dev/null
else
echo "系统中MySQL源码包已存在,请安装!!!" &>> $mysql_log
fi
}
ins_rpm(){#下载解压并安装软件包
delimiter | tee -a $mysql_log
echoinfo ${messages[6]} | tee -a $mysql_log
check_rpm | tee -a $mysql_log
rpm -ivh $mysql_rpm > /dev/null 2>&1
sleep 5
tar -jxvf $(pwd)/rpmbuild/SOURCES/$boost_bz -C $mysql_src/ &>> $mysql_log
tar -zxvf $(pwd)/rpmbuild/SOURCES/$mysql_gz -C $mysql_src/ &>> $mysql_log
if_test ${messages[6]}
num7="$(grep mysql /etc/passwd | awk -F ":" '{print $3}')"
if [ -z "$num7" ];then
echo -e "\033[42;37;1m mysql用户不存在,请创建!!!\033[0m" &>> $mysql_log
useradd -M -s /sbin/nologin mysql &>> $mysql_log
id mysql &>> $mysql_log
elif [ ! -z "$num7" ];then
echo -e "\033[45;37;1m mysql用户已存在,请使用!!!\033[0m" &>> $mysql_log
else
echo -e "\033[43;37;1m WARNING!!! WARNING!!! WARNING!!! \033[0m" &>> $mysql_log
fi
mkdir -p $mysql_dir/{data,log} &>> $mysql_log
chown -R mysql:mysql $mysql_dir &>> $mysql_log
}
pre_com(){#预编译
delimiter | tee -a $mysql_log
echoinfo ${messages[7]} | tee -a $mysql_log
cd $mysql_src/mysql-5.7.38/
sleep 3
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/src/boost_1_59_0 &>> $mysql_log
sleep 5
if_test ${messages[7]}
}
compile(){#编译
delimiter | tee -a $mysql_log
echoinfo ${messages[8]} | tee -a $mysql_log
num8="$(grep processor /proc/cpuinfo | wc -l)"
#num8="$(cat /proc/cpuinfo | awk '/processor/{print $1}' | wc -l)"
cd $mysql_src/mysql-5.7.38/
make -j $num8 &>> $mysql_log
sleep 5
if_test ${messages[8]}
}
com_ins(){#编译安装
delimiter | tee -a $mysql_log
echoinfo ${messages[9]} | tee -a $mysql_log
cd $mysql_src/mysql-5.7.38/
make install &>> $mysql_log
sleep 5
if_test ${messages[9]}
chown -R mysql:mysql $mysql_usr/ &>> $mysql_log
}
change(){#修改主配置文件
delimiter | tee -a $mysql_log
echoinfo ${messages[10]} | tee -a $mysql_log
cd /root/
mv /etc/my.cnf{,.bak} &>> $mysql_log
echo "[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
character-set-server=utf8
log-error=/data/mysql/log/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid
log-bin=/data/mysql/log/mysql_bin
server-id=1" &> /etc/my.cnf | tee -a $mysql_log
sleep 5
if_test ${messages[10]}
}
startall(){#启动mysql并设置开机自启
delimiter | tee -a $mysql_log
echoinfo ${messages[11]} | tee -a $mysql_log
cp $mysql_usr/support-files/mysql.server $init/mysqld &>> $mysql_log
chmod a+x $init/mysqld &>> $mysql_log
chkconfig --add mysqld &>> $mysql_log
chkconfig mysqld on &>> $mysql_log
sleep 3
chkconfig --list mysqld &>> $mysql_log
$mysql_usr/bin/mysqld --initialize-insecure --user=mysql --basedir=$mysql_usr --datadir=$mysql_dir/data > /dev/null 2>&1
sleep 3
$init/mysqld start &>> $mysql_log
sleep 3
ln -s $mysql_usr/bin/* /usr/local/bin/ &>> $mysql_log
netstat -antup | grep 3306 &>> $mysql_log
$init/mysqld stop &>> $mysql_log
sleep 3
systemctl start mysqld &>> $mysql_log
systemctl enable mysqld &>> $mysql_log
systemctl status mysqld &>> $mysql_log
sleep 5
netstat -antup | grep 3306 &>> $mysql_log
ps -ef | grep mysqld &>> $mysql_log
num9="$(ps -ef | grep mysqld | grep -v grep | awk '{print $2}' | sed -n "2p")"
if [ -z "$num9" ];then
echo -e "\033[43;37;1m mysql数据库未启动,请重启!!! \033[0m" | tee -a $mysql_log
systemctl restart mysqld &>> $mysql_log
sleep 5
num10="$(ps -ef | grep mysqld | grep -v grep | awk '{print $2}' | sed -n "2p")"
if [ -z "$num10" ];then
echo -e "\033[41;37;1m mysqld服务重启失败,请联系管理员!!!\033[0m"
else
echo -e "\033[44;37;1m mysql数据库已经重启成功!!!\033[0m" | tee -a $mysql_log
fi
else
echo -e "\033[44;37;1m mysql数据库正在运行,可以使用!!!\033[0m" | tee -a $mysql_log
fi
}
####################################################
echo "################BEGIN!!!$(date)################" | tee -a $mysql_log
firewalld_off
wait
selinux_off
wait
iptable_off
wait
set_repo
wait
set_iso
wait
ins_dep
wait
ins_rpm
wait
num11="$(ls $(pwd)/ | grep rpmbuild | wc -l)"
if [ $num11 -eq 1 ];then
pre_com
wait
compile
wait
com_ins
wait
change
wait
startall
wait
else
echo -e "\033[41;37;1m mysql安装失败!!!\033[0m \n" | tee -a $mysql_log
exit
fi
echo "################END!!!$(date)################" | tee -a $mysql_log
echo ""
echo -e "\033[45;37;1m Welcome To Use The Linux !!!!!! \033[0m \n" | tee -a $mysql_log
####################################################