MySQL5.7一键安装脚本可选8.0版本,可自己输入下载包路径(shell脚本)

#!/bin/bash
#author zhangfanyuan
#date: 2019-03-28
#this scripts is to install mysql
#if you don't have a mysql package,it will automatically download the mysql-5.7.22 package


#define a function
ONE_BUTTON_OPERATION()

{
	#create directory 
	cd /home/mysql
		if [[ `pwd` == '/home/mysql' ]];then
			echo -e "\nthe directory of data and log will be created ......\n"
			mkdir data && mkdir log && touch log/mysql_error.log
			sleep 0
			chown -R mysql:mysql /home/mysql
		else
			echo invaild path !
			exit 1
		fi
	
	#initialize mysql
		
		/home/mysql/bin/mysqld --initialize --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data
			if [[ $? == 0 ]];then
				echo initialize successful
			else
				echo "initialize error!"
				exit 1
			fi
		grep -ir 'password is generated for root@localhost: ' log/*|tail -1|awk '{print $NF}'>>./temp_password
		echo -e "--------------------------\npassword have been recorded in /home/mysql/temp_password or printed on the screen\n--------------------------"
	
	#configuration startup file
	
		cp ./support-files/mysql.server /etc/init.d/mysqld
			if [ $? = 0 ];then
				sed -i 's/^basedir=/basedir=\/home\/mysql/' /etc/init.d/mysqld
				sed -i 's/^datadir=/datadir=\/home\/mysql\/data/' /etc/init.d/mysqld
			fi
			chmod 755 /etc/init.d/mysqld
		cp ./bin/my_print_defaults /usr/bin/
	#set path
		echo 'export PATH=$PATH:/home/mysql/bin' >> /etc/profile 
		source /etc/profile
	
	#set my.cnf
	if test -w "/etc/my.cnf";then
		sleep 0
	else
		touch /etc/my.cnf
	fi
	
cat >/etc/my.cnf</dev/null
			if test -n "ps aux|grep mysql|grep -v grep";then
				echo mysql has been started successfully!
			fi
	fi
        chkconfig --add mysqld
break
}


############### shell head #################
#user
if [ "$(whoami)" != "root" ];then
	echo "please switch to root user to run this scripts"
	exit
fi

#Pre-installed system package
while true;do
read -ep "Have you installed gcc, gcc-c++,wget ? ( y/n ) :" ANSWER
case $ANSWER in 
y | Y | yes |YES)
	echo -e "\nOK,let's go\n"
	break
	;;
n | N | no | NO)
	for i in gcc gcc-c++ wget
	do
		yum -y install ${i}
	done
		if [ $? != 0 ];then
			echo "Please check if yum is available"
			exit 1
		fi
	break
	;;
	*)
	echo -e "invaild value!\n"
	continue
	;;
esac
done

#package
while true;do
read -ep "Do you have mysql package? ( y/n ) " ACTION
case $ACTION in 
y |Y |yes |YES )
	while true;do

cat << EOF

*********************************************************
 ( 1 ) Manually enter the path and name of the package: 
 ( 2 ) Let the system search for the package itself ... 
 ( 3 ) Exit
*********************************************************

EOF
read -ep "please enter your choice : " MANUALLY_SYSTEM
	case $MANUALLY_SYSTEM in
	1)
		echo -e "\n"
		read -ep  "OK, please enter the path and name of mysql package (for example: /home/mysql-5.7.tar.gz): " MANUAL_PACKAGE_NAME
		#useradd mysql
		groups mysql>/dev/null
			if [ $? = 0 ];then
				sleep 0
			else
				groupadd mysql
				useradd -r -g mysql mysql
				if [ $? = 0 ];then
					echo has added user mysql
				fi
			fi
		
		#decompress mysql
		cd /home
		echo -e "\nmysql package decompressing ......"
			if test -d "mysql";then
				rm -rf mysql && mkdir mysql	
			else
				mkdir mysql
			fi
			tar xf ${MANUAL_PACKAGE_NAME} -C /home/mysql/ --strip-components 1
				if [ $? = 0 ];then
					echo -e "\nhas been successfully decompressing"
				else
					echo decompressing error!
					continue
				fi
		sleep 0
		ONE_BUTTON_OPERATION
		break
		
	;;
	2)
		while true;do
		echo -e "\nOK,searching for mysql package ......"
		find / -name 'mysql*.tar*'|nl
			CHOICE_NUMBER=`find / -name 'mysql*.tar*'|wc -l`
			read -ep "which package will you chooise ( 1 | 2 | 3 |...), please choose a number:  " CHOOSE_PACKAGE
			if [[ ${CHOOSE_PACKAGE} -le ${CHOICE_NUMBER} ]] && [[ ${CHOOSE_PACKAGE} =~ ^[0-9]+$ ]];then
				SEARCH_MYSQL_PACKAGE=`find / -name 'mysql*.tar*'|sed -n "${CHOOSE_PACKAGE}p"`
				echo -e "\nOK! you have chosen ${SEARCH_MYSQL_PACKAGE} "
				#useradd mysql
				groups mysql>/dev/null
					if [ $? = 0 ];then
						sleep 0
					else
						groupadd mysql
						useradd -r -g mysql mysql
						if [ $? = 0 ];then
							echo has added user mysql
						fi
					fi
				
				#decompress mysql
				cd /home
				echo -e "\nmysql package decompressing ......"
				if test -d "mysql";then
					rm -rf mysql && mkdir mysql	
				else
					mkdir mysql
				fi
				tar xf ${SEARCH_MYSQL_PACKAGE} -C /home/mysql/ --strip-components 1
					if [ $? = 0 ];then
						echo -e "-------------------------\nhas been successfully decompressing"
					else
						echo decompressing error!
						exit 1
					fi
                	sleep 0
			ONE_BUTTON_OPERATION
			break
			else
				echo -e "\ninvaild choice!\n"
				continue
			fi
		done
	;;
	3)
		exit
	;;
        *)
	        echo "invaild choice!"
		continue
        ;;
        esac
	done

;;
n |N |no |NO )
# define a function

JUDGMENT_STATUS ()

{

if [ $? != 0 ];then
	echo -e "\nPlease check if it is connected to the Internet. If the network tries to download againd\n"
	read -ep "Do you want to continue downloading? (y/n): " ENTER
	case $ENTER in
	y |Y)
		continue
	;;
	n |N |*)
		exit
	;;
	esac
fi
}

################## download package #####################
	while true
	do
cat </dev/null
		if [ $? = 0 ];then
			sleep 0
		else
			groupadd mysql
			useradd -r -g mysql mysql
			if [ $? = 0 ];then
				echo -e "\nhas added user mysql"
			fi
		fi
	
	#decompress mysql
	cd /home
	echo -e "-------------------\nmysql package decompressing ......\n---------------------"
	PACKAGE=`find /home -name 'mysql*.tar*'`
		if [[ `echo ${PACKAGE}|wc -l` > 1 ]];then
			echo "number of mysql versions too many !"
		        find / -name 'mysql*.tar*'|nl
                        CHOICE_NUMBER=`find / -name 'mysql*.tar*'|wc -l`
                        read -ep "which package will you chooise ( 1 | 2 | 3 |...), please choose a number:  " CHOOSE_PACKAGE
                        if [[ ${CHOOSE_PACKAGE} -le ${CHOICE_NUMBER} ]] && [[ ${CHOOSE_PACKAGE} =~ ^[0-9]+$ ]];then
                                 SEARCH_MYSQL_PACKAGE=`find / -name 'mysql*.tar*'|sed -n "${CHOOSE_PACKAGE}p"`
                                 echo -e "\nOK! you have chosen ${SEARCH_MYSQL_PACKAGE} "
			       	 sleep 0
			       	 if test -d "mysql";then
			       	 	rm -rf mysql && mkdir mysql	
			       	 else
			       	 	mkdir mysql
			       	 fi
			       	 tar xf ${SEARCH_MYSQL_PACKAGE} -C /home/mysql/ --strip-components 1
			       	 	if [ $? = 0 ];then
			       	 		echo has been successfully decompressing
			       	 	else
			       	 		echo decompressing error!
			       	 		exit 1
			       	 	fi
			 fi
		fi
	if test -d "mysql";then
		rm -rf mysql && mkdir mysql	
	else
		mkdir mysql
	fi
	tar xf ${PACKAGE} -C /home/mysql/ --strip-components 1
		if [ $? = 0 ];then
			echo has been successfully decompressing
		else
			echo decompressing error!
			exit 1
		fi
	sleep 0
	ONE_BUTTON_OPERATION
	break
	done
;;
*)
	echo "invaild choice!"
	continue
;;
esac
break
done


有些不完善的地方,后期会更新完善。。。

若有错误,欢迎留言指正,谢谢!

 

你可能感兴趣的:(MySQL,shell)