shell安装mysql5.7(二进制安装包)

相比源码包,二进制包属于预编译过的,少了手工编译过程,可以自定义安装目录如果没有特殊需求的话正常用这个安装就可以了

#!/bin/bash

# msyql install

install_url="https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz"
edition=`echo "$install_url" | awk -F'/' '{print $(NF)}'`
install_dir="/usr/local"
mysql_prepare=`find . -name mysql -o -name *glibc*|cut -d'/' -f2`

echo -e "\e[1;34m此脚本为mysql安装脚本,使用glibc预编译安装包\e[0m"
echo -e "\e[1;34m如需更改版本,修改下载url,下载链接:https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/\e[0m"
echo -e "\e[1;34m目前脚本内下载的安装包为:$edition\n\e[0m"
echo -e "\e[1;31m可选择上传mysql5.7的glibc安装包至本脚本同目录执行。\n\e[0m"
echo -e "\e[1;31m输入 1 使用网络源下载安装包安装\n输入 2 使用自传安装包安装\n输入其它退出,30秒内未确认脚本会自行退出\e[0m"

init_check() {
	echo -e "\e[1;32m检测中,请稍等...\e[0m"
	[ ! -e /etc/redhat-release ] && echo -e "\e[1;31mERROR:仅支持redhat系列!\e[0m" && exit 1
	[ $USER != root ] && echo -e "\e[1;31mERROR:执行此脚本需root用户!\e[0m" && exit 1
	[ $(yum repolist | awk '/repolist/{print$2}' | sed 's/,//') -eq 0 ] && echo -e "\e[1;31mERROR:请检查yum源配置。\e[0m" && exit 1
	find / -name mysql -exec rm -rf {} \; &> /dev/null
	yum remove mariadb &> /dev/null
	yum list installed | grep wget &> /dev/null
	test $? -ne 0 && echo -e "\e[1;31m未找到wget,正在安装wget...\e[0m" && yum -y install wget &> /dev/null
}

download_mysql() {
	echo -e "\e[1;34m下载mysql中,勿中断脚本,请耐心等待...\e[0m"
	if wget $install_url;then
		echo -e "\e[1;34m解压中...\e[0m"
		tar xf $edition -C $install_dir
		cd $install_dir
		dir=`echo "$edition" | cut -d'.' -f1-4`
		if [ -e $dir ];then
			mv $dir mysql
			echo -e "\e[1;34m已解压至$install_dir\e[0m"
		else
			echo -e "\e[1;31mERROR:未找到$dir\e[0m"
			exit 2
		fi
	else
		echo -e "\e[1;31mERROR:下载$edition失败\e[0m"
		exit 2
	fi
}

my_cnf() {
touch /etc/my.cnf &>/dev/null
echo > /etc/my.cnf
cat >> /etc/my.cnf <<-EOF
[mysqld]
basedir=$install_dir/mysql
datadir=$install_dir/mysql/data
log-error=$install_dir/mysql/data/error.log
pid-file=$install_dir/mysql/data/mysql.pid
user=mysql
server-id=1
log-bin=$install_dir/mysql/binlog/bin-log
binlog_format="ROW"
max_binlog_size=200M
character-set-server=utf8
default-storage-engine=INNODB
skip-grant-tables
EOF
}

install_mysql() {
	if [ -e $install_dir/mysql ];then
		groupadd mysql
		useradd -r -g mysql -s /bin/false mysql
		cd $install_dir/mysql/
		echo -e "\e[1;34m安装mysql中..\e[0m"
		bin/mysqld --initialize --user=mysql --basedir=$install_dir/mysql --datadir=$install_dir/mysql/data
		if [ $? = 0 ];then
			my_cnf
			mkdir $install_dir/mysql/binlog
			chown mysql:mysql -R $install_dir/mysql/
			cp -f support-files/mysql.server /etc/init.d/mysqld
			chkconfig --add mysqld
			chkconfig mysqld on
			ln -s $install_dir/mysql/bin/mysql /usr/bin/
			service mysqld start
			if [ $? = 0 ];then
				$install_dir/mysql/bin/mysql -uroot -p123 -e "update mysql.user set authentication_string=password("123456");"
				sed -ri 's/skip-grant-tables/#skip-grant-tables/' /etc/my.cnf
				service mysqld restart
				if [ $? = 0 ];then
					echo -e "\e[1;32mmysql已安装完成并启动,初始密码为123456\e[0m"
					edition=`mysql -V|cut -d',' -f1|awk '{print $NF}'`
					echo -e "\e[1;32mmysql版本:$edition\e[0m"
				else
					echo -e "\e[1;31mERROR:mysql启动失败,请查看错误日志$install_dir/mysql/data/error.log\e[0m"
				fi
			else
				echo -e "\e[1;31mERROR:mysql启动失败,请查看错误日志$install_dir/mysql/data/error.log,还未修改密码。\e[0m"
				exit 3
			fi
		else
			echo -e "\e[1;31mERROR:安装失败\e[0m"
			exit 3
		fi
	else
		echo -e "\e[1;31mERROR:未找到解压后的mysql目录\e[0m"
		exit 3
	fi
}


read -t30 -p "请输入:" choice
case $choice in
1)
	init_check
	download_mysql
	install_mysql
	;;
2)
	if [ -e "$mysql_prepare" ];then
        echo -e "\e[1;34m查找到的安装包为:$mysql_prepare\e[0m"
                read -t20 -p "是否开始安装,按y开始,输入其它退出脚本:" confirm
                if [[ $confirm == "y" || $confirm == "Y" ]];then
						init_check
                        echo -e "\e[1;34m解压中...\e[0m"
                        tar xf $mysql_prepare -C $install_dir
                        cd $install_dir
                        dir=`echo "$mysql_prepare" | cut -d'.' -f1-4`
                        mv $dir mysql
                        echo -e "\e[1;34m已解压至$install_dir\e[0m"
                        install_mysql
                else
                        echo -e "\e[1;31m已退出\e[0m"
                        exit 4
                fi
	else
        echo -e "\e[1;31mERROR:未找到mysql安装包!\e[0m"
		exit 4
	fi
	;;
*)
	echo -e "\e[1;31m已退出\e[0m"
	exit 4
esac

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值