mysql-8.0.sh 7.5 KB

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