mariadb-10.0.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_MariaDB-10-0() {
  11. cd $oneinstack_dir/src
  12. [ "$IPADDR_STATE"x == "CN"x ] && DOWN_ADDR=http://mirrors.aliyun.com/mariadb || DOWN_ADDR=https://downloads.mariadb.org/f
  13. LIBC_VERSION=`getconf -a | grep GNU_LIBC_VERSION | awk '{print $NF}'`
  14. LIBC_YN=`echo "$LIBC_VERSION < 2.14" | bc`
  15. [ $LIBC_YN == '1' ] && GLIBC_FLAG=linux || GLIBC_FLAG=linux-glibc_214
  16. src_url=$DOWN_ADDR/mariadb-${mariadb_10_0_version}/bintar-${GLIBC_FLAG}-$SYS_BIT_a/mariadb-${mariadb_10_0_version}-${GLIBC_FLAG}-${SYS_BIT_b}.tar.gz && Download_src
  17. id -u mysql >/dev/null 2>&1
  18. [ $? -ne 0 ] && useradd -M -s /sbin/nologin mysql
  19. mkdir -p $mariadb_data_dir;chown mysql.mysql -R $mariadb_data_dir
  20. tar zxf mariadb-${mariadb_10_0_version}-${GLIBC_FLAG}-${SYS_BIT_b}.tar.gz
  21. [ ! -d "$mariadb_install_dir" ] && mkdir -p $mariadb_install_dir
  22. mv mariadb-${mariadb_10_0_version}-*-${SYS_BIT_b}/* $mariadb_install_dir
  23. if [ "$je_tc_malloc" == '1' ];then
  24. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libjemalloc.so@' $mariadb_install_dir/bin/mysqld_safe
  25. elif [ "$je_tc_malloc" == '2' ];then
  26. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libtcmalloc.so@' $mariadb_install_dir/bin/mysqld_safe
  27. fi
  28. if [ -d "$mariadb_install_dir/support-files" ];then
  29. echo "${CSUCCESS}MariaDB install successfully! ${CEND}"
  30. else
  31. rm -rf $mariadb_install_dir
  32. echo "${CFAILURE}MariaDB install failed, Please contact the author! ${CEND}"
  33. kill -9 $$
  34. fi
  35. /bin/cp $mariadb_install_dir/support-files/mysql.server /etc/init.d/mysqld
  36. sed -i "s@^basedir=.*@basedir=$mariadb_install_dir@" /etc/init.d/mysqld
  37. sed -i "s@^datadir=.*@datadir=$mariadb_data_dir@" /etc/init.d/mysqld
  38. chmod +x /etc/init.d/mysqld
  39. [ "$OS" == 'CentOS' ] && { chkconfig --add mysqld; chkconfig mysqld on; }
  40. [[ $OS =~ ^Ubuntu$|^Debian$ ]] && update-rc.d mysqld defaults
  41. cd ..
  42. # my.cnf
  43. [ -d "/etc/mysql" ] && /bin/mv /etc/mysql{,_bk}
  44. cat > /etc/my.cnf << EOF
  45. [client]
  46. port = 3306
  47. socket = /tmp/mysql.sock
  48. default-character-set = utf8mb4
  49. [mysqld]
  50. port = 3306
  51. socket = /tmp/mysql.sock
  52. basedir = $mariadb_install_dir
  53. datadir = $mariadb_data_dir
  54. pid-file = $mariadb_data_dir/mysql.pid
  55. user = mysql
  56. bind-address = 0.0.0.0
  57. server-id = 1
  58. init-connect = 'SET NAMES utf8mb4'
  59. character-set-server = utf8mb4
  60. skip-name-resolve
  61. #skip-networking
  62. back_log = 300
  63. max_connections = 1000
  64. max_connect_errors = 6000
  65. open_files_limit = 65535
  66. table_open_cache = 128
  67. max_allowed_packet = 4M
  68. binlog_cache_size = 1M
  69. max_heap_table_size = 8M
  70. tmp_table_size = 16M
  71. read_buffer_size = 2M
  72. read_rnd_buffer_size = 8M
  73. sort_buffer_size = 8M
  74. join_buffer_size = 8M
  75. key_buffer_size = 4M
  76. thread_cache_size = 8
  77. query_cache_type = 1
  78. query_cache_size = 8M
  79. query_cache_limit = 2M
  80. ft_min_word_len = 4
  81. log_bin = mysql-bin
  82. binlog_format = mixed
  83. expire_logs_days = 7
  84. log_error = $mariadb_data_dir/mysql-error.log
  85. slow_query_log = 1
  86. long_query_time = 1
  87. slow_query_log_file = $mariadb_data_dir/mysql-slow.log
  88. performance_schema = 0
  89. #lower_case_table_names = 1
  90. skip-external-locking
  91. default_storage_engine = InnoDB
  92. innodb_file_per_table = 1
  93. innodb_open_files = 500
  94. innodb_buffer_pool_size = 64M
  95. innodb_write_io_threads = 4
  96. innodb_read_io_threads = 4
  97. innodb_thread_concurrency = 0
  98. innodb_purge_threads = 1
  99. innodb_flush_log_at_trx_commit = 2
  100. innodb_log_buffer_size = 2M
  101. innodb_log_file_size = 32M
  102. innodb_log_files_in_group = 3
  103. innodb_max_dirty_pages_pct = 90
  104. innodb_lock_wait_timeout = 120
  105. bulk_insert_buffer_size = 8M
  106. myisam_sort_buffer_size = 8M
  107. myisam_max_sort_file_size = 10G
  108. myisam_repair_threads = 1
  109. interactive_timeout = 28800
  110. wait_timeout = 28800
  111. [mysqldump]
  112. quick
  113. max_allowed_packet = 16M
  114. [myisamchk]
  115. key_buffer_size = 8M
  116. sort_buffer_size = 8M
  117. read_buffer = 4M
  118. write_buffer = 4M
  119. EOF
  120. sed -i "s@max_connections.*@max_connections = $(($Mem/2))@" /etc/my.cnf
  121. if [ $Mem -gt 1500 -a $Mem -le 2500 ];then
  122. sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf
  123. sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf
  124. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf
  125. sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf
  126. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf
  127. sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf
  128. sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf
  129. elif [ $Mem -gt 2500 -a $Mem -le 3500 ];then
  130. sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf
  131. sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf
  132. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf
  133. sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf
  134. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf
  135. sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf
  136. sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf
  137. elif [ $Mem -gt 3500 ];then
  138. sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf
  139. sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf
  140. sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf
  141. sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf
  142. sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf
  143. sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf
  144. sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf
  145. fi
  146. $mariadb_install_dir/scripts/mysql_install_db --user=mysql --basedir=$mariadb_install_dir --datadir=$mariadb_data_dir
  147. chown mysql.mysql -R $mariadb_data_dir
  148. [ -d '/etc/mysql' ] && mv /etc/mysql{,_bk}
  149. service mysqld start
  150. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=$mariadb_install_dir/bin:\$PATH" >> /etc/profile
  151. [ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep $mariadb_install_dir /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=$mariadb_install_dir/bin:\1@" /etc/profile
  152. . /etc/profile
  153. $mariadb_install_dir/bin/mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"$dbrootpwd\" with grant option;"
  154. $mariadb_install_dir/bin/mysql -e "grant all privileges on *.* to root@'localhost' identified by \"$dbrootpwd\" with grant option;"
  155. $mariadb_install_dir/bin/mysql -uroot -p$dbrootpwd -e "delete from mysql.user where Password='';"
  156. $mariadb_install_dir/bin/mysql -uroot -p$dbrootpwd -e "delete from mysql.db where User='';"
  157. $mariadb_install_dir/bin/mysql -uroot -p$dbrootpwd -e "delete from mysql.proxies_priv where Host!='localhost';"
  158. $mariadb_install_dir/bin/mysql -uroot -p$dbrootpwd -e "drop database test;"
  159. $mariadb_install_dir/bin/mysql -uroot -p$dbrootpwd -e "reset master;"
  160. rm -rf /etc/ld.so.conf.d/{mysql,mariadb,percona}*.conf
  161. echo "$mariadb_install_dir/lib" > /etc/ld.so.conf.d/mariadb.conf
  162. ldconfig
  163. service mysqld stop
  164. }