1. 程式人生 > 實用技巧 >Linux環境使用Shell指令碼安裝Mysql5.6

Linux環境使用Shell指令碼安裝Mysql5.6

1. 前言

之前在Linux上安裝Mysql總是花費很多時間,一行一行執行命令,很是繁瑣,所以決定寫個指令碼提升下效率,這個指令碼是基於Mysql5.6版本寫的,後續版本不適用,但是如果自己能看懂指令碼的可以稍加修改,如果有需要其他版本的自己又不是很熟悉Shell指令碼的留言給我。

2. 準備工作

到Mysql官網或者到華為軟體倉庫去下載Mysql安裝包

官網連結:https://dev.mysql.com/downloads/mysql/5.6.html#downloads

有32位和64位的區分 ,根據需要下載

華為軟體倉庫:https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-5.6/

我下載的版本是mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz

下載好安裝包後拷貝到Linux伺服器的任意位置上

檢查機器上是否安裝autoconf,執行命令:rpm -qa|grep autoconf

若無如上輸出,則執行命令安裝:yum -y install autoconf

3. 配置指令碼

指令碼內容如下


#!/bin/bash
# Mysql安裝包所在路徑,需要帶上包名,示例:PACKAGE_FULL_WAY=/root/mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz
readonly
PACKAGE_FULL_WAY=/root/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
# Mysql安裝主目錄,示例:INSTALL_HOME=/usr/local/mysql
readonly INSTALL_HOME=/usr/local/mysql
# Mysql資料庫root使用者密碼,示例:USER_PASSWD=root
readonly USER_PASSWD=
#check user
if [[ "$UID" -ne 0 ]]; then
echo "ERROR: the script must run as root"
exit 3
fi

function log_info
() {
echo "[$(date -d today +"%Y-%m-%d %H:%M:%S %:::z")] $1"
}

function log_error() {
echo -e "[$(date +"%Y-%m-%d %H:%M:%S %Z%:z")] [ERROR] $* \n"
exit 1

}

function check_result() {
local ret_code=$1
shift
local error_msg=$*
if [[ ${ret_code} -ne 0 ]]; then
log_error ${error_msg}
fi
}

# 校驗引數
function check_param() {
if [[ ! -n ${PACKAGE_FULL_WAY} ]] || [[ ! -n ${INSTALL_HOME} ]] || [[ ! -n ${USER_PASSWD} ]]; then
log_error "Param: PACKAGE_FULL_WAY INSTALL_HOME USER_PASSWD can not be null"
fi
if [[ ! -f ${PACKAGE_FULL_WAY} ]]; then
log_error "Please check the config of PACKAGE_FULL_WAY dose config Mysql package name"
fi
}

function check_mysql_process() {
local mysql_process_count=`ps -ef |grep ${INSTALL_HOME}|grep -vwE "grep|vi|vim|tail|cat"|wc -l`
if [[ ${mysql_process_count} -gt 0 ]]; then
log_error "please stop and uninstall the mysql first"
fi
}

# 新建mysql使用者
function add_user() {
#create group mysql
grep "^mysql" /etc/group &> /dev/null
if [[ $? -ne 0 ]]; then
groupadd mysql
fi

#create user mysql
id mysql &> /dev/null
if [[ $? -ne 0 ]]; then
useradd -g mysql mysql
chage -M 99999 mysql
fi
}

# 安裝Mysql
function install_mysql() {
# 建立安裝主目錄
mkdir -p ${INSTALL_HOME}
# 解壓mysql到安裝主目錄
tar -zxvf ${PACKAGE_FULL_WAY} -C ${INSTALL_HOME} > /dev/null 2>&1
check_result $? "unzip Mysql package error"
local package_name=`ls ${INSTALL_HOME} |grep mysql`
mv ${INSTALL_HOME}/${package_name}/* ${INSTALL_HOME}
rm -rf ${INSTALL_HOME}/${package_name}
cd ${INSTALL_HOME}

# 新建資料庫目錄
mkdir -p ${INSTALL_HOME}/data/mysql
chown -R mysql:mysql ${INSTALL_HOME}

# 安裝並指定使用者和data資料夾位置
./scripts/mysql_install_db --user=mysql --datadir=${INSTALL_HOME}/data/mysql

# 複製mysql到服務自動啟動裡面
cp -pf ${INSTALL_HOME}/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
# 複製配置檔案到etc
cp -pf ${INSTALL_HOME}/support-files/my-default.cnf /etc/my.cnf
chmod 755 /etc/my.cnf
# 修改basedirdatadir
sed -i "s#^basedir=.*#basedir=${INSTALL_HOME}#" /etc/init.d/mysqld
sed -i "s#^datadir=.*#datadir=${INSTALL_HOME}\/data\/mysql#" /etc/init.d/mysqld
# 加入環境變數,方便使用mysql命令,但是需要source /etc/profile
echo "###MYSQL_PATH_ENV_S" >>/etc/profile
echo "export PATH=${INSTALL_HOME}/bin:\$PATH" >> /etc/profile
echo "###MYSQL_PATH_ENV_E" >> /etc/profile
# 啟動Mysql
start
# 修改Mysql使用者root密碼
./bin/mysqladmin -u root -h localhost.localdomain password ${USER_PASSWD}
cd ${INSTALL_HOME}

# 開啟遠端登入許可權
./bin/mysql -h127.0.0.1 -uroot -p${USER_PASSWD} << EOF
grant all privileges on *.* to root@'%' identified by 'root'; flush privileges;
EOF
chown -R mysql:mysql ${INSTALL_HOME}
}

# 安裝Mysql
function install() {
log_info "+++++++++++ step 1 ++++++++++++++++"
check_param
log_info "check_param finish"

log_info "+++++++++++ step 2 ++++++++++++++++"
check_mysql_process
log_info "check_mysql_process finish"

log_info "+++++++++++ step 3 ++++++++++++++++"
add_user
log_info "add_user finish"

log_info "+++++++++++ step 4 ++++++++++++++++"
install_mysql
log_info "install_mysql finish"
}

# 解除安裝Mysql
function uninstall() {
# 如果Mysql仍啟動則停止Msql
local mysql_process_count=`ps -ef |grep ${INSTALL_HOME}|grep -vwE "grep|vi|vim|tail|cat"|wc -l`
if [[ ${mysql_process_count} -gt 0 ]]; then
stop
fi

# 刪除建立的檔案
rm -rf ${INSTALL_HOME}
rm -rf /etc/init.d/mysqld
rm -rf /etc/my.cnf

# 刪除sock檔案
if [[ -f /tmp/mysql.sock ]]; then
rm -rf /tmp/mysql.sock
fi

# 刪除配置的環境變數
sed -i '/###MYSQL_PATH_ENV_S/,/###MYSQL_PATH_ENV_E/d' /etc/profile

#刪除使用者和使用者組
id mysql &> /dev/null
if [[ $? -eq 0 ]]; then
userdel mysql
fi
log_info "uninstall Mysql success"
}

# 停止Mysql
function stop() {
su - mysql -c "service mysqld stop"
}

# 啟動Mysql
function start() {
su - mysql -c "service mysqld start"
}

# Mysql狀態檢查
function check_status() {
su - mysql -c "service mysqld status"
}

function usage() {
echo "Usage: $PROG_NAME {start|stop|install|uninstall|check_status}"
exit 2

}

PROG_NAME=$0
ACTION=$1

case "$ACTION" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
install)
install
;;
uninstall)
uninstall
;;
check_status)
check_status
;;
*)
usage
;;
esac

使用root使用者登入Linxu伺服器,在任意目錄下執行命令

vim mysql_manager.sh

按一下insert鍵,複製以上的指令碼內容,在機器上右鍵貼上進去

複製完成後需要修改指令碼最上方的三個配置項

PACKAGE_FULL_WAY

Mysql安裝包所在路徑,需要帶上包名

示例:PACKAGE_FULL_WAY=/root/mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz

INSTALL_HOME

Mysql安裝主目錄

示例:INSTALL_HOME=/usr/local/mysql

USER_PASSWD

Mysql資料庫root使用者密碼

示例:USER_PASSWD=root

修改完成後按一下Esc鍵,鍵盤輸入:wq!即可

指令碼命令:

sh mysql_manager.sh install

安裝並啟動Mysql

sh mysql_manager.sh start

啟動Mysql

sh mysql_manager.sh stop

關閉Mysql

sh mysql_manager.sh restart

重啟Mysql

sh mysql_manager.sh check_status

Mysql狀態檢查

sh mysql_manager.sh uninstall

解除安裝Mysql

如果有Shell指令碼大神覺得寫得不對的地方請留言指正

  1. #!/bin/bash
  2. # Mysql安裝包所在路徑,需要帶上包名,示例:PACKAGE_FULL_WAY=/root/mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz
  3. readonly PACKAGE_FULL_WAY=
  4. # Mysql安裝主目錄,示例:INSTALL_HOME=/usr/local/mysql
  5. readonly INSTALL_HOME=
  6. # Mysql資料庫root使用者密碼,示例:USER_PASSWD=root
  7. readonly USER_PASSWD=
  8. #check user
  9. if [[ "$UID" -ne 0 ]]; then
  10. echo "ERROR: the script must run as root"
  11. exit 3
  12. fi
  13. function log_info() {
  14. echo "[$(date -d today +"%Y-%m-%d %H:%M:%S %:::z")] $1"
  15. }
  16. function log_error() {
  17. echo -e "[$(date +"%Y-%m-%d %H:%M:%S %Z%:z")] [ERROR] $* \n"
  18. exit 1
  19. }
  20. function check_result() {
  21. local ret_code=$1
  22. shift
  23. local error_msg=$*
  24. if [[ ${ret_code} -ne 0 ]]; then
  25. log_error ${error_msg}
  26. fi
  27. }
  28. # 校驗引數
  29. function check_param() {
  30. if [[ ! -n ${PACKAGE_FULL_WAY} ]] || [[ ! -n ${INSTALL_HOME} ]] || [[ ! -n ${USER_PASSWD} ]]; then
  31. log_error "Param: PACKAGE_FULL_WAY INSTALL_HOME USER_PASSWD can not be null"
  32. fi
  33. if [[ ! -f ${PACKAGE_FULL_WAY} ]]; then
  34. log_error "Please check the config of PACKAGE_FULL_WAY dose config Mysql package name"
  35. fi
  36. }
  37. function check_mysql_process() {
  38. local mysql_process_count=`ps -ef |grep ${INSTALL_HOME}|grep -vwE "grep|vi|vim|tail|cat"|wc -l`
  39. if [[ ${mysql_process_count} -gt 0 ]]; then
  40. log_error "please stop and uninstall the mysql first"
  41. fi
  42. }
  43. # 新建mysql使用者
  44. function add_user() {
  45. #create group mysql
  46. grep "^mysql" /etc/group &> /dev/null
  47. if [[ $? -ne 0 ]]; then
  48. groupadd mysql
  49. fi
  50. #create user mysql
  51. id mysql &> /dev/null
  52. if [[ $? -ne 0 ]]; then
  53. useradd -g mysql mysql
  54. chage -M 99999 mysql
  55. fi
  56. }
  57. # 安裝Mysql
  58. function install_mysql() {
  59. # 建立安裝主目錄
  60. mkdir -p ${INSTALL_HOME}
  61. # 解壓mysql到安裝主目錄
  62. tar -zxvf ${PACKAGE_FULL_WAY} -C ${INSTALL_HOME} > /dev/null 2>&1
  63. check_result $? "unzip Mysql package error"
  64. local package_name=`ls ${INSTALL_HOME} |grep mysql`
  65. mv ${INSTALL_HOME}/${package_name}/* ${INSTALL_HOME}
  66. rm -rf ${INSTALL_HOME}/${package_name}
  67. cd ${INSTALL_HOME}
  68. # 新建資料庫目錄
  69. mkdir -p ${INSTALL_HOME}/data/mysql
  70. chown -R mysql:mysql ${INSTALL_HOME}
  71. # 安裝並指定使用者和data資料夾位置
  72. ./scripts/mysql_install_db --user=mysql --datadir=${INSTALL_HOME}/data/mysql
  73. # 複製mysql到服務自動啟動裡面
  74. cp -pf ${INSTALL_HOME}/support-files/mysql.server /etc/init.d/mysqld
  75. chmod 755 /etc/init.d/mysqld
  76. # 複製配置檔案到etc下
  77. cp -pf ${INSTALL_HOME}/support-files/my-default.cnf /etc/my.cnf
  78. chmod 755 /etc/my.cnf
  79. # 修改basedir和datadir
  80. sed -i "s#^basedir=.*#basedir=${INSTALL_HOME}#" /etc/init.d/mysqld
  81. sed -i "s#^datadir=.*#datadir=${INSTALL_HOME}\/data\/mysql#" /etc/init.d/mysqld
  82. # 加入環境變數,方便使用mysql命令,但是需要source /etc/profile
  83. echo "###MYSQL_PATH_ENV_S" >>/etc/profile
  84. echo "export PATH=${INSTALL_HOME}/bin:\$PATH" >> /etc/profile
  85. echo "###MYSQL_PATH_ENV_E" >> /etc/profile
  86. # 啟動Mysql
  87. start
  88. # 修改Mysql使用者root密碼
  89. ./bin/mysqladmin -u root -h localhost.localdomain password ${USER_PASSWD}
  90. cd ${INSTALL_HOME}
  91. # 開啟遠端登入許可權
  92. ./bin/mysql -h127.0.0.1 -uroot -p${USER_PASSWD} << EOF
  93. grant all privileges on *.* to root@'%' identified by 'root'; flush privileges;
  94. EOF
  95. chown -R mysql:mysql ${INSTALL_HOME}
  96. }
  97. # 安裝Mysql
  98. function install() {
  99. log_info "+++++++++++ step 1 ++++++++++++++++"
  100. check_param
  101. log_info "check_param finish"
  102. log_info "+++++++++++ step 2 ++++++++++++++++"
  103. check_mysql_process
  104. log_info "check_mysql_process finish"
  105. log_info "+++++++++++ step 3 ++++++++++++++++"
  106. add_user
  107. log_info "add_user finish"
  108. log_info "+++++++++++ step 4 ++++++++++++++++"
  109. install_mysql
  110. log_info "install_mysql finish"
  111. }
  112. # 解除安裝Mysql
  113. function uninstall() {
  114. # 如果Mysql仍啟動則停止Msql
  115. local mysql_process_count=`ps -ef |grep ${INSTALL_HOME}|grep -vwE "grep|vi|vim|tail|cat"|wc -l`
  116. if [[ ${mysql_process_count} -gt 0 ]]; then
  117. stop
  118. fi
  119. # 刪除建立的檔案
  120. rm -rf ${INSTALL_HOME}
  121. rm -rf /etc/init.d/mysqld
  122. rm -rf /etc/my.cnf
  123. # 刪除sock檔案
  124. if [[ -f /tmp/mysql.sock ]]; then
  125. rm -rf /tmp/mysql.sock
  126. fi
  127. # 刪除配置的環境變數
  128. sed -i '/###MYSQL_PATH_ENV_S/,/###MYSQL_PATH_ENV_E/d' /etc/profile
  129. #刪除使用者和使用者組
  130. id mysql &> /dev/null
  131. if [[ $? -eq 0 ]]; then
  132. userdel mysql
  133. fi
  134. log_info "uninstall Mysql success"
  135. }
  136. # 停止Mysql
  137. function stop() {
  138. su - mysql -c "service mysqld stop"
  139. }
  140. # 啟動Mysql
  141. function start() {
  142. su - mysql -c "service mysqld start"
  143. }
  144. # Mysql狀態檢查
  145. function check_status() {
  146. su - mysql -c "service mysqld status"
  147. }
  148. function usage() {
  149. echo "Usage: $PROG_NAME {start|stop|install|uninstall|check_status}"
  150. exit 2
  151. }
  152. PROG_NAME=$0
  153. ACTION=$1
  154. case "$ACTION" in
  155. start)
  156. start
  157. ;;
  158. stop)
  159. stop
  160. ;;
  161. restart)
  162. stop
  163. start
  164. ;;
  165. install)
  166. install
  167. ;;
  168. uninstall)
  169. uninstall
  170. ;;
  171. check_status)
  172. check_status
  173. ;;
  174. *)
  175. usage
  176. ;;
  177. esac