要求:rpm包已经上传至/opt/mysql目录下面,现在需要自动安装MySQL,并且将root初始密码设置成为123456
#!/bin/bashfunction mysql_evn(){package_dir=\"/opt/mysql\"cd ${package_dir}chmod 755 *server=`find -maxdepth 1 -name \'*mysql*\'|grep tar.gz|awk -F \"/\" \'{print $2}\'`echo \"$server\"echo \"准备安装MySQL\"echo \"\"echo \"正在卸载系统自带数据库\"yum remove all mysql -yrm -rf /var/lib/mysql/*rm -rf /etc/my.cnfrm -rf /etc/init.d/mysqldrm -rf /var/log/mysql*timedatectl set-timezone Asia/Shanghaiyum install libaio\\* apt-get\\* numactl -yclearecho \"创建安装目录\"rm -rf /datamkdir /data/mysql/ -pecho \"开始解压软件包\"tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz -C /data/mysql/m=`ls /data/mysql/`mv /data/mysql/${m} /data/mysql/mysql5.7clearecho \"环境准备阶段完成\"}function mysql_install(){echo \"创建数据文件目录\"mkdir /data/mysql/data -pmkdir -p /data/mysql/log/touch /data/mysql/log/mysql-error.logtouch /data/mysql/log/mysql-slow.logtouch /data/mysql/mysql.pidtrue >/data/mysql/log/mysql-error.logtrue >/data/mysql/log/mysql-slow.logecho \"创建用户与组\"userdel mysqlgroupadd mysqluseradd -r -g mysql -s /bin/false mysqlchown -R mysql.mysql /data/mysql/rm -rf /etc/init.d/mysql*cp -a /data/mysql/mysql5.7/support-files/mysql.server /etc/init.d/mysql.serverecho \"数据库初始化\"rm -rf /data/mysql/data/*cat>/etc/my.cnf<<EOF[client]port = 3306socket = /tmp/mysql.sock[mysqld]port = 3306socket = /tmp/mysql.sockuser = mysqlbasedir = /data/mysql/mysql5.7datadir = /data/mysql/datapid-file = /data/mysql/mysql.pidlog_error = /data/mysql/log/mysql-error.logslow_query_log_file = /data/mysql/log/mysql-slow.logEOF/data/mysql/mysql5.7/bin/mysqld --initialize --user=mysql --basedir=/data/mysql/mysql5.7 --datadir=/data/mysql/dataif [ $? -eq 0 ]; thenecho \"数据库初始化成功\"elseecho \"数据库初始化失败,请检查报错日志\"fiecho \"创建(命令)软连接\"rm -rf /usr/local/mysqlmkdir -p /usr/local/mysql/bin/ln -s /data/mysql/mysql5.7 /usr/local/mysqlsed -i \'$a\\export mysql=/data/mysql/mysql5.7/bin\' /etc/profilesed -i \'$a\\export PATH=${PATH}:${mysql}\' /etc/profilesource /etc/profile}function startup(){echo \"正在启动\"service mysql.server stopservice mysql.server start}function reset_password(){read -p \"修改密码请按:y 键(首次登录修改密码请按1)\" optionif [ $option = 1 ]; thenecho \"正在重置密码(仅限第一次修改密码使用)...\"service mysql.server restartold_password=`grep \'temporary password\' /data/mysql/log/mysql-error.log|awk \'{print $NF}\'`echo \"\"echo -e \"\\033[5;34m 首次登录修改密码的命令为:\\033[0m \\033[31m ALTER USER \'root\'@\'localhost\' IDENTIFIED BY \\033[0m \'\\033[32mnew_password\\033[0m\' \"echo \"\"sleep 1echo \"你的初始密码为:${old_password}\"mysql -uroot -p\"${old_password}\"echo \"重置完成!\"elseread -p \"请输入原密码:\" pswdecho -e \"\\033[5;34m 修改密码的命令为:\\033[0m \\033[31m update mysql.user set authentication_string=password(\'new_password\') where User=\'USER\'\\033[0m \"echo \"\"mysql -uroot -p\"${pswd}\"echo \"密码重置完成!\"fi}function menu {clearechoecho -e \"\\t\\t\\tMySQLauto install Menu\\n\"echo -e \"\\t1. MySQL环境准备\"echo -e \"\\t2. MySQL软件安装\"echo -e \"\\t3. 启动数据库\"echo -e \"\\t4. 重置数据库密码\"echo -e \"\\t0. Exit program\\n\\n\"echo -en \"\\t\\tEnter option: \"read -n 1 option}while [ 1 ]domenucase $option in0)break ;;1)echo \"\"mysql_evn;;2)echo \"\"mysql_install ;;3)echo \"\"startup ;;4)echo \"\"reset_password ;;*)clearecho \"Sorry, wrong selection\";;esacecho -en \"\\n\\n\\t\\t\\tHit any key to continue\"read -n 1 linedoneclear