shell安装mysql5.7

时间:2023-02-23 15:57:51


#/bin/bash
# mysql install for linux
# date: 2017-03-03 Author:Zhengwei OS:CentOS6 mysql:5.7.14
# 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 mysql*
fi

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


if [ -f /tools/mysql*tar.gz ]
then
cd /opt/mysql/
tar -zxvf /tools/mysql*tar.gz
mv mysql-5.7.14-linux-glibc2.5-x86_64/ mysql-5.7.14
else
echo "the mysql install package is not exists"
fi

cd /usr/local/
ln -s /opt/mysql/mysql-5.7.14 /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 ]
then
echo "data is exists"
cd /
mv data data"$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"