自动安装MySQL脚本

在linux 上安装mysql单实例shell脚本

在CentOS 6.5环境测试通过

#!/bin/bash     user=mysql  group=mysql  port=3306  basedir=/usr/local/mysql  datadir=/data/mysql/mysql_${port}/data  sourcefile=$1  mysqlprofile=/etc/my.cnf  logfile=/tmp/mysqlinstall.log  nowtime=`date '+%Y-%m-%d %H:%M:%S'`  retval=0     Usage(){    nowtime=`date '+%Y-%m-%d %H:%M:%S'`    echo -e "e[0;36m${nowtime}[INFO]:Usage: `basename $0` MySQL_Source_Filee[m"  }     if [ $# != 1 ];then     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[ERROR]:INPUT ARGUMENTS ERROR!e[m"     Usage     exit 1  fi     [ ! -d $basedir  ]&& mkdir -p $basedir  [ ! -d $datadir  ]&& mkdir -p $datadir     content=`ls $basedir`  if [ "x$content" != "x" ];then     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[ERROR]:'$basedir' IS NOT NULL.e[m"     exit 1  fi     content1=`ls $datadir`  if [ "x$content1" != "x" ];then     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[ERROR]:'$datadir' IS NOT NULL.e[m"     exit 1  fi     check_port=`netstat -na |grep ":${port}" |awk '{print $4}' |grep ":${port}"`  if [ "x$check_port" != "x" ];then     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[ERROR]:'$port' PORT ALREADY USED!e[m"     exit 1  fi     if [ ! -f $sourcefile ];then     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[ERROR]:'$sourcefile' IS NOT EXISTS.e[m"     Usage     exit 1  else     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[INFO]:Extract MySQL Install File.e[m"     tar xzf $sourcefile --strip-components 1 -C $basedir     retval=$?     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     [ $retval -eq 0 ] && echo -e "e[0;36m${nowtime}[INFO]:Extract MySQL Install File Complete.e[m"  fi     if [ $retval -ne 0  ];then     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[ERROR]:Extract File ERROR,Please Check Your Package.e[m"        echo 1  fi     #create group if not exists    egrep "^$group" /etc/group >& /dev/null    if [ $? -ne 0 ]    then        groupadd $group        nowtime=`date '+%Y-%m-%d %H:%M:%S'`      echo -e "e[0;36m${nowtime}[INFO]:Add Group...e[m"  fi         #create user if not exists    egrep "^$user" /etc/passwd >& /dev/null    if [ $? -ne 0 ]    then        useradd -g $group $user      nowtime=`date '+%Y-%m-%d %H:%M:%S'`        echo -e "e[0;36m${nowtime}[INFO]:Add User...e[m"  fi     #Create my.cnf  datapath=${datadir%/*}  [ ! -d "${datapath}/tmp"  ] && mkdir -p ${datapath}/tmp  [ ! -d "${datapath}/logs"  ] && mkdir -p ${datapath}/logs           if [ ! -f $mysqlprofile ];then  nowtime=`date '+%Y-%m-%d %H:%M:%S'`  echo -e "e[0;36m${nowtime}[INFO]:Starting Create my.cnf.e[m"  cat > $mysqlprofile  '  default_character_set = utf8  EOF  retval=$?  fi  nowtime=`date '+%Y-%m-%d %H:%M:%S'`  [ $retval -eq 0 ] && echo -e "e[0;36m${nowtime}[INFO]:Create my.cnf SUCESS.e[m"     #Initializing datadir  if [ -d $datapath  ];then     chown -R mysql:mysql ${datapath%/*}     cd $basedir     chown -R mysql:mysql *     ./scripts/mysql_install_db --user=$user --datadir=$datadir >  $logfile 2>&1     retval=$?  fi     if [ $retval -ne 0  ];then     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[ERROR]:MySQL Initializing FAIL!e[m"     exit 1  fi  #Add env variables  grep "$basedir/bin" /etc/profile > /dev/null 2>&1  if [ $? -ne 0 ];then     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo  "export PATH=$PATH:$basedir/bin">>/etc/profile     source /etc/profile  fi  #start mysql  if [ ! -f "/etc/init.d/mysqld" ];then      cp $basedir/support-files/mysql.server /etc/init.d/mysqld      /etc/init.d/mysqld start >/dev/null 2>&1      retval=$?  fi     if [ $retval -eq 0 ];then     $basedir/bin/mysqladmin -u root password '123456' 2> $logfile     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[INFO]:Starting MySQL.. SUCCESS!e[m"     retval=0  else     nowtime=`date '+%Y-%m-%d %H:%M:%S'`     echo -e "e[0;36m${nowtime}[ERROR]:Starting MySQL.. FAIL!e[m"     exit 1  fi     #setting account security  if [ $retval -eq 0 ];then  mysql -uroot -p123456 2>/dev/null 
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享