mysql-5.7.sh 7.7 KB

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