percona-5.7.sh 7.9 KB

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