千家信息网

MySQL自动安装脚本分享

发表于:2025-01-22 作者:千家信息网编辑
千家信息网最后更新 2025年01月22日,本篇内容介绍了"MySQL自动安装脚本分享"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!环境要求:c
千家信息网最后更新 2025年01月22日MySQL自动安装脚本分享

本篇内容介绍了"MySQL自动安装脚本分享"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

环境要求:centos:6.8
MySQL:5.7.20
MySQL安装文件名:mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
MySQL源文件路径:/usr/local/src
MYSQL脚本文件:/usr/local/src/install_mysql.sh

1、脚本如下:

  1. #/bin/sh

  2. #px 2017-11-29

  3. #mysql install

  4. #email:kitten-pan@163.com & 752684428

  5. #version 2.0

  6. #mysql-version mysql5.7.20


  7. source_path=/usr/local/src

  8. install_path=/usr/local/mysql

  9. install_name=mysql-5.7.20-linux-glibc2.12-x86_64


  10. ##检测本机环境

  11. env_fun()

  12. {

  13. echo "============检测本机环境,please wait ..."

  14. username=`cat /etc/passwd |grep mysql|cut -c 1-5`

  15. if [ "$username" = "mysql" ];then

  16. echo "MySQL account is exits..."

  17. return 10

  18. else

  19. echo "mysql account is not exits,ready add..."

  20. groupadd mysql

  21. useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql

  22. return 12

  23. fi

  24. }


  25. ##判断MySQL是否安装

  26. check_fun()

  27. {

  28. rpm_name=`rpm -aq |grep mysql-server`

  29. echo $rpm_name

  30. if [ ! $rpm_name ];then

  31. echo "begin check the source install,please wait for a moment..."

  32. if [ -d $install_path ];then

  33. echo "source install is exist,quit..."

  34. exit 0

  35. else

  36. echo "本机未安装MySQL,稍后为你安装..."

  37. install_fun

  38. op_fun


  39. fi

  40. else

  41. echo -n "rpm MySQL已经安装,是否删除?[yes|no]"

  42. read select

  43. if [ $select = yes ];then

  44. rpm -e $rpm_name --nodeps

  45. rm -rf /var/log/mysqld.log

  46. check_fun

  47. else

  48. exit 0

  49. fi

  50. fi

  51. }


  52. ###安装MySQL

  53. install_fun()

  54. {

  55. read -p "Input a mysql version:" -t 30 mysql_version

  56. read -p "Input a mysql port:" -t 30 mysql_port

  57. read -p "Input a mysql_server_id:" -t 30 mysql_server_id

  58. cd $source_path && echo "解压中,please waiting..."

  59. tar -zxvf ${install_name}.tar.gz && mv $install_name $install_path

  60. ##创建数据目录

  61. mkdir -pv /data/mysqldata

  62. ###给MySQL目录的MySQL账号权限

  63. cd /usr/local && chown mysql.mysql mysql

  64. chown -R mysql.mysql /data/mysqldata

  65. cd $install_path && bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysqldata


  66. cd support-files && cp -rf mysql.server /etc/init.d/mysqld

  67. sed -i 's#datadir\=\/usr\/local\/mysql\/data#\datadir\=\/data\/mysqldata#g' /etc/init.d/mysqld


  68. cat > /etc/my.cnf << EOF

  69. [client]

  70. port = 3309

  71. socket = /tmp/mysql.sock

  72. default-character-set = utf8


  73. [mysqld]

  74. ##mysql-version=5.7.20

  75. port = 3309

  76. socket = /tmp/mysql.sock

  77. datadir = /data/mysqldata

  78. basedir = /usr/local/mysql

  79. pid-file = /data/mysqldata/mysqld.pid

  80. user = mysql

  81. server-id = 1

  82. lower_case_table_names = 1

  83. character-set-server = utf8

  84. back_log = 300

  85. max_connections = 1000

  86. max_connect_errors = 6000

  87. open_files_limit = 65535

  88. table_open_cache = 128

  89. max_allowed_packet = 4M

  90. binlog_cache_size = 1M

  91. max_heap_table_size = 8M

  92. tmp_table_size = 16M


  93. read_buffer_size = 2M

  94. read_rnd_buffer_size = 8M

  95. sort_buffer_size = 8M

  96. join_buffer_size = 8M

  97. key_buffer_size = 4M


  98. thread_cache_size = 8

  99. query_cache_type = 0

  100. query_cache_size = 2M

  101. query_cache_limit = 2M


  102. ft_min_word_len = 4


  103. log_bin = mysql-bin

  104. binlog_format = row

  105. expire_logs_days = 30


  106. log-error=/data/mysqldata/mysqld.log

  107. slow_query_log = 1

  108. long_query_time = 1

  109. slow_query_log_file = /data/mysqldata/mysql-slow.log


  110. performance_schema = 0

  111. explicit_defaults_for_timestamp


  112. skip-external-locking

  113. default-time_zone = '+8:00'


  114. default_storage_engine = InnoDB


  115. innodb_file_per_table = 1

  116. innodb_data_file_path = ibdata1:300M:autoextend

  117. innodb_open_files = 5000

  118. innodb_buffer_pool_size = 2048M

  119. innodb_write_io_threads = 4

  120. innodb_read_io_threads = 8

  121. innodb_thread_concurrency = 0

  122. innodb_purge_threads = 1

  123. innodb_flush_log_at_trx_commit = 2

  124. innodb_log_buffer_size = 128M

  125. innodb_log_file_size = 256M

  126. innodb_log_files_in_group = 2

  127. innodb_max_dirty_pages_pct = 90

  128. innodb_lock_wait_timeout = 120


  129. bulk_insert_buffer_size = 8M

  130. myisam_sort_buffer_size = 8M

  131. myisam_max_sort_file_size = 10G

  132. myisam_repair_threads = 1

  133. interactive_timeout = 28800

  134. wait_timeout = 300


  135. [mysqldump]

  136. quick

  137. max_allowed_packet = 16M


  138. [myisamchk]

  139. key_buffer_size = 8M

  140. sort_buffer_size = 8M

  141. read_buffer = 4M

  142. write_buffer = 4M


  143. EOF

  144. echo "export PATH=/$PATH:/usr/local/mysql/bin">>/etc/profile

  145. echo 'export MYSQL_PS1="(\u@\h:\p) [\d]>"' >> /etc/profile

  146. source /etc/profile


  147. service mysqld restart

  148. }


  149. op_fun()

  150. {

  151. chmod 755 /etc/init.d/mysqld

  152. chkconfig --add mysqld

  153. chkconfig --level 345 mysqld on

  154. }



  155. main(){

  156. echo "###==================================================###"

  157. echo "###welcome to make install mysql with this script###"

  158. echo "###copyright 2013 by px###"

  159. echo "###==================================================###"

  160. sleep 5

  161. env_fun

  162. re=$?

  163. if [ re = 10 ];then

  164. check_fun

  165. else

  166. check_fun

  167. fi

  168. }

  169. main

  170. exit 0

2、执行自动安装脚本
需要填写MySQL的版本信息、端口信息、serverid信息等

点击(此处)折叠或打开

  1. sh install_mysql.sh

  2. ###==================================================###

  3. ###welcome to make install mysql with this script###

  4. ###copyright 2013 by px###

  5. ###==================================================###

  6. ============检测本机环境,please wait ...

  7. mysql account is not exits,ready add...


  8. begin check the source install,please wait for a moment...

  9. 本机未安装MySQL,稍后为你安装...

  10. Input a mysql version:5.7.20

  11. Input a mysql port:3309

  12. Input a mysql_server_id:1

  13. 解压中,please waiting...

3、自动安装完毕 启动

# service mysqld restart
MySQL server PID file could not be found! [FAILED]
Starting MySQL............. [ OK ]


4、登入MySQL 修改密码

点击(此处)折叠或打开

  1. [root@ct562 mysqldata]# mysql -uroot -p

  2. Enter password:

  3. Welcome to the MySQL monitor. Commands end with ; or \g.

  4. Your MySQL connection id is 3

  5. Server version: 5.7.20-log


  6. Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.


  7. Oracle is a registered trademark of Oracle Corporation and/or its

  8. affiliates. Other names may be trademarks of their respective

  9. owners.


  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


  11. (root@localhost:mysql.sock) [(none)]>

  12. (root@localhost:mysql.sock) [(none)]>

  13. (root@localhost:mysql.sock) [(none)]>

  14. (root@localhost:mysql.sock) [(none)]>

  15. (root@localhost:mysql.sock) [(none)]>

  16. (root@localhost:mysql.sock) [(none)]>set password for root@'localhost' =PASSWORD('sysdba@321')

"MySQL自动安装脚本分享"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0