#!/bin/bash Mysql_Major_version="5.6" Mysql_Minin_version="27" Mysql_download_url="http://mirrors.sohu.com/mysql/MySQL-${Mysql_Major_version}/mysql-${Mysql_Major_version}.${Mysql_Minin_version}.tar.gz" Tools_download_dir="/home/tools" Mysql_install_dir="/app/mysql" Mysql_root_dir="/mydata" Mysql_data_dir="${Mysql_root_dir}/data" Mysql_bin_log_dir="${Mysql_root_dir}/bin_log" Mysql_error_log_dir="${Mysql_root_dir}/error_log" Mysql_relay_log_dir="${Mysql_root_dir}/relay_log" Mysql_user="mysql" Mysql_group="mysql" Mysql_cmake="cmake . -DCMAKE_INSTALL_PREFIX=${Mysql_install_dir} -DMYSQL_DATADIR=${Mysql_data_dir} -DMYSQL_UNIX_ADDR=${Mysql_root_dir}/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0" PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin export PATH #**************************************************************************************************************************************************************************** check_result(){ if [ $1 != 0 ];then echo -e "\e[1;31m ERROR!!!! <========Exit install mysql=======> \e[0m" exit 1 fi } #************************************************************************************************************************************************************************** root_or_not() { if [ "$UID" != 0 ];then echo -e "\e[1;31m ERROR, You are not root ,Please login in root to execule the scripts! \e[0m" exit 1 else echo -e "\e[1;31m Good, You are root,and you can continue to install mysql! \e[0m" fi } #************************************************************************************************************************************************************************** install_or_not () { read -p "Installed mysql,Press N to exit or Press Y to continue: " INSTALL_Y_N case $INSTALL_Y_N in Y|y) echo -e "\e[1;31m <=========Starting install mysql=======> \e[0m" start_install ;; N|n) echo -e "\e[1;31m <=========Stop install mysql=======> \e[0m" exit 0 ;; *) install_or_not ;; esac } #************************************************************************************************************************************************************************** check_user_group() { echo "\e[1;44m <======Creating mysql user and group ======> \e[0m" check_user=`awk -F: ‘{a[$1]}END{ if("‘$Mysql_user‘" in a){print "yes"} else print "no"}‘ /etc/passwd` check_group=`awk -F: ‘{a[$1]}END{ if("‘$Mysql_group‘" in a){print "yes"} else print "no"}‘ /etc/group` if [ "${check_user}" = "yes" -a "${check_group}" = "yes" ]; then echo -e "\e[1;31m mysql is exists! \e[0m" else /usr/sbin/groupadd mysql && /usr/sbin/useradd -g mysql -M mysql fi } #************************************************************************************************************************************************************************** download_mysql() { echo -e "\e[1;44m Starting download mysql source tarball,Version is ${Mysql_Major_version}.${Mysql_Minin_version},DownLoad URL:http://mirrors.sohu.com \e[0m" [ ! -d ${Tools_download_dir} ] && mkdir ${Tools_download_dir} -p cd ${Tools_download_dir} yum install wget -y check_result $? wget ${Mysql_download_url} check_result $? } #************************************************************************************************************************************************************************** install_process() { yum -y install gcc gcc-c++ ncurses-devel bison perl check_result $? echo -e "\e[1;44m mysql source tarball,create mysql data_root directory ${Mysql_root_dir} \e[0m" [ ! -d ${Mysql_data_dir} ] && mkdir ${Mysql_data_dir} -p [ ! -d ${Mysql_bin_log_dir} ] && mkdir ${Mysql_bin_log_dir} -p [ ! -d ${Mysql_error_log_dir} ] && mkdir ${Mysql_error_log_dir} -p [ ! -d ${Mysql_relay_log_dir} ] && mkdir ${Mysql_relay_log_dir} echo -e "\e[1;44m Creating${Mysql_install_dir} \e[0m" [ ! -d ${Mysql_install_dir} ] && mkdir -p ${Mysql_install_dir} echo -e "\e[1;44m <======First,Download cmake =====> \e[0m" cd ${Tools_download_dir} wget https://cmake.org/files/v3.4/cmake-3.4.0.tar.gz check_result $? if [ ! -f ${Tools_download_dir}/cmake-3.4.0.tar.gz ] ;then echo "\e[1;31m cmake-3.4.0.tar.gz is not exist!! \e[0m" exit 1 fi tar xf cmake-3.4.0.tar.gz check_result $? cd cmake-3.4.0 ./configure check_result $? gmake check_result $? gmake install check_result $? echo -e "\e[1;44m cmake development tool is installed ,Sencond: starting install mysql-${Mysql_Major_version}.${Mysql_Minin_version}.tar.gz \e[0m" if [ ! -f ${Tools_download_dir}/mysql-${Mysql_Major_version}.${Mysql_Minin_version}.tar.gz ] ; then echo "\e[1;31m mysql-${Mysql_Major_version}.${Mysql_Minin_version}.tar.gz is not exist!! \e[0m" exit 1 fi cd ${Tools_download_dir} tar xf mysql-${Mysql_Major_version}.${Mysql_Minin_version}.tar.gz check_result $? cd mysql-${Mysql_Major_version}.${Mysql_Minin_version} $Mysql_cmake check_result $? make check_result $? make install check_result $? } configure_env(){ echo -e "\e[1;44m <==========Configure system Environment========> \e[0m" /bin/cat /etc/profile | grep mysql && result=echo $? if [ "$result" != 0 ];then echo "export PATH=${Mysql_install_dir}/bin:\$PATH" >> /etc/profile && source /etc/profile echo -e "\e[1;31m <=======Environment is OK.======> \e[0m" else echo -e "\e[1;31m <=======Environment is OK,Please don‘t repeat configure it.======> \e[0m" fi } configure_privilege(){ /bin/chown -R ${Mysql_user}.${Mysql_group} ${Mysql_root_dir} check_result $? /bin/chmod -R 1777 /tmp } mysql_init(){ cd ${Mysql_install_dir}/scripts ./mysql_install_db --basedir=${Mysql_install_dir} --datadir=${Mysql_data_dir} --user=${Mysql_user} check_result $? } prepare_start_shell(){ cd ${Mysql_install_dir}/support-files check_result $? /bin/cp mysql.server /etc/init.d/mysqld check_result $? } configure_conf(){ echo -e "\e[1;44m Confiugre my.cnf file \e[0m" /bin/cat >/etc/my.cnf << EOF [mysqld] datadir=/mydata/data socket=/mydata/mysql.sock user=mysql symbolic-links=0 [mysqld_safe] log-error=/mydata/error_log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid EOF echo -e "Configure file Finished" } start_mysql(){ /etc/init.d/mysqld start check_result $? } #************************************************************************************************************************************************************************ start_install() { check_user_group download_mysql install_process configure_env configure_privilege mysql_init prepare_start_shell configure_conf start_mysql } #************************************************************************************************************************************************************************* root_or_not install_or_not
时间: 2024-10-10 08:27:47