mysql一键安装脚本
脚本内容如下:
# @Author: GongGuan
# @Date: 2021-10-26
set -e
echo "**********************Install MySQL*********************"
workdir=/usr/local/
echo "************tar xf file*************"
FileName=mysql-5.7.35-linux-glibc2.12-x86_64.tgz
#解压文件并创建软链接
if [ -f $FileName ];then
tar zxvf $FileName -C ${workdir}
if [ $? -eq 0 ];then
echo "********File decompression Success********"
echo "********Create Soft Link********"
cd ${workdir}
ln -s mysql-5.7.35-linux-glibc2.12-x86_64/ mysql
echo "********Soft Link Create Success********"
echo "********Copy my.cnf to /etc/********"
\cp -a mysql/support-files/my.cnf /etc/
else
echo "File Decompression Faild,Please Check!!!"
exit 1
fi
else
echo "No Found File,Please Check!!!"
fi
#创建普通用户和组
echo "********create mysql user and group********"
[ -z "$(grep 'mysql' /etc/group | awk -F ':' '{print $1}')" ] && groupadd -g 1000 mysql
[ -z "$(grep 'mysql' /etc/passwd | awk -F ':' '{print $1}')" ] && useradd -g mysql -u 1000 -s /sbin/nologin mysql
#给mysql目录和配置文件设置属主和属组
echo "********Set permissions********"
chown -R mysql:mysql mysql/*
chown mysql:mysql /etc/my.cnf
#将mysql可执行文件添加到环境变量中
echo "********Add mysql Binary file to env********"
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
#c创建数据目录和binlog目录,执行初始化
echo "********create data directory or binlog directory********"
[ ! -d "/data/mysql/data" -o ! -d "/data/mysql/binlog" ] && mkdir -p /data/mysql/{data,binlog}
chown -R mysql:mysql /data/mysql
echo "********mysql initialization********"
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
passwd_root=$(grep 'temporary password' /data/mysql/data/mysql_error.log | awk '{print $11}')
[ -n "${passwd_root}" ] && echo "<<<<<<<<<<mysql初始化成功>>>>>>>>>>>>"
echo "root初始密码为:${passwd_root}" > /data/mysql/mysql_root_passwd.txt
#copy可执行文件到/etc/init.d/目录下,并重命名为mysqld
\cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
echo "********将mysql设置为开机自启动********"
echo "/etc/init.d/mysqld start" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
#添加软连接
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
#启动Mysql并执行登陆测试
echo "********start mysqld**********"
/etc/init.d/mysqld start
#登陆mysql并设置密码即可(alter user 命令)