1 Star 2 Fork 1

伊长空/Centos7_Mysql5.7自动化安装脚本

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
install_mysql - 副本.sh 4.15 KB
一键复制 编辑 原始数据 按行查看 历史
伊长空 提交于 2022-03-19 09:33 . 上传shell文件
#!/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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Shell
1
https://gitee.com/yichangkong/Centos7_Mysql5.7_AutoInstallShell.git
git@gitee.com:yichangkong/Centos7_Mysql5.7_AutoInstallShell.git
yichangkong
Centos7_Mysql5.7_AutoInstallShell
Centos7_Mysql5.7自动化安装脚本
master

搜索帮助