mysql-5.7.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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-7() {
  11. cd $oneinstack_dir/src
  12. [ "$IPADDR_STATE"x == "CN"x ] && { DOWN_ADDR_MYSQL=http://mirrors.linuxeye.com/oneinstack/src; DOWN_ADDR_BOOST=$DOWN_ADDR_MYSQL; } || { DOWN_ADDR_MYSQL=http://cdn.mysql.com/Downloads/MySQL-5.7; DOWN_ADDR_BOOST=http://downloads.sourceforge.net/project/boost/boost/1.59.0; }
  13. if [ ! -e "/usr/local/lib/libboost_system.so" ];then
  14. src_url=$DOWN_ADDR_BOOST/boost_1_59_0.tar.gz && Download_src
  15. tar xzf boost_1_59_0.tar.gz
  16. cd boost_1_59_0
  17. ./bootstrap.sh
  18. ./bjam --prefix=/usr/local
  19. ./b2 install
  20. cd ..
  21. fi
  22. echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
  23. ldconfig
  24. if [ "$je_tc_malloc" == '1' ];then
  25. EXE_LINKER="-DCMAKE_EXE_LINKER_FLAGS='-ljemalloc'"
  26. elif [ "$je_tc_malloc" == '2' ];then
  27. EXE_LINKER="-DCMAKE_EXE_LINKER_FLAGS='-ltcmalloc'"
  28. fi
  29. src_url=$DOWN_ADDR_MYSQL/mysql-$mysql_5_7_version.tar.gz && Download_src
  30. id -u mysql >/dev/null 2>&1
  31. [ $? -ne 0 ] && useradd -M -s /sbin/nologin mysql
  32. [ ! -d "$mysql_install_dir" ] && mkdir -p $mysql_install_dir
  33. mkdir -p $mysql_data_dir;chown mysql.mysql -R $mysql_data_dir
  34. tar zxf mysql-$mysql_5_7_version.tar.gz
  35. cd mysql-$mysql_5_7_version
  36. make clean
  37. cmake . -DCMAKE_INSTALL_PREFIX=$mysql_install_dir \
  38. -DMYSQL_DATADIR=$mysql_data_dir \
  39. -DSYSCONFDIR=/etc \
  40. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  41. -DWITH_PARTITION_STORAGE_ENGINE=1 \
  42. -DWITH_FEDERATED_STORAGE_ENGINE=1 \
  43. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  44. -DWITH_MYISAM_STORAGE_ENGINE=1 \
  45. -DENABLED_LOCAL_INFILE=1 \
  46. -DENABLE_DTRACE=0 \
  47. -DDEFAULT_CHARSET=utf8mb4 \
  48. -DDEFAULT_COLLATION=utf8mb4_general_ci \
  49. -DWITH_EMBEDDED_SERVER=1 \
  50. $EXE_LINKER
  51. make -j `grep processor /proc/cpuinfo | wc -l`
  52. make install
  53. if [ -d "$mysql_install_dir/support-files" ];then
  54. echo "${CSUCCESS}MySQL install successfully! ${CEND}"
  55. cd ..
  56. rm -rf mysql-$mysql_5_7_version
  57. else
  58. rm -rf $mysql_install_dir
  59. echo "${CFAILURE}MySQL install failed, Please contact the author! ${CEND}"
  60. kill -9 $$
  61. fi
  62. /bin/cp $mysql_install_dir/support-files/mysql.server /etc/init.d/mysqld
  63. chmod +x /etc/init.d/mysqld
  64. [ "$OS" == 'CentOS' ] && { chkconfig --add mysqld; chkconfig mysqld on; }
  65. [[ $OS =~ ^Ubuntu$|^Debian$ ]] && update-rc.d mysqld defaults
  66. cd ..
  67. # my.cnf
  68. [ -d "/etc/mysql" ] && /bin/mv /etc/mysql{,_bk}
  69. cat > /etc/my.cnf << EOF
  70. [client]
  71. port = 3306
  72. socket = /tmp/mysql.sock
  73. default-character-set = utf8mb4
  74. [mysql]
  75. prompt="MySQL [\\d]> "
  76. no-auto-rehash
  77. [mysqld]
  78. port = 3306
  79. socket = /tmp/mysql.sock
  80. basedir = $mysql_install_dir
  81. datadir = $mysql_data_dir
  82. pid-file = $mysql_data_dir/mysql.pid
  83. user = mysql
  84. bind-address = 0.0.0.0
  85. server-id = 1
  86. init-connect = 'SET NAMES utf8mb4'
  87. character-set-server = utf8mb4
  88. skip-name-resolve
  89. #skip-networking
  90. back_log = 300
  91. max_connections = 1000
  92. max_connect_errors = 6000
  93. open_files_limit = 65535
  94. table_open_cache = 128
  95. max_allowed_packet = 4M
  96. binlog_cache_size = 1M
  97. max_heap_table_size = 8M
  98. tmp_table_size = 16M
  99. read_buffer_size = 2M
  100. read_rnd_buffer_size = 8M
  101. sort_buffer_size = 8M
  102. join_buffer_size = 8M
  103. key_buffer_size = 4M
  104. thread_cache_size = 8
  105. query_cache_type = 1
  106. query_cache_size = 8M
  107. query_cache_limit = 2M
  108. ft_min_word_len = 4
  109. log_bin = mysql-bin
  110. binlog_format = mixed
  111. expire_logs_days = 7
  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. myisam_repair_threads = 1
  139. interactive_timeout = 28800
  140. wait_timeout = 28800
  141. [mysqldump]
  142. quick
  143. max_allowed_packet = 16M
  144. [myisamchk]
  145. key_buffer_size = 8M
  146. sort_buffer_size = 8M
  147. read_buffer = 4M
  148. write_buffer = 4M
  149. EOF
  150. sed -i "s@max_connections.*@max_connections = $(($Mem/2))@" /etc/my.cnf
  151. if [ $Mem -gt 1500 -a $Mem -le 2500 ];then
  152. sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf
  153. sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf
  154. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf
  155. sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf
  156. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf
  157. sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf
  158. sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf
  159. elif [ $Mem -gt 2500 -a $Mem -le 3500 ];then
  160. sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf
  161. sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf
  162. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf
  163. sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf
  164. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf
  165. sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf
  166. sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf
  167. elif [ $Mem -gt 3500 ];then
  168. sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf
  169. sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf
  170. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf
  171. sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf
  172. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf
  173. sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf
  174. sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf
  175. fi
  176. $mysql_install_dir/bin/mysqld --initialize-insecure --user=mysql --basedir=$mysql_install_dir --datadir=$mysql_data_dir
  177. chown mysql.mysql -R $mysql_data_dir
  178. [ -d '/etc/mysql' ] && mv /etc/mysql{,_bk}
  179. service mysqld start
  180. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=$mysql_install_dir/bin:\$PATH" >> /etc/profile
  181. [ -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
  182. . /etc/profile
  183. $mysql_install_dir/bin/mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"$dbrootpwd\" with grant option;"
  184. $mysql_install_dir/bin/mysql -e "grant all privileges on *.* to root@'localhost' identified by \"$dbrootpwd\" with grant option;"
  185. $mysql_install_dir/bin/mysql -uroot -p$dbrootpwd -e "reset master;"
  186. rm -rf /etc/ld.so.conf.d/{mysql,mariadb,percona}*.conf
  187. [ -e "$mysql_install_dir/my.cnf" ] && rm -rf $mysql_install_dir/my.cnf
  188. echo "$mysql_install_dir/lib" > /etc/ld.so.conf.d/mysql.conf
  189. ldconfig
  190. service mysqld stop
  191. }