代码拉取完成,页面将自动刷新
#!/bin/bash
# write by luozhidong
# date: 20190610
# description:默认路径为/usr/local,若要变更则修改MYSQL_HOME
# mysql包为:mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
MYSQL_HOME='/usr/local'
#my-default.cnf参数检查
checkCNF(){
BASEDIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep basedir)
if [[ $(echo $BASEDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql ]];then
sed -i "s|$BASEDIR|basedir = $MYSQL_HOME/mysql|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
fi
DATADIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep datadir)
if [[ $(echo $DATADIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/data ]];then
sed -i "s|$DATADIR|datadir = $MYSQL_HOME/mysql/data|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
fi
LOGDIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep log)
if [[ $(echo $LOGDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/log/mysqld.log ]];then
sed -i "s|$LOGDIR|log-error = $MYSQL_HOME/mysql/log/mysqld.log|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
fi
#检查pid-file = $MYSQL_HOME/mysql/mysqld.pid
PIDDIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep pid)
if [[ $(echo $PIDDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/mysqld.pid ]];then
sed -i "s|$PIDDIR|pid-file = $MYSQL_HOME/mysql/mysqld.pid|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
fi
#检查socket = $MYSQL_HOME/mysql/mysql.sock
SOCKETLINENUM=$(cat -n $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep socket|wc -l)
SOCKETLINE=$(cat -n $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep socket|awk '{print $1}')
for((i=1;i<=${SOCKETLINENUM};i++));
do
LINE=$(echo $SOCKETLINE|awk '{print $'$i'}')
SOCKDIR=$(sed -n ${LINE}p /usr/local/mysql/support-files/my-default.cnf)
if [[ $(echo $SOCKDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/mysql.sock ]];then
sed -i "s|$SOCKDIR|socket = $MYSQL_HOME/mysql/mysql.sock|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
fi
done
}
#检查其他库文件并删除
if [[ -n $(rpm -qa | grep mysql) ]];then
rpm -qa | grep mysql|awk '{print $0}'|xargs rpm -e --nodeps
fi
#检查mysql组
if [[ -z $(cat /etc/group|grep mysql) ]];then
groupadd mysql
fi
#检查mysql用户
if [[ -z $(cat /etc/passwd | grep mysql) ]];then
useradd -r -g mysql mysql
fi
id mysql
CHECK=$(ls $MYSQL_HOME |grep mysql)
if [[ -z $CHECK ]];then
#解压
TARPATH=$(find . -name 'mysql*linux*.tar.gz')
tar -zvxf $TARPATH -C $MYSQL_HOME
mv $MYSQL_HOME/$(ls $MYSQL_HOME |grep mysql) $MYSQL_HOME/mysql
mkdir -p $MYSQL_HOME/mysql/data $MYSQL_HOME/mysql/log
touch $MYSQL_HOME/mysql/log/mysqld.log
touch $MYSQL_HOME/mysql/mysqld.pid
touch $MYSQL_HOME/mysql/mysql.sock
#复制
CNFPATH=$(find . -name 'my-default.cnf')
cp $CNFPATH $MYSQL_HOME/mysql/support-files
#参数检查
checkCNF
#复制到etc
cp $(find $MYSQL_HOME/mysql/ -name "my-default.cnf") /etc/my.cnf
cp $(find $MYSQL_HOME/mysql/ -name "mysql.server") /etc/init.d/mysqld
chown -R mysql:mysql $MYSQL_HOME/mysql/
#环境变量
echo "export PATH=$MYSQL_HOME/mysql/bin:\$PATH" >> /etc/profile
#echo "export MYSQL_HOME_PATH=$MYSQL_HOME/mysql/bin" >> /etc/profile
#source /etc/profile
#执行安装
$MYSQL_HOME/mysql/bin/mysqld_safe --initialize --user=mysql --basedir=$MYSQL_HOME/mysql --datadir=$MYSQL_HOME/mysql/data
#$MYSQL_HOME/mysql/bin/mysqld --initialize --user=mysql --basedir=$MYSQL_HOME/mysql --datadir=$MYSQL_HOME/mysql/data
#$MYSQL_HOME/mysql/bin/mysqld_safe --user=mysql &
#自启动
chkconfig --add mysqld
#临时密码
#cat /var/log/mysqld.log |grep "temporary password"|awk -F':' '{print $4}'
cat $MYSQL_HOME/mysql/log/mysqld.log |grep "temporary password"|awk -F'for' '{print $2}' > passwd.txt
/etc/init.d/mysqld start
#开启3306端口(centos6)
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
/etc/init.d/iptables save
fi
ps -ef |grep mysql|grep -v grep
echo "mysql installed.."
echo "temporary password: " $(cat $(find . -name 'passwd.txt'))
sleep 1s
#ps -ef |grep install_mysql|grep -v grep |awk '{print $2}'|xargs kill
exit 0
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。