MySQL二进制安装脚本

1.运行前准备

把配置好的my.cnf拷贝到/usr/local/src,或者见>>>MySQL二进制安装脚本<<<

2.脚本

#!/bin/bash
. /etc/init.d/functions
VERSION="mysql-5.7.26"
PACKAGE_DIR="/usr/local/src"
BASE_DIR="/usr/local/${VERSION}"
WORK_DIR="/usr/local/mysql"
DATA_DIR="/data/mysql"
DIR_TOP="/data"
BINLOG_DIR="/data/binlog"
RELAYLOG_DIR="/data/relaylog"
SLOG_DIR="/data/slowlog"
MYSQL_USER="mysql"
###
echo -e "\033[34m######Starting intall ${VERSION}######\033[0m"
###Error Tip###
Error(){
action "$1" /bin/false
exit 1
}
###移除mariadb###
RemoveMariadb(){
Mariadb_Check=`rpm -qa|grep mariadb`
if [[ $? == 0 ]]
then
yum remove mariadb* -y &>/dev/null || Error "Remove Mariadb Failed"
action "Mariadb Removed Successfully" /bin/true
else
action "Mariadb Already Removed" /bin/true
fi
}
###安装依赖###
InstallDep(){
Dep_Package="libaio-devel ncurses-devel net-tools wget"
for i in ${Dep_Package}
do
Package_Check=`rpm -qa|grep ${i}`
if [[ "${Package_Check}" == "" ]]
then
echo "Starting install ${i}"
yum install -y ${i} &>/dev/null || Error "Install ${i} Package Failed"
action "${i} Package Installed Successfully" /bin/true
else
action "${i} Package Already Exists" /bin/true
fi
done
}
###创建用户###
CreateUser(){
User_Check=`cat /etc/passwd|grep ${MYSQL_USER}`
if [[ $? != 0 ]]
then
useradd -s /sbin/nologin -M ${MYSQL_USER} || Error "Create ${MYSQL_USER} User Failed"
action "${MYSQL_USER} User Created Successfully" /bin/true
else
action "User ${MYSQL_USER} Exists" /bin/true
fi
}
###创建目录###
CreateDir(){
dirlist="${DIR_TOP} ${BASE_DIR} ${DATA_DIR} ${SLOG_DIR} ${BINLOG_DIR} ${RELAYLOG_DIR}"
for i in ${dirlist};
do
if [ ! -d ${i} ]
then
mkdir -p ${i} || Error "Create Path ${i} Failed"
action "Path ${i} Created Successfully" /bin/true
else
action "Path ${i} Already Exists" /bin/true
fi
Owner_Check=`ls -l -d ${i} |awk '{print $3}'`
if [[ "${Owner_Check}" != "${MYSQL_USER}" ]]
then
chown -R ${MYSQL_USER}:${MYSQL_USER} ${i} || Error "Change The Directory Owner Failed"
action "Path ${i} Changed Owner Successfully" /bin/true
else
action "Path ${i} Owner Already Changed" /bin/true
fi
done
}
###设置环境变量###
InitEnv(){
bashrc="/etc/profile"
Check_Env=`cat ${bashrc}|grep ${WORK_DIR}`
if [[ ${Check_Env} == "" ]]
then
echo "export PATH=${WORK_DIR}/bin:\$PATH" >> ${bashrc} || Error "Set Environment Failed"
echo "export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:${WORK_DIR}/lib" >> ${bashrc} || Error "Set Environment Failed"
action "Environment Setted Successfully" /bin/true
else
action "${WORK_DIR} Is Already Exists In ${bashrc}" /bin/true
fi
source /etc/profile
}
###复制my.cnf###
SetMycnf(){
Mycnf="/etc/my.cnf"
if [[ ! -f ${Mycnf} ]]
then
# mv /etc/my.cnf ${Mycnf}.bak || Error "Mv ${Mycnf} Failed"
if [[ -f ${PACKAGE_DIR}/my.cnf ]]
then
cp $PACKAGE_DIR/my.cnf /etc/ || Error "Cp $PACKAGE_DIR/my.cnf Failed"
action "${PACKAGE_DIR}/my.cnf Config Successfully" /bin/true
else
action "${PACKAGE_DIR}/my.cnf Is Not Found" /bin/false
exit 1
fi
else
action "${Mycnf}/my.cnf Already Exists" /bin/true
fi
}
###下载安装包###
DownloadPackages(){
Package_URL="http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz"
Mysql_Package=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz`
if [[ ! -f ${Mysql_Package} ]]
then
action "Starting download Package" /bin/true
wget -P ${PACKAGE_DIR} ${Package_URL} || Error "Download Package Failed"
action "Package Downloaded Successfully" /bin/true
else
action "Package Already Exists" /bin/true
fi
}
###解压文件###
UnzipPackages(){
Mysql_Folder=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz -exec basename {} .tar.gz \;`
Mysql_Package=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz`
if [[ ! -d ${PACKAGE_DIR}/${Mysql_Folder} ]]
then
echo "Starting Unzip MySQL Package,Just Wait"
tar -xf ${Mysql_Package} -C ${PACKAGE_DIR} || Error "Unzip Package Failed"
mv ${PACKAGE_DIR}/${Mysql_Folder}/* ${BASE_DIR}
action "Package Unzipped Successfully" /bin/true
else
action "Package Already Unzipped" /bin/true
fi
}
###初始化数据库###
InitMySql(){
Initcode="--defaults-file=/etc/my.cnf --user=${MYSQL_USER} --datadir=${DATA_DIR} --basedir=${WORK_DIR} --initialize-insecure"
DATA_DIR_File=`ls -A ${DATA_DIR}|wc -l`
if [[ ${DATA_DIR_File} == 0 ]]
then
if [[ ! -d ${WORK_DIR} ]]
then
ln -s ${BASE_DIR} ${WORK_DIR} || Error "Link ${BASE_DIR} ${WORK_DIR} Failed"
chown -R ${MYSQL_USER}:${MYSQL_USER} ${WORK_DIR} || Error "Chown ${WORK_DIR} Failed"
echo "Starting Init ${VERSION}"
${WORK_DIR}/bin/mysqld ${Initcode}|| Error "Init MySQL Failed"
action "${VERSION} Initialized Successfully" /bin/true
fi
else
action "${VERSION} Was Initialized Successfully" /bin/true
fi
}
###服务器启动脚本###
StartScript(){
if [[ ! -f ${WORK_DIR}/support-files/mysql.server ]]
then
action "Start Script File Is Not Found" /bin/false
exit 1
else
if [[ -f /etc/init.d/mysqld ]]
then
action "Script Already Exists" /bin/true
else
cp ${WORK_DIR}/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
action "Script Setted successfully" /bin/true
fi
fi
}
###配置动态链接库目录###
LibDir(){
Lib_File="/etc/ld.so.conf.d/mysql-x86_64.conf"
Lib_Dir="${WORK_DIR}/lib"
if [[ ! -f ${Lib_File} ]]
then
touch ${Lib_File}
echo ${Lib_Dir} >> ${Lib_File}
action "${Lib_File} Setted Successfully" /bin/true
else
action "${Lib_File} Already Setted" /bin/true
fi
ldconfig
}
###启动MySql服务###
StartMySql(){
Stat_Check=`netstat -lntup|grep 3306`
if [[ $? != 0 ]]
then
service mysqld start || Error "MySQL Start Failed"
action "MySQL Start Successfully" /bin/true
else
action "MySQL Service Is Already Running,Exit The Script" /bin/true
exit 1
fi
}
SetPasswd(){
mysql -e "SET PASSWORD = PASSWORD('123.');flush privileges;" || Error "Set MySQL Root Password Failed"
action "MySQL Root Password Setted Successfully" /bin/true
}
TaskMain(){
echo -e "\033[32mStarting Remove Mariadb\033[0m"
RemoveMariadb
echo -e "\033[32mStep One:Starting Install Dependency Packages\033[0m"
InstallDep
echo -e "\033[32mStep Two:Starting Create MySQL User\033[0m"
CreateUser
echo -e "\033[32mStep Three:Starting Create Directory\033[0m"
CreateDir
echo -e "\033[32mStep Four:Starting Set Env\033[0m"
InitEnv
echo -e "\033[32mStep Five:Starting Copy my.cnf to /etc\033[0m"
SetMycnf
echo -e "\033[32mStep Six:Starting Download Package\033[0m"
DownloadPackages
echo -e "\033[32mStep Seven:Starting UnzipPackages\033[0m"
UnzipPackages
echo -e "\033[32mStep Eight:Starting Init MySQL\033[0m"
InitMySql
echo -e "\033[32mStep Nine:Starting Set Service Start Script\033[0m"
StartScript
echo -e "\033[32mStep Ten:Starting Set Libdir\033[0m"
LibDir
echo -e "\033[32mStep Eleven:Starting MySQL Service\033[0m"
StartMySql
echo -e "\033[32mStep Twelve:Starting Set MySQL Root Password\033[0m"
SetPasswd
echo -e "\033[32mCongratulations, MySQL Installation Was Successfully\033[0m"
}
TaskMain


「 文章如果对你有帮助,请点个赞哦^^ 」 

0

这篇文章有一条评论

评论关闭。