Ubuntu环境下Mysql自动安装脚本

1.需自备mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz或者其他版本的源码包
2.需修改脚本头部SOURCE_GZ_FILE变量路径
3.脚本代码:

#!/bin/bash

# 安装mysql,使用脚本前请修改源码包在文件系统中的路径
# author:someone
# date:2018/04/21

SOURCE_GZ_FILE="/home/someone/Downloads/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz"
INSTALL_DIR="/usr/local/mysql"

# 判断mysql是否安装
echo "检查mysql是否安装"
mysql -version > /dev/null 2>&1

if [ $? -eq 127 ];then
	echo "没有安装mysql!"
	if [ ! -f ${SOURCE_GZ_FILE} ];then
		echo "${SOURCE_GZ_FILE}文件不存在!"
		exit 1
	fi
	# 如果INSTALL_DIR目录已经存在,那么则删除
	if [ -d ${INSTALL_DIR} ];then
		echo "安装目录已存在,正在删除${INSTALL_DIR}..."
		sudo rm -rf ${INSTALL_DIR}
	fi
	echo "创建安装目录${INSTALL_DIR}..."
	sudo mkdir ${INSTALL_DIR}
	echo "解压${SOURCE_GZ_FILE}到安装目录${INSTALL_DIR}..."
	sudo tar -xzf ${SOURCE_GZ_FILE} -C ${INSTALL_DIR}
	TEMP=`ls ${INSTALL_DIR}`
	sudo cp -R -t ${INSTALL_DIR} ${INSTALL_DIR}/${TEMP}/*
	sudo rm -rf ${INSTALL_DIR}/${TEMP}
	echo "解压完毕!"
	echo "添加mysql用户组、用户..."
	sudo groupadd mysql
	sudo useradd -g mysql mysql
	echo "开始安装..."
	cd ${INSTALL_DIR} && pwd
	sudo mkdir data/mysql
	sudo chown -R mysql:mysql ${INSTALL_DIR}
	sudo ./scripts/mysql_install_db --user=mysql --basedir=${INSTALL_DIR} --datadir=${INSTALL_DIR}/data/mysql
	sudo cp support-files/mysql.server /etc/init.d/mysql
	sudo chmod 755 /etc/init.d/mysql
	sudo cp support-files/my-default.cnf /etc/my.cnf
	echo "请修改/etc/init.d/mysql文件"
	echo "basedir=${INSTALL_DIR}/"
	echo "datadir=${INSTALL_DIR}/data/mysql"
	echo "按Enter键确认..."
	read -n 1
	sudo vim /etc/init.d/mysql
	echo "请修改/etc/profile文件,添加mysql环境变量"
	echo '文件末尾追加export PATH=${PATH}:'${INSTALL_DIR}/bin
	echo "按Enter键确认..."
	read -n 1
	sudo vim /etc/profile	
	source /etc/profile
	sudo echo "安装成功!启动mysql服务..."
	sudo systemctl enable mysql
	sudo service mysql start
	echo "请修改mysql -> root密码..."
	echo "请输入密码:"
	read mysql_pwd
	sudo mysqladmin -u root password ${mysql_pwd}
	
	
else
	echo "Mysql已安装!"
	mysql -version
fi

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