vlambda博客
学习文章列表

mysql一键安装脚本

  • 脚本内容如下:

#!/bin/bash# @Author: GongGuan# @Date: 2021-10-26set -eecho "**********************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 fielse 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/profilesource /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/mysqlecho "********mysql initialization********"mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/datapasswd_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/mysqldecho "********将mysql设置为开机自启动********"echo "/etc/init.d/mysqld start" >> /etc/rc.d/rc.localchmod +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 命令)