mysql-8.2.sh 7.8 KB

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