MySQL自動(dòng)安裝腳本分享-天天熱文

2023-07-04 05:26:40       來源:博客園

這里分享一個(gè)我寫的MySQL自動(dòng)安裝腳本mysql_auto_install.sh,它的功能非常簡(jiǎn)單,就是自動(dòng)化安裝MySQL單實(shí)例,讓DBA的工作更簡(jiǎn)單、輕松一些,不用去手工安裝MySQL實(shí)例。從簡(jiǎn)單重復(fù)的工作中釋放出來。


(資料圖片)

下面是關(guān)于mysql_auto_install.sh腳本的一些說明:

  • 1:這個(gè)腳本運(yùn)行在mysql用戶下(注意,root用戶下運(yùn)行會(huì)報(bào)錯(cuò),需要修改部分地方),主要原因是因?yàn)镈BA沒有root賬號(hào)權(quán)限,只有mysql用戶的權(quán)限。另外,我也寫了一個(gè)mysql_auto_install_root.sh腳本,這個(gè)腳本是在root用戶下面運(yùn)行,功能更多一些。暫時(shí)還在測(cè)試當(dāng)中。

  • 2:此腳本運(yùn)行在mysql用戶下,因?yàn)闄?quán)限問題,不會(huì)禁用透明大頁(yè),卸載RPM包,調(diào)整內(nèi)核參數(shù)、配置systemctl服務(wù)等操作,這些工作交給系統(tǒng)管理員處理。而且我們也假設(shè)這些都已經(jīng)是設(shè)定好了的。

  • 3:此腳本只適用于Linux,不適用HP-UX等操作系統(tǒng)。

  • 4:my.cnf的模板中的參數(shù)要跟腳本中的參數(shù)一致。否則可能有問題。另外模板中參數(shù)設(shè)置請(qǐng)自己保證正確性與合理性。

  • 5:腳本經(jīng)過多次測(cè)試驗(yàn)證,但是并不保證各種場(chǎng)景都沒有bug。發(fā)現(xiàn)問題,歡迎反饋,我自己也在不斷完善、改進(jìn)這個(gè)腳本。

  • 6: 安裝前,要準(zhǔn)備好MySQL安裝介質(zhì)、mysql_auto_install.sh、alter_root_passwd.sql,my.cnf的模板(這里命名為mysql8_template.cnf)

其中alter_root_passwd.sql是修改root密碼的腳本,如下所示,根據(jù)實(shí)際情況輸入密碼,安裝完成后,刪除此腳本。

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

mysql_auto_install.sh腳本的詳情如下(格式可能因?yàn)榇a插件展示問題,有所混亂,請(qǐng)自行調(diào)整。原始腳本格式不會(huì)有下面亂七八糟的縮進(jìn)或不對(duì)齊問題)

#!/bin/bash############################################################################################Thisscriptisusedforautoinstallmysql8.0.xx###############################################################################################ScriptName:mysql_auto_install.sh##Author:瀟湘隱者##CreateDate:2019-09-06##Blogs:www.cnblogs.com/kerrycode##Email:kerry2008code@qq.com##***************************************************************************************##參數(shù)配置##---------------------------------------------------------------------------------------##MYSQL_BASE_DIR/opt/mysql##MYSQL_DATA_DIR/data/mysql##MYSQL_PORT3306##LOG_OUT_TYPElog##---------------------------------------------------------------------------------------##注意事項(xiàng):##1:如果維護(hù)的MySQL數(shù)據(jù)庫(kù)都規(guī)范化安裝、配置的化,下面很多參數(shù)都不需要修改##2:my.cnf模板很重要,如果配置有問題,可能導(dǎo)致安裝失敗。腳本里面有些參數(shù)設(shè)定都是依賴##my.cnf模板中的設(shè)置,請(qǐng)確保你的my.cnf模板中設(shè)定跟腳本參數(shù)一致##3:此腳本在mysql用戶下安裝,很多操作受限,例如禁用透明大頁(yè)、卸載rpm包,調(diào)整內(nèi)核參數(shù)##如需處理這些建議使用另外一個(gè)版本的腳本處理。此處設(shè)定系統(tǒng)管理員已經(jīng)處理好這些事情##**注意**:此腳本只能在mysql用戶下運(yùn)行,root用戶下需要修改部分地方才能成功##---------------------------------------------------------------------------------------##***************************************************************************************##VersionModifiedDateDescription##***************************************************************************************##V.1.02019-09-06創(chuàng)建此腳本##V.1.12019-10-26按SHELL編程規(guī)范,重新命名規(guī)范各類對(duì)象##V.2.02023-06-16重新改寫該腳本,增加驗(yàn)證/優(yōu)化步驟###########################################################################################MySQL的安裝路徑:basedir的上層目錄MYSQL_BASE_DIR=/opt/mysql#MySQL數(shù)據(jù)目錄:datadir的上層目錄MYSQL_DATA_DIR=/data/mysql#MySQL安裝日志信息MYSQL_INSTALL_LOG=/tmp/soft/mysql_install.log#MySQL壓縮包路徑MYSQL_INSTALLER_DIR=/tmp/soft#MySQL安裝介質(zhì)#注意事項(xiàng),如果安裝介質(zhì)不是tar.xz,請(qǐng)修改代碼中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的端口號(hào)MYSQL_PORT=3306#記錄安裝過程的日志信息functionlog_info(){#判斷參數(shù)個(gè)數(shù)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}#記錄安裝過程的錯(cuò)誤信息functionlog_error(){#判斷參數(shù)個(gè)數(shù)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}#卸載系統(tǒng)自帶的MARIADB#rpm-qa|grepmariadb|xargsyumremove-y>/dev/nullfunctioncheck_env_setting(){#檢查操作系統(tǒng)是否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數(shù)據(jù)庫(kù)用戶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對(duì)應(yīng)的目錄是否存在,此目錄為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目錄下是否存在其目錄,如果有其它目錄,則退出安裝(擔(dān)心已經(jīng)安裝了MySQL實(shí)例)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#驗(yàn)證MySQL安裝介質(zhì)的MD5#檢查是否存在MySQL實(shí)例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#只能作為一個(gè)依據(jù),并不能完全保證。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}#創(chuàng)建MySQL用戶和用戶組(此處注釋,用戶由系統(tǒng)管理員創(chuàng)建)#echo"[info]:createthemysqluser..."#groupaddmysql#/usr/sbin/groupaddmysql#useradd-gmysqlmysql#/usr/sbin/useradd-gmysqlmysql#創(chuàng)建MySQL的相關(guān)目錄functioncreate_mysql_dir(){cd$MYSQL_DATA_DIRmkdirconfdatalogsslow_logsbin_logsmysql_tempchmod750$MYSQL_DATA_DIR#對(duì)目錄進(jìn)行授權(quán)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(){#設(shè)置環(huán)境變量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的二進(jìn)制安裝包functionmysql_install(){echo"[info]:unzipthemysqlinstallationmedia...."tar-xvf$MYSQL_INSTALLER-C$MYSQL_BASE_DIRif[$?-ne0];thenlog_error"mysqlmediaunzipfailed,pleasecheckthedetail."exitelselog_info"mysqlmediaunzipsuccess,mysqlautoinstallcontinue..."fi#創(chuàng)建軟連接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服務(wù)啟動(dòng)sleep15#檢查MySQL服務(wù)是否啟動(dòng)成功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的安全設(shè)置,此腳本在mysql用戶下運(yùn)行,默認(rèn)這些文件的屬主為mysqlfunctionmysql_security_setting{#注意,此處寫死m(xù)ysql_error.log,是因?yàn)閙y.cnf模板中命名統(tǒng)一規(guī)范chmod660$MYSQL_DATA_DIR/logs/mysql_error.log#注意,此處寫死m(xù)ysql_binlog.*,是因?yàn)閙y.cnf模板中命名統(tǒng)一規(guī)范chmod600$MYSQL_DATA_DIR/bin_logs/mysql_binlog.*chmod400$MYSQL_DATA_DIR/data/*.pem#注意,此處寫死m(xù)ysql_slow.log,是因?yàn)閙y.cnf模板中命名統(tǒng)一規(guī)范chmod660$MYSQL_DATA_DIR/slow_logs/mysql_slow.log#plugin_dir的權(quán)限設(shè)置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

下面是安裝測(cè)試過程的截圖

下面是安裝日志mysql_install.log的輸出信息截圖:

關(guān)鍵詞:
x 廣告
x 廣告

Copyright @  2015-2022 海外生活網(wǎng)版權(quán)所有  備案號(hào): 滬ICP備2020036824號(hào)-21   聯(lián)系郵箱:562 66 29@qq.com