写在最前面:

1. mysql的版本为 mysql-5.5.31,脚本运行环境在centos5.9 64bit通过.

2. 为什么不写lnmp一键安装的了,而单单拆分了,我也不知道.

3. 当然,如果你有任何你解决不了的问题,随时联系我.




 
  
  1. #!/bin/bash

  2. #version 1.0

  3. #date    2013-03-21

  4. #author  yangcan

  5. #mail    yoncan@qq.com

  6. _work_path=$(pwd)

  7. _mysql_path=/usr/local/mysql

  8. _mysql_data_path=/home/var

  9. _mysql_passwd=yangcan

  10. _user=mysql

  11. _cmake_soft="cmake-2.8.10.2"

  12. _bison_soft="bison-2.7"

  13. _mysql_soft="mysql-5.5.31"

  14. _CHECK_STATS() {

  15.        if [ $? == 0 ];then

  16.                echo "*************************************" >>$_work_path/install.log

  17.                echo "$1 : at $(date +'%F %T') install OK "  >>$_work_path/install.log

  18. else

  19.                echo "install mysql error. please cat $_work_path/install.log "

  20.                echo "*************************************" >>$_work_path/install.log

  21.                echo "$1 : at $(date +'%F %T') install               <<FAIL>> "  >>$_work_path/install.log

  22.                exit 1

  23.        fi

  24. }

  25. CHECK_FILE() {

  26. ( yum grouplist|grep -B 100 "Available Groups:"|grep "Development Libraries" ) || yum groupinstall "Development Libraries"

  27. ( yum grouplist|grep -B 100 "Available Groups:"|grep "Development Tools" ) || yum groupinstall "Development Tools"

  28. yum install gcc gcc-c++ ncurses-devel make wget -y

  29. [ -s $_cmake_soft.tar.gz ] || wget  http://www.cmake.org/files/v2.8/$_cmake_soft.tar.gz

  30. [ -s $_bison_soft.tar.gz ] || wget  http://mirror.bjtu.edu.cn/gnu/bison/$_bison_soft.tar.gz

  31. [ -s $_mysql_soft.tar.gz ] || wget  http://cdn.mysql.com/Downloads/MySQL-5.5/$_mysql_soft.tar.gz

  32. }

  33. INSTALL_DEPTH() {

  34. cd $_work_path

  35. tar -zxf $_cmake_soft.tar.gz

  36. cd ${_cmake_soft}

  37. ./bootstrap

  38. make && make install

  39. _CHECK_STATS $_cmake_soft

  40. cd $_work_path

  41. tar -zxf $_bison_soft.tar.gz

  42. cd ${_bison_soft}

  43. ./configure

  44. make && make install

  45. _CHECK_STATS $_bison_soft

  46. }

  47. INSTALL_MYSQL() {

  48. cd $_work_path

  49. tar -zxf $_mysql_soft.tar.gz

  50. [ $(id mysql > /dev/null 2>&1) ] || useradd -M -r -s /sbin/nologin mysql

  51. cd ${_mysql_soft}

  52. cmake -DCMAKE_INSTALL_PREFIX=$_mysql_path  \

  53.    -DSYSCONFDIR=/etc \

  54.    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

  55.    -DDEFAULT_CHARSET=utf8 \

  56.    -DDEFAULT_COLLATION=utf8_general_ci \

  57.    -DWITH_EXTRA_CHARSETS=all \

  58.    -DWITH_MYISAM_STORAGE_ENGINE=1 \

  59.    -DWITH_INNOBASE_STORAGE_ENGINE=1 \

  60.    -DWITH_FEDERATED_STORAGE_ENGINE=1 \

  61.    -DWITH_PARTITION_STORAGE_ENGINE=1 \

  62.    -DWITH_READLINE=1 \

  63.    -DENABLED_LOCAL_INFILE=1 \

  64.    -DMYSQL_DATADIR=$_mysql_data_path \

  65.    -DWITH_DEBUG=0

  66. _CHECK_STATS mysql_config

  67. make  && make install

  68. _CHECK_STATS mysql_make

  69. }

  70. CONFIG_MYSQL() {

  71. cd ${_mysql_soft}

  72. [ ! -d  $_mysql_data_path ] && mkdir -p $_mysql_data_path

  73. [ id mysql >/dev/null 2>&1 ] || useradd -M -r -s /sbin/nologin $_user

  74. chown -R $_user:$_user $_mysql_data_path

  75. chown -R $_user:$_user $_mysql_path

  76. echo  "$_mysql_path/lib"  >>/etc/ld.so.conf && ldconfig

  77. ln -s $_mysql_path/bin/* /usr/local/bin/

  78. cp -a support-files/mysql.server /etc/init.d/mysqld && chmod a+x /etc/init.d/mysqld

  79. sed -i "s#\(^basedir=\).*#\1$_mysql_path#g" /etc/init.d/mysqld  

  80. sed -i "s#\(^datadir=\).*#\1$_mysql_data_path#g" /etc/init.d/mysqld

  81. chkconfig --add mysqld

  82. chkconfig  mysqld on

  83. [ -f /etc/my.cnf ] && mv /etc/my.cnf{,.bak}

  84. cat > /etc/my.cnf <<END_OF

  85. [client]

  86. port                    = 3306

  87. socket                  = /tmp/mysql.sock

  88. [mysqld]

  89. port                    = 3306

  90. socket                  = /tmp/mysql.sock

  91. character-set-server    = utf8

  92. basedir                 = ${_mysql_path}

  93. datadir                 = ${_mysql_data_path}

  94. general_log_file        = ${_mysql_data_path}/mysqld.log

  95. pid-file                = ${_mysql_data_path}/mysqld.pid

  96. log-error               = ${_mysql_data_path}/mysqld.err

  97. user                    = mysql

  98. default-storage-engine  = MyISAM

  99. key_buffer_size         = 256M

  100. max_allowed_packet      = 20M

  101. table_open_cache        = 256

  102. sort_buffer_size        = 1M

  103. read_buffer_size        = 1M

  104. join-buffer-size        = 1M

  105. read_rnd_buffer_size    = 4M

  106. thread-cache-size       = 300

  107. query-cache-size        = 512M

  108. query-cache-limit       = 2M

  109. query-cache-min-res-unit = 2k

  110. transaction-isolation   = REPEATABLE-READ

  111. thread-stack            = 192K

  112. tmp-table-size          = 256M

  113. max-heap-table-size     = 256M

  114. long-query-time         = 3

  115. skip-name-resolve

  116. open-files-limit        = 10240

  117. table-cache             = 614

  118. table-open-cache        = 600

  119. back-log                = 500

  120. max-connections         = 5000

  121. max-connect-errors      = 100

  122. max-user-connections    = 100

  123. external-locking        = FALSE

  124. interactive-timeout     = 120

  125. wait-timeout            = 120

  126. # myisam engine

  127. myisam_sort_buffer_size = 64M

  128. myisam-sort-buffer-size = 128M

  129. myisam-max-sort-file-size = 10G

  130. myisam-repair-threads   = 2

  131. myisam-recover-options

  132. # Try number of CPU's*2 for thread_concurrency

  133. thread_concurrency = 4

  134. server-id               = 1

  135. log-bin                 = mysql-bin

  136. expire-logs-days        = 30

  137. binlog-cache-size       = 4M

  138. binlog-format           = MIXED

  139. max-binlog-cache-size   = 8M

  140. max-binlog-size         = 1G

  141. # Uncomment the following if you are using InnoDB tables

  142. #innodb_data_home_dir            = ${_mysql_data_path}

  143. #innodb_data_file_path           = ibdata1:10M:autoextend

  144. #innodb_log_group_home_dir       = ${_mysql_data_path}

  145. # You can set .._buffer_pool_size up to 50 - 80 %

  146. # of RAM but beware of setting memory usage too high

  147. #innodb_buffer_pool_size = 256M

  148. #innodb_additional_mem_pool_size = 20M

  149. # Set .._log_file_size to 25 % of buffer pool size

  150. #innodb_log_file_size            = 64M

  151. #innodb_log_buffer_size          = 8M

  152. #innodb-log-files-in-group       = 3

  153. #innodb_flush_log_at_trx_commit  = 2

  154. #innodb_lock_wait_timeout        = 50

  155. #innodb-file-io-threads          = 4

  156. #innodb-additional-mem-pool-size = 16M

  157. #innodb-max-dirty-pages-pct      = 90

  158. #innodb-lock-wait-timeout        = 120

  159. #innodb-file-per-table           = FALSE

  160. [mysqldump]

  161. quick

  162. max_allowed_packet      = 16M

  163. [mysql]

  164. no-auto-rehash

  165. [myisamchk]

  166. key_buffer_size         = 128M

  167. sort_buffer_size        = 128M

  168. read_buffer             = 2M

  169. write_buffer            = 2M

  170. [mysqlhotcopy]

  171. interactive-timeout

  172. [safe_mysqld]

  173. err-log                 = ${_mysql_data_path}/mysqld.log

  174. END_OF

  175. $_mysql_path/scripts/mysql_install_db --defaults-file=/etc/my.cnf  --basedir=$_mysql_path --datadir=$_mysql_data_path  

  176. /etc/init.d/mysqld start

  177. $_mysql_path/bin/mysqladmin -u root password"$_mysql_passwd"

  178. /etc/init.d/mysqld restart

  179. $_mysql_path/bin/mysql -uroot -p$_mysql_passwd -e "delete from mysql.user where Password=' '"

  180. _CHECK_STATS mysql_configure

  181. }

  182. if [ `id -u` -ne 0 ];then

  183.    echo "install mysql need root"

  184.    exit 1

  185. else

  186.    CHECK_FILE

  187.    INSTALL_DEPTH

  188.    INSTALL_MYSQL

  189.    CONFIG_MYSQL

  190.    [ $? -eq 0 ] && echo " $(date +"%F %T")  @@@@@@@ MYSQL COMPLETE  @@@@@@ " >> $_work_path/install.log

  191.    sleep 5

  192. fi