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<<EOF
[client]
port = 3306
socket = /home/mysql/lib/mysql.sock
default-character-set = utf8

[mysql]
prompt="MySQL [\d]> "
no-auto-rehash

[mysqld]
port = 3306
socket = /home/mysql/lib/mysql.sock
character-set-server = utf8

basedir = /home/mysql
datadir = /home/mysql/data
pid-file = /home/mysql/lib/mysql.pid
general_log_file = /home/mysql/log/mysql.log
log_error = /home/mysql/log/mysql_error.log
slow_query_log_file = /home/mysql/log/mysql_slow.log
user = mysql
server-id = 21
log_bin = mysql-bin
binlog_format = row
expire_logs_days = 7
EOF
	chown -R mysql:mysql /etc/my.cnf
	
	#start mysql
	if [ $? = 0 ];then
		echo starting mysql ......
		/etc/init.d/mysqld start || service mysqld start >/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 <<EOF
 choose the package you want to download:

 ( 1 ) mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
 ( 2 ) mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz
 ( 3 ) Manually enter the download address
 ( 4 ) exit

EOF
	read -ep "Choose the version you want to install ( 1 | 2 |...): " CHOOSE_VERSION
	case $CHOOSE_VERSION in
	1)
		echo -e "\nyou have chosen mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz\n"
		echo -e "To download the mysql-5.7.22 package ...\n"
		wget  https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -P /home
			JUDGMENT_STATUS
	;;
	2)
		echo -e "\nyou have chosen mysql-8.0.15-linux-glibc2.12-x86_64.tar\n"			
		echo -e "To download the mysql-8.0.15 package ...\n"
		wget  https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz -P /home
			JUDGMENT_STATUS
	;;
	3)
		while true
		do
		read -ep "please enter the download address: " DOWNLOAD_ADDRESS
			if [[ "${DOWNLOAD_ADDRESS}" =~ "dev.mysql.com/get/Downloads" ]];then
				wget ${DOWNLOAD_ADDRESS}
			else
				echo -e "\n${DOWNLOAD_ADDRESS}\n"
				read -ep "Is the address you entered correct? (y/n)" ENTER_STATUS
				case $ENTER_STATUS in
				y | Y)
					wget ${DOWNLOAD_ADDRESS}
				;;
				n | N)
					continue
				;;
				*)
					echo "invaild choice!"
					continue
				;;
				esac
			fi
		JUDGMENT_STATUS
		break
		done
				
	;;
	4)
		exit
	;;
	*)
		echo "invaild choice!"
		continue
	;;
	esac
	#useradd mysql
	groups mysql>/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


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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值