mysql-5.7.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # http://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. Install_MySQL-5-7() {
  11. cd $oneinstack_dir/src
  12. if [ "`../include/check_port.py aliyun-oss.linuxeye.com 80`" == 'True' ];then
  13. DOWN_ADDR_MYSQL=http://aliyun-oss.linuxeye.com/mysql/MySQL-5.7
  14. else
  15. [ "$IPADDR_STATE"x == "CN"x ] && DOWN_ADDR_MYSQL=http://mirrors.sohu.com/mysql/MySQL-5.7 || DOWN_ADDR_MYSQL=http://cdn.mysql.com/Downloads/MySQL-5.7
  16. fi
  17. src_url=$DOWN_ADDR_MYSQL/mysql-${mysql_5_7_version}-linux-glibc2.5-${SYS_BIT_b}.tar.gz && Download_src
  18. id -u mysql >/dev/null 2>&1
  19. [ $? -ne 0 ] && useradd -M -s /sbin/nologin mysql
  20. [ ! -d "$mysql_install_dir" ] && mkdir -p $mysql_install_dir
  21. mkdir -p $mysql_data_dir;chown mysql.mysql -R $mysql_data_dir
  22. tar zxf mysql-${mysql_5_7_version}-linux-glibc2.5-${SYS_BIT_b}.tar.gz
  23. mv mysql-${mysql_5_7_version}-linux-glibc2.5-${SYS_BIT_b}/* $mysql_install_dir
  24. if [ "$je_tc_malloc" == '1' ];then
  25. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libjemalloc.so@' $mysql_install_dir/bin/mysqld_safe
  26. elif [ "$je_tc_malloc" == '2' ];then
  27. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libtcmalloc.so@' $mysql_install_dir/bin/mysqld_safe
  28. fi
  29. if [ -d "$mysql_install_dir/support-files" ];then
  30. echo "${CSUCCESS}MySQL install successfully! ${CEND}"
  31. cd ..
  32. rm -rf mysql-$mysql_5_7_version
  33. else
  34. rm -rf $mysql_install_dir
  35. echo "${CFAILURE}MySQL install failed, Please contact the author! ${CEND}"
  36. kill -9 $$
  37. fi
  38. /bin/cp $mysql_install_dir/support-files/mysql.server /etc/init.d/mysqld
  39. sed -i "s@^basedir=.*@basedir=$mysql_install_dir@" /etc/init.d/mysqld
  40. sed -i "s@^datadir=.*@datadir=$mysql_data_dir@" /etc/init.d/mysqld
  41. chmod +x /etc/init.d/mysqld
  42. [ "$OS" == 'CentOS' ] && { chkconfig --add mysqld; chkconfig mysqld on; }
  43. [[ $OS =~ ^Ubuntu$|^Debian$ ]] && update-rc.d mysqld defaults
  44. cd ..
  45. # my.cnf
  46. [ -d "/etc/mysql" ] && /bin/mv /etc/mysql{,_bk}
  47. cat > /etc/my.cnf << EOF
  48. [client]
  49. port = 3306
  50. socket = /tmp/mysql.sock
  51. default-character-set = utf8mb4
  52. [mysql]
  53. prompt="MySQL [\\d]> "
  54. no-auto-rehash
  55. [mysqld]
  56. port = 3306
  57. socket = /tmp/mysql.sock
  58. basedir = $mysql_install_dir
  59. datadir = $mysql_data_dir
  60. pid-file = $mysql_data_dir/mysql.pid
  61. user = mysql
  62. bind-address = 0.0.0.0
  63. server-id = 1
  64. init-connect = 'SET NAMES utf8mb4'
  65. character-set-server = utf8mb4
  66. skip-name-resolve
  67. #skip-networking
  68. back_log = 300
  69. max_connections = 1000
  70. max_connect_errors = 6000
  71. open_files_limit = 65535
  72. table_open_cache = 128
  73. max_allowed_packet = 500M
  74. binlog_cache_size = 1M
  75. max_heap_table_size = 8M
  76. tmp_table_size = 16M
  77. read_buffer_size = 2M
  78. read_rnd_buffer_size = 8M
  79. sort_buffer_size = 8M
  80. join_buffer_size = 8M
  81. key_buffer_size = 4M
  82. thread_cache_size = 8
  83. query_cache_type = 1
  84. query_cache_size = 8M
  85. query_cache_limit = 2M
  86. ft_min_word_len = 4
  87. log_bin = mysql-bin
  88. binlog_format = mixed
  89. expire_logs_days = 7
  90. log_error = $mysql_data_dir/mysql-error.log
  91. slow_query_log = 1
  92. long_query_time = 1
  93. slow_query_log_file = $mysql_data_dir/mysql-slow.log
  94. performance_schema = 0
  95. explicit_defaults_for_timestamp
  96. #lower_case_table_names = 1
  97. skip-external-locking
  98. default_storage_engine = InnoDB
  99. #default-storage-engine = MyISAM
  100. innodb_file_per_table = 1
  101. innodb_open_files = 500
  102. innodb_buffer_pool_size = 64M
  103. innodb_write_io_threads = 4
  104. innodb_read_io_threads = 4
  105. innodb_thread_concurrency = 0
  106. innodb_purge_threads = 1
  107. innodb_flush_log_at_trx_commit = 2
  108. innodb_log_buffer_size = 2M
  109. innodb_log_file_size = 32M
  110. innodb_log_files_in_group = 3
  111. innodb_max_dirty_pages_pct = 90
  112. innodb_lock_wait_timeout = 120
  113. bulk_insert_buffer_size = 8M
  114. myisam_sort_buffer_size = 8M
  115. myisam_max_sort_file_size = 10G
  116. myisam_repair_threads = 1
  117. interactive_timeout = 28800
  118. wait_timeout = 28800
  119. [mysqldump]
  120. quick
  121. max_allowed_packet = 500M
  122. [myisamchk]
  123. key_buffer_size = 8M
  124. sort_buffer_size = 8M
  125. read_buffer = 4M
  126. write_buffer = 4M
  127. EOF
  128. sed -i "s@max_connections.*@max_connections = $(($Mem/2))@" /etc/my.cnf
  129. if [ $Mem -gt 1500 -a $Mem -le 2500 ];then
  130. sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf
  131. sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf
  132. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf
  133. sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf
  134. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf
  135. sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf
  136. sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf
  137. elif [ $Mem -gt 2500 -a $Mem -le 3500 ];then
  138. sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf
  139. sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf
  140. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf
  141. sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf
  142. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf
  143. sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf
  144. sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf
  145. elif [ $Mem -gt 3500 ];then
  146. sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf
  147. sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf
  148. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf
  149. sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf
  150. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf
  151. sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf
  152. sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf
  153. fi
  154. $mysql_install_dir/bin/mysqld --initialize-insecure --user=mysql --basedir=$mysql_install_dir --datadir=$mysql_data_dir
  155. chown mysql.mysql -R $mysql_data_dir
  156. [ -d '/etc/mysql' ] && mv /etc/mysql{,_bk}
  157. service mysqld start
  158. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=$mysql_install_dir/bin:\$PATH" >> /etc/profile
  159. [ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep $mysql_install_dir /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=$mysql_install_dir/bin:\1@" /etc/profile
  160. . /etc/profile
  161. $mysql_install_dir/bin/mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"$dbrootpwd\" with grant option;"
  162. $mysql_install_dir/bin/mysql -e "grant all privileges on *.* to root@'localhost' identified by \"$dbrootpwd\" with grant option;"
  163. $mysql_install_dir/bin/mysql -uroot -p$dbrootpwd -e "reset master;"
  164. rm -rf /etc/ld.so.conf.d/{mysql,mariadb,percona}*.conf
  165. [ -e "$mysql_install_dir/my.cnf" ] && rm -rf $mysql_install_dir/my.cnf
  166. echo "$mysql_install_dir/lib" > /etc/ld.so.conf.d/mysql.conf
  167. ldconfig
  168. service mysqld stop
  169. }