MySQL自动安装脚本分享-天天热文

2023-07-04 05:26:40       来源:博客园

这里分享一个我写的MySQL自动安装脚本mysql_auto_install.sh,它的功能非常简单,就是自动化安装MySQL单实例,让DBA的工作更简单、轻松一些,不用去手工安装MySQL实例。从简单重复的工作中释放出来。


(资料图片)

下面是关于mysql_auto_install.sh脚本的一些说明:

  • 1:这个脚本运行在mysql用户下(注意,root用户下运行会报错,需要修改部分地方),主要原因是因为DBA没有root账号权限,只有mysql用户的权限。另外,我也写了一个mysql_auto_install_root.sh脚本,这个脚本是在root用户下面运行,功能更多一些。暂时还在测试当中。

  • 2:此脚本运行在mysql用户下,因为权限问题,不会禁用透明大页,卸载RPM包,调整内核参数、配置systemctl服务等操作,这些工作交给系统管理员处理。而且我们也假设这些都已经是设定好了的。

  • 3:此脚本只适用于Linux,不适用HP-UX等操作系统。

  • 4:my.cnf的模板中的参数要跟脚本中的参数一致。否则可能有问题。另外模板中参数设置请自己保证正确性与合理性。

  • 5:脚本经过多次测试验证,但是并不保证各种场景都没有bug。发现问题,欢迎反馈,我自己也在不断完善、改进这个脚本。

  • 6: 安装前,要准备好MySQL安装介质、mysql_auto_install.sh、alter_root_passwd.sql,my.cnf的模板(这里命名为mysql8_template.cnf)

其中alter_root_passwd.sql是修改root密码的脚本,如下所示,根据实际情况输入密码,安装完成后,删除此脚本。

ALTERUSER"root"@"localhost"IDENTIFIEDBY"********";flushprivileges;

mysql_auto_install.sh脚本的详情如下(格式可能因为代码插件展示问题,有所混乱,请自行调整。原始脚本格式不会有下面乱七八糟的缩进或不对齐问题)

#!/bin/bash############################################################################################Thisscriptisusedforautoinstallmysql8.0.xx###############################################################################################ScriptName:mysql_auto_install.sh##Author:潇湘隐者##CreateDate:2019-09-06##Blogs:www.cnblogs.com/kerrycode##Email:kerry2008code@qq.com##***************************************************************************************##参数配置##---------------------------------------------------------------------------------------##MYSQL_BASE_DIR/opt/mysql##MYSQL_DATA_DIR/data/mysql##MYSQL_PORT3306##LOG_OUT_TYPElog##---------------------------------------------------------------------------------------##注意事项:##1:如果维护的MySQL数据库都规范化安装、配置的化,下面很多参数都不需要修改##2:my.cnf模板很重要,如果配置有问题,可能导致安装失败。脚本里面有些参数设定都是依赖##my.cnf模板中的设置,请确保你的my.cnf模板中设定跟脚本参数一致##3:此脚本在mysql用户下安装,很多操作受限,例如禁用透明大页、卸载rpm包,调整内核参数##如需处理这些建议使用另外一个版本的脚本处理。此处设定系统管理员已经处理好这些事情##**注意**:此脚本只能在mysql用户下运行,root用户下需要修改部分地方才能成功##---------------------------------------------------------------------------------------##***************************************************************************************##VersionModifiedDateDescription##***************************************************************************************##V.1.02019-09-06创建此脚本##V.1.12019-10-26按SHELL编程规范,重新命名规范各类对象##V.2.02023-06-16重新改写该脚本,增加验证/优化步骤###########################################################################################MySQL的安装路径:basedir的上层目录MYSQL_BASE_DIR=/opt/mysql#MySQL数据目录:datadir的上层目录MYSQL_DATA_DIR=/data/mysql#MySQL安装日志信息MYSQL_INSTALL_LOG=/tmp/soft/mysql_install.log#MySQL压缩包路径MYSQL_INSTALLER_DIR=/tmp/soft#MySQL安装介质#注意事项,如果安装介质不是tar.xz,请修改代码中tar.xzMYSQL_INSTALLER=$MYSQL_INSTALLER_DIR/mysql-8.0.33-linux-glibc2.17-x86_64-minimal.tar.xz#MySQL的my.cnf模板MYSQL_CONFIG_FILE=$MYSQL_INSTALLER_DIR/mysql8_template.cnf#alter_root_passwd文件SQL_ALTER_USER_PWD=$MYSQL_INSTALLER_DIR/alter_root_passwd.sql#Log输出方式:log或out或allLOG_OUT_TYPE=all#MySQL的端口号MYSQL_PORT=3306#记录安装过程的日志信息functionlog_info(){#判断参数个数if[$#-eq1];thenlocallog_msg=$1elseecho"[error]:`date"+%Y%m%d%H:%M:%S"`>thenumberofparametersisincorrect!"fi:<$log_msg"elseecho"[info]:`date`>$log_msg">>$MYSQL_INSTALL_LOGfiEOFcase$LOG_OUT_TYPEinout)echo"[info]:`date"+%Y%m%d%H:%M:%S"`>$log_msg";;log)echo"[info]:`date"+%Y%m%d%H:%M:%S"`>$log_msg">>$MYSQL_INSTALL_LOG;;all)echo"[info]:`date"+%Y%m%d%H:%M:%S"`>$log_msg"echo"[info]:`date"+%Y%m%d%H:%M:%S"`>$log_msg">>$MYSQL_INSTALL_LOG;;*)esac}#记录安装过程的错误信息functionlog_error(){#判断参数个数if[$#-eq1];thenlocallog_msg=$1elseecho"[error]:`date"+%Y%m%d%H:%M:%S"`>thenumberofparametersisincorrect!"fi:<$log_msg"elseecho"[error]:`date`>$log_msg">>$MYSQL_INSTALL_LOGfiEOFcase$LOG_OUT_TYPEinout)echo"[error]:`date"+%Y%m%d%H:%M:%S"`>$log_msg";;log)echo"[error]:`date"+%Y%m%d%H:%M:%S"`>$log_msg">>$MYSQL_INSTALL_LOG;;all)echo"[error]:`date"+%Y%m%d%H:%M:%S"`>$log_msg"echo"[error]:`date"+%Y%m%d%H:%M:%S"`>$log_msg">>$MYSQL_INSTALL_LOG;;*)esac}#卸载系统自带的MARIADB#rpm-qa|grepmariadb|xargsyumremove-y>/dev/nullfunctioncheck_env_setting(){#检查操作系统是否Linux(此脚本不适用HP-UX/SunOS/AIX)if[`uname-s`="Linux"];thenlog_info"Theosislinux,mysqlautoinstallcontinue..."elselog_error"theosisnotlinux,pleasecheckit."exit1fi#检查存放安装包目录是否存在if[!-d"$MYSQL_INSTALLER_DIR"];thenlog_error"themysqlinstallerdirectory$MYSQL_INSTALLER_DIRdidn"texists,pleasecheckit"exit1elselog_info"themysqlinstallerdirectory$MYSQL_INSTALLER_DIRexist,mysqlautoinstallcontinue...";fi#检查MySQL安装文件是否存在if[!-f"$MYSQL_INSTALLER"];thenlog_error"themysqlinstallerfile$MYSQL_INSTALLERdidn"texist,pleasecheckit"exit1elselog_info"themysqlinstallerfile$MYSQL_INSTALLERexist,mysqlautoinstallcontinue..."fi#检查MySQL的my.cnf的模板文件是否存在if[!-f$MYSQL_CONFIG_FILE];thenlog_error"themysqlconfigurationtemplatefile$MYSQL_CONFIG_FILEdid"ntexist,pleasecheckit"exit1elselog_info"themysqlconfigurationtemplatefile$MYSQL_CONFIG_FILEexists,mysqlautoinstallcontinue..."fi#检查修改mysql数据库用户root密码的SQL文件是否存在。if[!-f$SQL_ALTER_USER_PWD];thenlog_error"thesqlfile$SQL_ALTER_USER_PWDdid"ntexist,pleasecheckit"exit1elselog_info"thesqlfile$SQL_ALTER_USER_PWDexists,mysqlautoinstallcontinue..."fi#检查MySQL安装目录的上层目录是否存在if[!-d"$MYSQL_BASE_DIR"];thenlog_error"the$MYSQL_BASE_DIRdidn"texists,pleasecheckit"exit1elselog_info"the$MYSQL_BASE_DIRexists,mysqlautoinstallcontinue..."fi#检查MYSQL_DATA_DIR对应的目录是否存在,此目录为datadir的上一层目录if[!-d"$MYSQL_DATA_DIR"];thenlog_error"the$MYSQL_DATA_DIRdidn"texists,pleasecheckit"exit1elselog_info"the$MYSQL_DATA_DIRexists,mysqlautoinstallcontinue..."fi#检查MYSQL_DATA_DIR目录下是否存在其目录,如果有其它目录,则退出安装(担心已经安装了MySQL实例)if[-d${MYSQL_DATA_DIR}-a"`ls-A${MYSQL_DATA_DIR}`"!=""];thenlog_error"the$MYSQL_DATA_DIRdirectoryhaveexistsfiles,plesecheckit"exit1elselog_info"the$MYSQL_DATA_DIRcheckisok,mysqlautoinstallcontinue..."fi#验证MySQL安装介质的MD5#检查是否存在MySQL实例mysql_status=`netstat-nl|awk"NR>2{if($4~/.*:"${MYSQL_PORT}"/){print"Yes";exit0}}"`echo$mysql_statusif["$mysql_status"="Yes"];thenlog_error"MySQLinstanceexists.pleasecheckit"exit1else#只能作为一个依据,并不能完全保证。log_info"MySQLinstancedidn"texists,mysqlautoinstallcontinue..."fiecho"check_env_settingrunsuccessful."#readANSread-n1-p"Doyouwanttocontinue[Y/N]?"answercase$answerinY|y)log_info"mysqlautoinstallcontinue...";;N|n)log_info"mysqlautoinstallexit.";;*)log_error"yourchoiceiswrong!";;esac}#创建MySQL用户和用户组(此处注释,用户由系统管理员创建)#echo"[info]:createthemysqluser..."#groupaddmysql#/usr/sbin/groupaddmysql#useradd-gmysqlmysql#/usr/sbin/useradd-gmysqlmysql#创建MySQL的相关目录functioncreate_mysql_dir(){cd$MYSQL_DATA_DIRmkdirconfdatalogsslow_logsbin_logsmysql_tempchmod750$MYSQL_DATA_DIR#对目录进行授权chmod-R760$MYSQL_DATA_DIR/bin_logschmod-R700$MYSQL_DATA_DIR/logschmod-R760$MYSQL_DATA_DIR/slow_logs}functionmysql_cnf_setting(){#拷贝my.cnf的模板配置文件,这里不打算直接写入生成my.cnf文件cp$MYSQL_CONFIG_FILE$MYSQL_DATA_DIR/conf/my.cnfif[$?-ne0];thenlog_error"failedtocpthemy.cnf,pleasecheckit"exit1elselog_info"cpthe$MYSQL_CONFIG_FILEtomy.cnfsuccess!"fi}functionmysql_env_setting(){#设置环境变量localenv_exist=`cat~/.bash_profile|grep-E"MYSQL_HISTFILE|$MYSQL_BASE_DIR/mysql8.0/bin"|wc-l`if[$env_exist-eq0];thenecho"exportPATH=\$PATH:$MYSQL_BASE_DIR/mysql8.0/bin">>~/.bash_profileecho"exportMYSQL_HISTFILE=/dev/null">>~/.bash_profilesource~/.bash_profileelselog_info"theenvironmentvariableexists,pleasecheckit!"fi}#解压MySQL的二进制安装包functionmysql_install(){echo"[info]:unzipthemysqlinstallationmedia...."tar-xvf$MYSQL_INSTALLER-C$MYSQL_BASE_DIRif[$?-ne0];thenlog_error"mysqlmediaunzipfailed,pleasecheckthedetail."exitelselog_info"mysqlmediaunzipsuccess,mysqlautoinstallcontinue..."fi#创建软连接localMYSQL_MEDIA_DIR=$(basename${MYSQL_INSTALLER}".tar.xz")#echo$MYSQL_MEDIA_DIRln-s$MYSQL_BASE_DIR/$MYSQL_MEDIA_DIR$MYSQL_BASE_DIR/mysql8.0if[$?-ne0];thenlog_error"createthemysqlsoftlinkfailed,pleasecheckit"exit1elselog_info"createthemysqlsoftlinksuccess,mysqlautoinstallcontinue..."fi#初始化MySQLcd$MYSQL_BASE_DIR/mysql8.0$MYSQL_BASE_DIR/mysql8.0/bin/mysqld\--defaults-file=$MYSQL_DATA_DIR/conf/my.cnf\--initialize\--basedir=$MYSQL_BASE_DIR/mysql8.0\--datadir=$MYSQL_DATA_DIR/data2>&1|>>$MYSQL_INSTALL_LOG#$MYSQL_BASE_DIR/mysql8.0/bin/mysql_ssl_rsa_setup--datadir=$MYSQL_DATA_DIR/data}functionmysql_startup(){$MYSQL_BASE_DIR/mysql8.0/bin/mysqld_safe--defaults-file=$MYSQL_DATA_DIR/conf/my.cnf&#休眠几秒,等待MySQL服务启动sleep15#检查MySQL服务是否启动成功mysql_status=`netstat-nl|awk"NR>2{if($4~/.*:"${MYSQL_PORT}"/){print"Yes";exit0}}"`echo$mysql_statusif["$mysql_status"!="Yes"];thenlog_error"MySQLdidnotstart.pleasecheckit"exit1elselog_info"MySQLstartsuccess,mysqlautoinstallcontinue..."fi}#修改MySQL的root用户密码functionchange_mysql_root_pwd(){#获取root初始密码mysql_root_init_passd=`grep"temporarypassword"$MYSQL_DATA_DIR/logs/mysql_error.log|awk"{print$NF}"`#修改MySQL的root密码mysql-uroot-p"$mysql_root_init_passd"--connect-expired-password<$SQL_ALTER_USER_PWD2>/dev/nullif[$?-ne0];thenlog_error"failtoperformMySQLoperation!!!"exit1elselog_info"alterthemysqluserroot"spasswordsucceed!"fi}#MySQL的安全设置,此脚本在mysql用户下运行,默认这些文件的属主为mysqlfunctionmysql_security_setting{#注意,此处写死mysql_error.log,是因为my.cnf模板中命名统一规范chmod660$MYSQL_DATA_DIR/logs/mysql_error.log#注意,此处写死mysql_binlog.*,是因为my.cnf模板中命名统一规范chmod600$MYSQL_DATA_DIR/bin_logs/mysql_binlog.*chmod400$MYSQL_DATA_DIR/data/*.pem#注意,此处写死mysql_slow.log,是因为my.cnf模板中命名统一规范chmod660$MYSQL_DATA_DIR/slow_logs/mysql_slow.log#plugin_dir的权限设置chmod550$MYSQL_BASE_DIR/mysql8.0/lib/plugin/}functionmain(){check_env_settingcreate_mysql_dirmysql_cnf_settingmysql_env_settingmysql_installmysql_startupchange_mysql_root_pwdif[$?-ne0];thenlog_error"change_mysql_root_pwdrunfailed!"elselog_info"themysqlautoinstallwassuccessful!"fimysql_security_setting}#invokemainfunctionmain

下面是安装测试过程的截图

下面是安装日志mysql_install.log的输出信息截图:

关键词:
x 广告
x 广告

Copyright @  2015-2022 海外生活网版权所有  备案号: 沪ICP备2020036824号-21   联系邮箱:562 66 29@qq.com