AI智能
改变未来

MySQL自动安装脚本(cmake)

要求:源码包已经上传至/opt/mysql目录下面,现在需要自动安装MySQL,并且将root初始密码设置成为123456

[root@localhost ~]# cat auto_install_mysql.sh#!/bin/bash#写长脚本一定要用变量,这样才能灵活使用,否则下次软件包不在这个目录的时候你可什么办呢?MYSQL_P=\"/opt/mysql/\" #软件包目录BOOST_P=\"/opt/boost/\" #软件包目录MYSQL_T=\"/usr/local/mysql/\" #解压目录BOOST_T=\"/usr/local/boost/\" #解压目录BASE=\"/data/mysql/mysql5.7.29/\" #安装目录--你的是什么版本就改成什么版本,我的是5.7.29DATA=\"/data/mysql/data/\" #数据文件目录LOG=\"/data/mysql/log/\" #日志目录TIME=\"3\" #休眠时间function mysql_evn(){echo \"---环境准备阶段---\"echo  \"\"#rm这么多东西是因为我有可能重新安装,所以每次都需要确保系统环境、目录的纯净rm -rf ${MYSQL_T} >/dev/nullrm -rf ${BOOST_T} >/dev/nullrm -rf ${BASE} >/dev/nullrm -rf ${DATA} >/dev/nullrm -rf ${LOG}  >/dev/nullecho \"1.磁盘空间检查(至少余留8G)\"disk=`df -lh|sed -n \'2p\'|awk \'{print $4}\'|sed \'s/G//g\'`echo \"现在磁盘还有${disk}G空间可用\"declare -i d=`df -lh|sed -n \'2p\'|awk \'{print $4}\'|awk -F \".\" \'{print $1}\'|sed \'s/G//g\'` #处理可能存在的浮点型if [ $d -ge 8 ]; thenecho \"磁盘空间充足\"elseecho \"磁盘空间不足,在${TIME}S后将退出安装界面,请联系管理员扩容\"sleep ${TIME} #休眠一下,显示更加友好breakfiecho \"\"echo \"2.安装必备依赖包\"yum install -y  gcc gcc-c++ cmake ncurses ncurses-devel bison openssl\\*  tree openssl-devel  expat-devel*  libxml2-devel \"Development Libraries\"  \"Development Tools\"  >/dev/nullif [ $? -eq 0 ]; thenecho \"依赖包安装完成!\"elseecho  \"yum出错,系统将在${TIME}S后退出安装界面,请检查错误日志\"sleep ${TIME}breakfiecho \"\"echo \"3.设置系统环境、清空系统MySQL\"sed -i \'7s/enforcing/disabled/g\' /etc/selinux/config >/dev/nulltimedatectl set-timezone Asia/Shanghaidate \"+%F %T\"systemctl stop firewalld >/dev/nullsystemctl disable firewalld >/dev/nullyum -y remove mysql >/dev/nullrm -rf  /var/lib/mysql/* >/dev/nullrm -rf  /etc/my.cnf >/dev/nullrm -rf  /etc/init.d/mysqld >/dev/nullecho \"\"echo \"4.创建MySQL用户、组、数据目录、日志文件、环境变量\"userdel -r mysql >/dev/null #为了确保创建用户组不报错rm -rf ${DATA} >/dev/nullrm -rf ${BASE} >/dev/nullrm -rf ${LOG} >/dev/nullsed -i \'/mysql/d\' /etc/passwdrm -rf /var/mail/mysqluseradd -s /sbin/nologin mysql >/dev/nullmkdir -p ${BASE}mkdir -p ${DATA}mkdir -p ${LOG}rm -rf ${LOG}mysql.log >/dev/nulltouch ${LOG}mysql.logchown -R mysql:mysql ${BASE}chown -R mysql:mysql ${LOG}chown -R mysql:mysql ${DATA}sed -i \'/mysql/d\' /etc/profile >/dev/null #确保这个文件的整洁,以及删除以前的环境变量echo \"export mysql=${BASE}\" >>/etc/profile #sed在这里转义不成功,暂时不知缘由,先用echosed -i \'$a\\export PATH=${PATH}:${mysql}bin\'  /etc/profilesource /etc/profileecho \"\"echo \"5.解压MySQL软件包\"rm -rf ${MYSQL_T}rm -rf ${BOOS_T}mkdir -p  ${MYSQL_T}mkdir -p  ${BOOST_T}chmod  -R 755 ${MYSQL_P}chmod -R 755 ${BOOST_P}echo \"---正在解压MySQL,请耐心等待---\"tar -zxf ${MYSQL_P}`ls ${MYSQL_P}` -C ${MYSQL_T}echo \"---正在解压boost库,请耐心等待---\"tar -zxf ${BOOST_P}`ls ${BOOST_P}` -C ${BOOST_T}echo \"\"echo \"6.环境准备阶段报告如下:\"echo -e \"环境变量:\\n\"tail -n 2 /etc/profileecho \"\"echo -e \"目录结构:\\n\"tree /dataecho \"\"echo \"MySQL文件目录:${MYSQL_T}`ls ${MYSQL_T}`\"echo \"boost库目录:${BOOST_T}`ls ${BOOST_T}`\"echo \"\"disk=`df -lh|sed -n \'2p\'|awk \'{print $4}\'|sed \'s/G//g\'`echo \"现在磁盘还有${disk}G空间可用\"echo \"\"sleep ${TIME}echo \"---环境准备阶段完成---\"}function mysql_install(){echo \"---MySQL安装---\"echo \"\"echo \"1.编译安装\"cd ${MYSQL_T}`ls ${MYSQL_T}`rm -rf Makefile >/dev/nullboost=\"${BOOST_T}`ls ${BOOST_T}`\"cmake \\-DCMAKE_INSTALL_PREFIX=${BASE} \\-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \\-DDEFAULT_CHARSET=utf8 \\-DDEFAULT_COLLATION=utf8_general_ci \\-DWITH_EXTRA_CHARSETS=all \\-DWITH_MYISAM_STORAGE_ENGINE=1\\-DWITH_INNOBASE_STORAGE_ENGINE=1\\-DWITH_MEMORY_STORAGE_ENGINE=1\\-DWITH_READLINE=1\\-DENABLED_LOCAL_INFILE=1\\-DMYSQL_DATADIR=${DATA} \\-DMYSQL-USER=mysql \\-DWITH_BOOST=${boost}if [ $? -eq 0 ]; thendeclare -i n=`cat /proc/cpuinfo| grep \"physical id\"|sort|wc -l`make -j ${n} #这个参数还是蛮重要的,如果是云主机,2核的写成-j 8运行到一半CPU不一定报错(因为CPU是可压缩资源),但是内存可能会挺不住导致程序崩溃。所以严格按照CPU核心数来进行make。elseecho \"cmake出错,系统将在${TIME}S退出编译界面,请检查日志\"breakfirm -rf ${BASE}*make installecho \"编译安装完成\"echo \"\"echo \"2.数据库初始化\"rm -rf ${DATA}* #为了确保初始化成功,这个目录一旦有数据,就会失败true>${LOG}mysql.logecho -e \" \\n[client]port        = 3306socket      = /tmp/mysql.sock[mysqld]port        = 3306socket      = /tmp/mysql.sockuser = mysqlbasedir = ${BASE}datadir = ${DATA}pid-file = ${BASE}mysql.pidlog_error = ${LOG}mysql.log\">/etc/my.cnf${BASE}bin/mysqld \\--initialize \\--user=mysql \\--basedir=${BASE} \\--datadir=${DATA} \\--socket=/tmp/mysql.sockcp ${BASE}support-files/mysql.server /etc/init.d/mysqldchmod +x /etc/init.d/mysqldecho \"数据库安装完成!\"echo \"\"disk=`df -lh|sed -n \'2p\'|awk \'{print $4}\'|sed \'s/G//g\'`echo \"现在磁盘还有${disk}G空间可用\" #提醒作用,因为编译安装真的很占用空间}function startup(){echo \"正在启动\"service mysqld stop >/dev/null #不用restart是因为数据库可能没有开启,restart会报错,显示画面不友好service mysqld start}function reset_password(){read -p \"修改密码请按:y 键(首次登录修改密码请按1)\" option #首次改密码必须alter,所以做了判断if [ $option = 1 ]; thenecho \"正在重置密码(仅限第一次修改密码使用)...\"service mysqld startold_password=`grep \'temporary password\' ${LOG}mysql.log | awk \'{print $NF}\'`echo \"\"echo -e \"\\033[47;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 auto_install_all(){mysql_evnmysql_installstartupreset_password}#菜单栏--图形化菜单可以用dialog 、kdialog 。但是我觉得也不怎么好用,也很丑。function menu {clearechoecho -e \"\\t\\t\\tMySQL auto install Menu\\n\"echo -e \"\\t1. MySQL环境准备\"echo -e \"\\t2. MySQL软件安装\"echo -e \"\\t3. 启动数据库\"echo -e \"\\t4. 重置数据库密码\"echo -e \"\\t5. 全自动安装\"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 ;;5)echo \"\"auto_install_all ;;*)clearecho \"Sorry, wrong selection\";;esacecho -en  \"\\n\\n\\t\\t\\tHit any key to continue\"read -n 1 linedoneclear[root@localhost ~]#
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » MySQL自动安装脚本(cmake)