CentOS 7ワンタッチでMySQL 5をインストールする.7.20修正

2719 ワード

#/bin/bash  
# mysql install for linux   
# date: 2017-03-03 Author:Zhengwei OS:CentOS7 mysql:5.7.20  
# copy to linux   

# if /usr/local/mysql  exists  
d=`date +%Y%m%d`
if [ -e /usr/local/mysql ]
   then
    #mv /usr/local/mysql mysql'$d'.bak  
    rm -rf /usr/local/mysql*
fi

if [ -d /opt/mysql ]
    then
    echo "mysql is exists!"       
        rm -rf /opt
        mkdir -p /opt/mysql
else
                mkdir -p /opt/mysql
fi


if [ -f /tools/mysql*tar.gz ]
   then
     cd /opt/mysql/
     tar -zxvf /tools/mysql*tar.gz
     #echo $mysqlname
         cd /opt/mysql/
         mysqlname=`ls`
     mv $mysqlname mysql-5.7.20
   else
      echo "the mysql install package is not exists"  
fi

cd /usr/local/
ln -s /opt/mysql/mysql-5.7.20 /usr/local/mysql


# create mysql user  
user=mysql
group=mysql

#create group if not exists    
egrep "^$group" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    groupadd $group
    echo "$group create successful!!"  
else
    echo "$group  group is exists!"       
fi

#create user if not exists    
egrep "^$user" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    useradd -M -g $group -s /sbin/nologin -d /usr/local/mysql $user
    echo "$user create successful!!"  
     mysqlpath='export PATH=/usr/local/mysql/bin:$PATH'
     echo $mysqlpath  >> /root/.bash_profile
     source /root/.bash_profile
else
    echo "$user user is exists!"  
fi

#create dirctory  
#d=`date +%Y%m%d`  
if [ -d /data/mysql ]
then
    echo "data is exists"  
    cd  /data
    mv  mysql mysql"$d".bak
    mkdir -p /data/mysql
    mkdir -p /data/mysql/mysql_3306
    cd /data/mysql/mysql_3306/
    mkdir data
    mkdir logs
    mkdir tmp
    chown -R mysql:mysql /data/mysql/mysql_3306
 else
    mkdir -p /data/mysql
    mkdir -p /data/mysql/mysql_3306
    cd /data/mysql/mysql_3306/
    mkdir data
    mkdir logs
    mkdir tmp
    chown -R mysql:mysql /data/mysql/mysql_3306

fi

#my.cnf  

cd /usr/local/mysql/
./bin/mysqld --initialize


if [ -f /usr/local/mysql/support-files/mysql.server ]
   then
   rm -f /etc/init.d/mysql/mysql.server
   cp  /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
else
   cp  /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

fi

if [ -f /usr/local/mysql/support-files/mysql.server ]
   then
    service mysql restart
    echo "mysql start successful"  
else
    echo "mysql start failed"  
fi

# print password  

p1=`sed -n '/temporary password/p'  /data/mysql/mysql_3306/data/error.log|awk -F ':' '{print $4}'`
echo "your password is:$p1"