#!/bin/bash # #************************************************************************************** #Author: Noise Lys #QQ: 578110218 #Date: 2021-06-02 #Filename: mysql_v5.7_install.sh #URL: https://www.cnblogs.com/noise/ #Description: The test script #Copyright (C): 2021 All rights reserved #************************************************************************************** . /etc/init.d/functions START=‘echo -e \E[01:31m‘ END=‘\E[0m‘ OS_VERSION=`cat /etc/os-release | sed ‘s/"/=/g‘ | awk -F[=]+ ‘NR==3{print $2}‘` #MYSQL=mysql-5.7.31-linux-glibc2.12-x86_64 #MYSQL_PASSWORD=123456 check(){ if [ $UID -ne 0 ]; then action "not root user,install failed" exit 1 fi cat<<EOF 1> online install 2> offline install EOF read -p "please choose(1/2):" INSTALL_SW sleep 1 read -p "please input the version you want to install (e.g. 5.7.31):" MYSQL_VERSION MYSQL_VERSION_SHORT=`echo $MYSQL_VERSION | awk -F[.]+ ‘{print $1"."$2}‘` sleep 1 read -p "do you want to set password?(y/n):" MYSQL_PASSWORD_SW if [[ ${MY_PASSWORD_SW} == "y" || ${MY_PASSWORD_SW} == "Y" ]]; then read -p "please input the password you want to set:" MYSQL_PASSWORD fi } install_before(){ action "***STEP 1***start to install the related packages***" sleep 1 yum -y install wget &>/dev/null wget http://mirrors.163.com/mysql/Downloads/MySQL-${MYSQL_VERSION_SHORT}/mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64.tar.gz && ${START}"wget mysql successfully"${END} || ${START}"wget mysql error, please curl mirrors.163.com/mysql/Downloads/Mysql-${version}"${END} action "***STEP 2***start to install the related packages***" sleep 1 yum -y install libaio numactl-libs && ${START}"install libaio numactl-libs successfully"${END} || ${START}"install libaio numactl-libs failed"${END} action "***STEP 3***start to add mysql user and group***" sleep 1 groupadd mysql useradd -r -g mysql -s /bin/false mysql } install_mysql(){ action "***STEP 4***unzip the mysql packages***" sleep 1 tar xvf mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64.tar.gz -C /usr/local &>/dev/null && ${START}"unzipped successfully"${END} || ${START}"unzipped failed"${END} action "***STEP 5***create the soft link and grants***" sleep 1 cd /usr/local ln -s mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64/ mysql chown -R root.root /usr/local/mysql/ action "***STEP 6***prepare the PATH***" sleep 1 echo ‘PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh . /etc/profile.d/mysql.sh action "***STEP 7***prepare the configure file***" sleep 1 touch /etc/my.cnf &>/dev/null cat > /etc/my.cnf<<EOF [mysqld] datadir=/data/mysql skip_name_resolve=1 socket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.log pid-file=/data/mysql/mysql.pid [client] socket=/data/mysql/mysql.sock EOF [ -d /data ] || mkdir /data action "***STEP 8***genearte mysql key data***" sleep 1 mkdir -pv /data/mysql mysqld --initialize-insecure --user=mysql --datadir=/data/mysql action "***STEP 9***start mysql service***" sleep 1 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on service mysqld start sleep 1 if [[ ${MYSQL_PASSWORD_SW} == ‘y‘ || ${MYSQL_PASSWORD_SW} == ‘Y‘ ]]; then action "***STEP 10***set password if yes***" mysqladmin -uroot password ${MYSQL_PASSWORD} fi echo -e ‘[mysql]\nprompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"‘ >> /etc/my.cnf reboot } check install_before install_mysql