当前位置:   article > 正文

Linux环境使用Shell脚本安装Mysql5.7_usage : ***.sh install|start|stop|status

usage : ***.sh install|start|stop|status

1. 前言

        之前写过使用shell脚本安装Mysql5.6版本,闲暇之余研究了下mysql5.7与5.6安装的区别,写下Mysql5.7的shell运维脚本,供大家参考。(Linux环境使用Shell脚本安装Mysql5.6:https://blog.csdn.net/u011294519/article/details/89004581

2.Mysql5.6与5.7的安装区别

  • Mysql5.7的安装是使用/bin/mysqld,Mysql5.6是使用/scripts/mysql_install_db
  • Mysql5.7没有默认的配置文件my-default.cnf,Mysql5.6在/support-files目录下有my-default.cnf,可以作为my.cnf文件模板
  • Mysql5.7安装完成后首次本机登录不能使用127.0.0.1,必须使用localhost,Mysql5.6则可以使用127.0.0.1

3. 准备工作

        到Mysql官网或者到华为软件仓库去下载Mysql安装包

        官网链接:https://dev.mysql.com/downloads/mysql/5.7.html#downloads

        有32位和64位的区分 ,根据需要下载

        华为软件仓库:https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-5.7/

        我下载的版本是mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

        下载好安装包后拷贝到Linux服务器的任意位置上

        检查机器上是否安装autoconf,执行命令:rpm -qa|grep autoconf

        若无如上输出,则执行命令安装:yum -y install autoconf

4. 配置脚本

        脚本内容如下

  1. #!/bin/bash
  2. # Mysql安装包所在路径,需要带上包名,示例:PACKAGE_FULL_WAY=/root/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
  3. readonly PACKAGE_FULL_WAY=
  4. # Mysql安装主目录,示例:INSTALL_HOME=/opt/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. # 初始化my.cnf,按需配置
  58. function init_my_conf() {
  59. local host_ip=$(/sbin/ip route get 1| sed 's/^.*src \(\S*\).*$/\1/;q')
  60. local total_mem=`free -m |grep "Mem"|awk '{print $2}'`
  61. mkdir -p ${INSTALL_HOME}/log
  62. mkdir -p ${INSTALL_HOME}/mysqld
  63. touch ${INSTALL_HOME}/mysqld/mysqld.pid
  64. chown mysql.mysql ${INSTALL_HOME}/mysqld/mysqld.pid
  65. touch ${INSTALL_HOME}/log/mysql-error.log
  66. touch ${INSTALL_HOME}/log/mysql-slow.log
  67. touch ${INSTALL_HOME}/log/mysqld.log
  68. chown mysql.mysql -R ${INSTALL_HOME}/log
  69. echo "[mysql]
  70. default-character-set=UTF8
  71. [mysqld]
  72. explicit_defaults_for_timestamp = true
  73. socket = ${INSTALL_HOME}/mysql.sock
  74. # Disabling symbolic-links is recommended to prevent assorted security risks
  75. symbolic-links = 0
  76. port = 3306
  77. basedir = ${INSTALL_HOME}
  78. datadir = ${INSTALL_HOME}/data/mysql
  79. pid-file = ${INSTALL_HOME}/mysql.pid
  80. user = mysql
  81. server-id = 1
  82. relay_log_purge=0
  83. read_only=0
  84. slave-skip-errors=1396
  85. lower_case_table_names = 1
  86. character-set-server = utf8
  87. skip-name-resolve
  88. skip-external-locking
  89. back_log = 500
  90. max_connections = 500
  91. max_connect_errors = 2000
  92. open_files_limit = 65535
  93. table_open_cache = 128
  94. max_allowed_packet = 64M
  95. key_buffer_size = 64M
  96. read_buffer_size = 64M
  97. read_rnd_buffer_size = 16M
  98. sort_buffer_size = 16M
  99. join_buffer_size = 16M
  100. tmp_table_size = 96M
  101. max_heap_table_size = 96M
  102. query_cache_size = 8M
  103. query_cache_limit = 8M
  104. thread_cache_size = 64
  105. log_bin = mysql-bin
  106. binlog_format = mixed
  107. binlog_cache_size = 8M
  108. sync_binlog = 1
  109. max_binlog_cache_size = 8M
  110. max_binlog_size = 500M
  111. expire_logs_days = 10
  112. log_error = ${INSTALL_HOME}/log/mysql-error.log
  113. slow_query_log = 1
  114. long_query_time = 1
  115. slow_query_log_file = ${INSTALL_HOME}/log/mysql-slow.log
  116. default_storage_engine = InnoDB
  117. innodb_buffer_pool_size = ${total_mem}
  118. innodb_file_per_table = 1
  119. innodb_data_home_dir = ${INSTALL_HOME}/data/mysql
  120. innodb_log_group_home_dir = ${INSTALL_HOME}
  121. innodb_log_file_size = 500M
  122. innodb_log_buffer_size = 20M
  123. innodb_flush_log_at_trx_commit = 1
  124. innodb_print_all_deadlocks = 1
  125. [mysqld_safe]
  126. log-error = ${INSTALL_HOME}/log/mysqld.log" >/etc/my.cnf
  127. chown mysql.mysql /etc/my.cnf
  128. }
  129. # 安装Mysql
  130. function install_mysql() {
  131. # 创建安装主目录
  132. mkdir -p ${INSTALL_HOME}
  133. # 解压mysql到安装主目录
  134. tar -zxvf ${PACKAGE_FULL_WAY} -C ${INSTALL_HOME} > /dev/null 2>&1
  135. check_result $? "unzip Mysql package error"
  136. local package_name=`ls ${INSTALL_HOME} |grep mysql`
  137. mv ${INSTALL_HOME}/${package_name}/* ${INSTALL_HOME}
  138. rm -rf ${INSTALL_HOME}/${package_name}
  139. cd ${INSTALL_HOME}
  140. # 新建数据库目录
  141. mkdir -p ${INSTALL_HOME}/data/mysql
  142. chown -R mysql:mysql ${INSTALL_HOME}
  143. # 安装并指定用户和data文件夹位置
  144. ./bin/mysqld --initialize-insecure --user=mysql --basedir=${INSTALL_HOME} --datadir=${INSTALL_HOME}/data/mysql
  145. # 初始化my.cnf,按需配置
  146. init_my_conf
  147. # 复制mysql到服务自动启动里面
  148. cp -pf ${INSTALL_HOME}/support-files/mysql.server /etc/init.d/mysqld
  149. chmod 755 /etc/init.d/mysqld
  150. # 修改basedir和datadir
  151. sed -i "s#^basedir=.*#basedir=${INSTALL_HOME}#" /etc/init.d/mysqld
  152. sed -i "s#^datadir=.*#datadir=${INSTALL_HOME}\/data\/mysql#" /etc/init.d/mysqld
  153. # 加入环境变量,方便使用mysql命令,但是需要source /etc/profile
  154. echo "###MYSQL_PATH_ENV_S" >>/etc/profile
  155. echo "export PATH=${INSTALL_HOME}/bin:\$PATH" >> /etc/profile
  156. echo "###MYSQL_PATH_ENV_E" >> /etc/profile
  157. # 启动Mysql
  158. start
  159. if [[ ! -L ${INSTALL_HOME}/mysql.sock ]]; then
  160. ln -s ${INSTALL_HOME}/mysql.sock /tmp
  161. fi
  162. sleep 1
  163. cd ${INSTALL_HOME}
  164. # 修改Mysql用户root密码
  165. ./bin/mysql -u root << EOF
  166. FLUSH PRIVILEGES;
  167. ALTER USER 'root'@'localhost' IDENTIFIED BY '${USER_PASSWD}';
  168. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${USER_PASSWD}' WITH GRANT OPTION;
  169. EOF
  170. sleep 3
  171. # 开启远程登录权限
  172. ./bin/mysql -hlocalhost -uroot -p${USER_PASSWD} << EOF
  173. grant all privileges on *.* to root@'%' identified by '${USER_PASSWD}'; flush privileges;
  174. EOF
  175. chown -R mysql:mysql ${INSTALL_HOME}
  176. }
  177. # 安装Mysql
  178. function install() {
  179. log_info "+++++++++++ step 1 ++++++++++++++++"
  180. check_param
  181. log_info "check_param finish"
  182. log_info "+++++++++++ step 2 ++++++++++++++++"
  183. check_mysql_process
  184. log_info "check_mysql_process finish"
  185. log_info "+++++++++++ step 3 ++++++++++++++++"
  186. add_user
  187. log_info "add_user finish"
  188. log_info "+++++++++++ step 4 ++++++++++++++++"
  189. install_mysql
  190. log_info "install_mysql finish"
  191. }
  192. # 卸载Mysql
  193. function uninstall() {
  194. # 如果Mysql仍启动则停止Msql
  195. local mysql_process_count=`ps -ef |grep ${INSTALL_HOME}|grep -vwE "grep|vi|vim|tail|cat"|wc -l`
  196. if [[ ${mysql_process_count} -gt 0 ]]; then
  197. stop
  198. fi
  199. # 删除创建的文件
  200. rm -rf ${INSTALL_HOME}
  201. rm -rf /etc/init.d/mysqld
  202. rm -rf /etc/my.cnf
  203. if [[ -L /tmp/mysql.sock ]]; then
  204. rm -rf /tmp/mysql.sock
  205. fi
  206. # 删除配置的环境变量
  207. sed -i '/###MYSQL_PATH_ENV_S/,/###MYSQL_PATH_ENV_E/d' /etc/profile
  208. #删除用户和用户组
  209. id mysql &> /dev/null
  210. if [[ $? -eq 0 ]]; then
  211. userdel mysql
  212. fi
  213. log_info "uninstall Mysql success"
  214. }
  215. # 停止Mysql
  216. function stop() {
  217. su - mysql -c "service mysqld stop"
  218. }
  219. # 启动Mysql
  220. function start() {
  221. su - mysql -c "service mysqld start"
  222. }
  223. # Mysql状态检查
  224. function check_status() {
  225. su - mysql -c "service mysqld status"
  226. }
  227. function usage() {
  228. echo "Usage: $PROG_NAME {start|stop|install|uninstall|check_status}"
  229. exit 2
  230. }
  231. PROG_NAME=$0
  232. ACTION=$1
  233. case "$ACTION" in
  234. start)
  235. start
  236. ;;
  237. stop)
  238. stop
  239. ;;
  240. restart)
  241. stop
  242. start
  243. ;;
  244. install)
  245. install
  246. ;;
  247. uninstall)
  248. uninstall
  249. ;;
  250. check_status)
  251. check_status
  252. ;;
  253. *)
  254. usage
  255. ;;
  256. esac

        使用root用户登录Linxu服务器,在任意目录下执行命令

        vim mysql_manager.sh

        按一下insert键,复制以上的脚本内容,在机器上右键粘贴进去

        复制完成后需要修改脚本最上方的三个配置项

PACKAGE_FULL_WAY

Mysql安装包所在路径,需要带上包名

示例:PACKAGE_FULL_WAY=/root/mysql-5.7.25-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脚本大神觉得写得不对的地方请留言指正

5.安装后校验

        使用Navicat登录

        若出现无法连接的情况需要检查是不是服务器防火墙没有放开,执行service iptables stop关闭或放开3306端口即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/139781
推荐阅读
相关标签
  

闽ICP备14008679号