MySQL安装脚本

绿色脚本

#!/bin/bash
package=mysql-community-5.7.26-1.el7.src.rpm
boost=boost_1_59_0.tar.bz2
MY=mysql-5.7.26.tar.gz
config="-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STO
RAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 \-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/root/boost_1_59_0"


yum -y remove boost-* mysql* mariadb* &>/dev/null

yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel &>/dev/null

if [ -e $package ];then
	rpm -ivh $package &>/dev/null
	if [ $? -eq 0 ];then
		echo -e "\e[32mThe mysql package install successfully!!\e[0m"
		cd rpmbuild/SOURCES
		tar -jxf $boost -C /root
		if [ $? -eq 0 ];then
			echo -e "\033[32mboost is install\033[0m"
			tar -zxf $MY -C /root  
			if [ $? -eq 0 ];then
				echo -e "\033[32mmysql is install\033[0m"
			else
				echo -e "\033[31mmysql install failed!!\033[0m"
				exit
			fi
		else
			echo -e "\033[31mboost install failed!!\033[0m"	
			exit
		fi
	else
		echo -e "\033[31mThe mysql package install failed!!\033[0m"
		exit
	fi
else
	echo -e "\033[31mYou need rz the mysql package!!\033[0m"
	exit
fi

usercreat=`awk '{print $1}' /etc/passwd | grep mysql`
groupadd mysql
if [ $? -eq 0 ];then
	useradd -M -s /sbin/nologin/ -r -g mysql mysql
	echo -e "\033[32mmysql user is create successfully!!\033[0m"	
	id mysql &>/dev/null
	if [ $? -eq 0 ];then
		mkdir -p /data/mysql/{
     data,log} 
		chown -R mysql:mysql /data/mysql	
	else
		echo -e "\033[31m can't craete /data/mysql"
		exit
	fi
else
	echo -e "\033[31mmysql user is create failed\033[0m"
	exit
fi

cd /root/mysql-5.7.26
cmake $config &>/dev/null
if [ $? -eq 0 ];then
	echo -e "\033[32mcmake is successfully!!\033[0m"
	make -j 4 &>/dev/null
	if [ $? -eq 0 ];then
		echo -e "\033[32mmake is successfully!!\033[0m"
		make install &>/dev/null
		if [ $? -eq 0 ];then
			echo -e "\033[32mmake install is successfully!!\033[0m"
			chown -R mysql:mysql /usr/local/mysql
		else
			echo -e "\033[31mmake install is failed!!\033[0m"
		fi
	else
		echo "make error,plese rm -rf CmakeCache,config again"
		exit
	fi
else
	echo "cmake error,plese rm -rf CmakeCache,config again"
	exit
fi

echo "[mysqld]
basedir=/usr/local/mysql     
datadir=/data/mysql/data    
port=3306                
socket=/usr/local/mysql/mysql.sock 
symbolic-links=0             
character-set-server=utf8     
log-error=/data/mysql/log/mysqld.log 
pid-file=/usr/local/mysql/mysqld.pid" >/etc/my.cnf

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
if [ $? -eq 0 ];then
	echo -e "\033[32mmysql is add system server\033[0m"
	/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/my
sql/data	if [ $? -eq 0 ];then
		echo -e "\033[32minitialize is successfully!!\033[0m"
		/etc/init.d/mysqld start &>/dev/null
		if [ $? -eq 0 ];then
			echo -e "\033[32mThe mysqld is running!!\033[0m"
			sed -i '$i export MYSQL_HOME=/usr/local/mysql\nexport PATH=$PATH:$MYSQL_HOME/bin' /etc/profi
le			source /etc/profile
			if [ $? -eq 0 ];then
				echo -e "\033[32mPATH is successfully!!\033[0m"
				read -p "please input mysql password:" OP
				mysqladmin -u root password "$OP" &>/dev/null
			else
				echo -e "\033[31mPATH is failed!!\033[0m"
				exit
			fi
		else
			echo -e "\033[31mThe mysqld is run failed!!\033[0m"
			exit
		fi
	else
		echo -e "\033[31minitialize is failed!!\033[0m"
		exit
	fi
else
	echo -e "\033[31mmysql is add system server failed!!\033[0m"
	exit
fi

如果想要后台无交互,直接把echo注释掉

[root@localhost ~]#source MySQL.sh
The mysql package install successfully!!
boost is install
mysql is install
mysql user is create successfully!!
cmake is successfully!!
make is successfully!!
make install is successfully!!
mysql is add system server
initialize is successfully!!
The mysqld is running!!
PATH is successfully!!

你可能感兴趣的:(脚本,mysql)