mysql-5.6.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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-6() {
  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.6
  14. else
  15. [ "$IPADDR_STATE"x == "CN"x ] && DOWN_ADDR_MYSQL=http://mirrors.sohu.com/mysql/MySQL-5.6 || DOWN_ADDR_MYSQL=http://cdn.mysql.com/Downloads/MySQL-5.6
  16. fi
  17. src_url=$DOWN_ADDR_MYSQL/mysql-${mysql_5_6_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_6_version}-linux-glibc2.5-${SYS_BIT_b}.tar.gz
  23. mv mysql-${mysql_5_6_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_6_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. chmod +x /etc/init.d/mysqld
  40. [ "$OS" == 'CentOS' ] && { chkconfig --add mysqld; chkconfig mysqld on; }
  41. [[ $OS =~ ^Ubuntu$|^Debian$ ]] && update-rc.d mysqld defaults
  42. cd ..
  43. # my.cnf
  44. [ -d "/etc/mysql" ] && /bin/mv /etc/mysql{,_bk}
  45. cat > /etc/my.cnf << EOF
  46. [client]
  47. port = 3306
  48. socket = /tmp/mysql.sock
  49. default-character-set = utf8mb4
  50. [mysql]
  51. prompt="MySQL [\\d]> "
  52. no-auto-rehash
  53. [mysqld]
  54. port = 3306
  55. socket = /tmp/mysql.sock
  56. basedir = $mysql_install_dir
  57. datadir = $mysql_data_dir
  58. pid-file = $mysql_data_dir/mysql.pid
  59. user = mysql
  60. bind-address = 0.0.0.0
  61. server-id = 1
  62. init-connect = 'SET NAMES utf8mb4'
  63. character-set-server = utf8mb4
  64. skip-name-resolve
  65. #skip-networking
  66. back_log = 300
  67. max_connections = 1000
  68. max_connect_errors = 6000
  69. open_files_limit = 65535
  70. table_open_cache = 128
  71. max_allowed_packet = 500M
  72. binlog_cache_size = 1M
  73. max_heap_table_size = 8M
  74. tmp_table_size = 16M
  75. read_buffer_size = 2M
  76. read_rnd_buffer_size = 8M
  77. sort_buffer_size = 8M
  78. join_buffer_size = 8M
  79. key_buffer_size = 4M
  80. thread_cache_size = 8
  81. query_cache_type = 1
  82. query_cache_size = 8M
  83. query_cache_limit = 2M
  84. ft_min_word_len = 4
  85. log_bin = mysql-bin
  86. binlog_format = mixed
  87. expire_logs_days = 7
  88. log_error = $mysql_data_dir/mysql-error.log
  89. slow_query_log = 1
  90. long_query_time = 1
  91. slow_query_log_file = $mysql_data_dir/mysql-slow.log
  92. performance_schema = 0
  93. explicit_defaults_for_timestamp
  94. #lower_case_table_names = 1
  95. skip-external-locking
  96. default_storage_engine = InnoDB
  97. innodb_file_per_table = 1
  98. innodb_open_files = 500
  99. innodb_buffer_pool_size = 64M
  100. innodb_write_io_threads = 4
  101. innodb_read_io_threads = 4
  102. innodb_thread_concurrency = 0
  103. innodb_purge_threads = 1
  104. innodb_flush_log_at_trx_commit = 2
  105. innodb_log_buffer_size = 2M
  106. innodb_log_file_size = 32M
  107. innodb_log_files_in_group = 3
  108. innodb_max_dirty_pages_pct = 90
  109. innodb_lock_wait_timeout = 120
  110. bulk_insert_buffer_size = 8M
  111. myisam_sort_buffer_size = 8M
  112. myisam_max_sort_file_size = 10G
  113. myisam_repair_threads = 1
  114. interactive_timeout = 28800
  115. wait_timeout = 28800
  116. [mysqldump]
  117. quick
  118. max_allowed_packet = 500M
  119. [myisamchk]
  120. key_buffer_size = 8M
  121. sort_buffer_size = 8M
  122. read_buffer = 4M
  123. write_buffer = 4M
  124. EOF
  125. sed -i "s@max_connections.*@max_connections = $(($Mem/2))@" /etc/my.cnf
  126. if [ $Mem -gt 1500 -a $Mem -le 2500 ];then
  127. sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf
  128. sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf
  129. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf
  130. sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf
  131. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf
  132. sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf
  133. sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf
  134. elif [ $Mem -gt 2500 -a $Mem -le 3500 ];then
  135. sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf
  136. sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf
  137. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf
  138. sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf
  139. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf
  140. sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf
  141. sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf
  142. elif [ $Mem -gt 3500 ];then
  143. sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf
  144. sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf
  145. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf
  146. sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf
  147. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf
  148. sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf
  149. sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf
  150. fi
  151. $mysql_install_dir/scripts/mysql_install_db --user=mysql --basedir=$mysql_install_dir --datadir=$mysql_data_dir
  152. chown mysql.mysql -R $mysql_data_dir
  153. [ -d '/etc/mysql' ] && mv /etc/mysql{,_bk}
  154. service mysqld start
  155. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=$mysql_install_dir/bin:\$PATH" >> /etc/profile
  156. [ -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
  157. . /etc/profile
  158. $mysql_install_dir/bin/mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"$dbrootpwd\" with grant option;"
  159. $mysql_install_dir/bin/mysql -e "grant all privileges on *.* to root@'localhost' identified by \"$dbrootpwd\" with grant option;"
  160. $mysql_install_dir/bin/mysql -uroot -p$dbrootpwd -e "delete from mysql.user where Password='';"
  161. $mysql_install_dir/bin/mysql -uroot -p$dbrootpwd -e "delete from mysql.db where User='';"
  162. $mysql_install_dir/bin/mysql -uroot -p$dbrootpwd -e "delete from mysql.proxies_priv where Host!='localhost';"
  163. $mysql_install_dir/bin/mysql -uroot -p$dbrootpwd -e "drop database test;"
  164. $mysql_install_dir/bin/mysql -uroot -p$dbrootpwd -e "reset master;"
  165. rm -rf /etc/ld.so.conf.d/{mysql,mariadb,percona}*.conf
  166. echo "$mysql_install_dir/lib" > /etc/ld.so.conf.d/mysql.conf
  167. [ -e "$mysql_install_dir/my.cnf" ] && rm -rf $mysql_install_dir/my.cnf
  168. ldconfig
  169. service mysqld stop
  170. }