mysql 5.7.11编译安装

Posted on Posted in mysql
Tips: 本文创建于2016年3月16日,已超过 2 年,内容或图片可能已经失效!

系统环境

  1. CentOS release 6.5 (Final)  
  2. Kernel \r on an \m  

1、安装依赖

  1. yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison python-devel bzip2-devel  

2、安装 Boost

  1. tar zxvf boost_1_59_0.tar.gz  
  2. cd boost_1_59_0  
  3. ./bootstrap.sh  
  4. ./b2  
  5. ./b2 install  

3、安装 jemalloc (用于php环境的资源优化)

  1. tar jxf jemalloc-4.0.4.tar.bz2  
  2. cd jemalloc-4.0.4  
  3. ./configure  
  4. make && make install  
  5. ldconfig  

为了防止ldconfig没有找到so文件

  1. find / -name libjemalloc.so.2  
  2. echo "/usr/local/lib/"(上一条命令查询的路径) > /etc/ld.so.conf.d/jemalloc.conf  
  3. ldconfig  

4、编译mysql

  1. tar zxvf mysql-5.7.11.tar.gz  
  2. cd mysql-5.7.11  
  3. cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DCMAKE_EXE_LINKER_FLAGS='-ljemalloc' -DWITH_SAFEMALLOC=OFF  
  4. make && make install  
  5.   
  6. groupadd mysql  
  7. useradd -s /sbin/nologin -M -g mysql mysql  
  8.   
  9. cat > /etc/my.cnf<<EOF  
  10. [client]  
  11. #password   = your_password  
  12. port        = 3306  
  13. socket      = /tmp/mysql.sock  
  14.   
  15. [mysqld]  
  16. port        = 3306  
  17. socket      = /tmp/mysql.sock  
  18. datadir = /usr/local/mysql/var  
  19. skip-external-locking  
  20. key_buffer_size = 16M  
  21. max_allowed_packet = 1M  
  22. table_open_cache = 64  
  23. sort_buffer_size = 512K  
  24. net_buffer_length = 8K  
  25. read_buffer_size = 256K  
  26. read_rnd_buffer_size = 512K  
  27. myisam_sort_buffer_size = 8M  
  28. thread_cache_size = 8  
  29. query_cache_size = 8M  
  30. tmp_table_size = 16M  
  31. table_open_cache = 64  
  32.   
  33. explicit_defaults_for_timestamp = true  
  34. #skip-networking  
  35. max_connections = 500  
  36. max_connect_errors = 100  
  37. open_files_limit = 65535  
  38.   
  39. log-bin=mysql-bin  
  40. binlog_format=mixed  
  41. server-id   = 1  
  42. expire_logs_days = 10  
  43.   
  44. #loose-innodb-trx=0  
  45. #loose-innodb-locks=0  
  46. #loose-innodb-lock-waits=0  
  47. #loose-innodb-cmp=0  
  48. #loose-innodb-cmp-per-index=0  
  49. #loose-innodb-cmp-per-index-reset=0  
  50. #loose-innodb-cmp-reset=0  
  51. #loose-innodb-cmpmem=0  
  52. #loose-innodb-cmpmem-reset=0  
  53. #loose-innodb-buffer-page=0  
  54. #loose-innodb-buffer-page-lru=0  
  55. #loose-innodb-buffer-pool-stats=0  
  56. #loose-innodb-metrics=0  
  57. #loose-innodb-ft-default-stopword=0  
  58. #loose-innodb-ft-inserted=0  
  59. #loose-innodb-ft-deleted=0  
  60. #loose-innodb-ft-being-deleted=0  
  61. #loose-innodb-ft-config=0  
  62. #loose-innodb-ft-index-cache=0  
  63. #loose-innodb-ft-index-table=0  
  64. #loose-innodb-sys-tables=0  
  65. #loose-innodb-sys-tablestats=0  
  66. #loose-innodb-sys-indexes=0  
  67. #loose-innodb-sys-columns=0  
  68. #loose-innodb-sys-fields=0  
  69. #loose-innodb-sys-foreign=0  
  70. #loose-innodb-sys-foreign-cols=0  
  71.   
  72. default_storage_engine = InnoDB  
  73. #innodb_data_home_dir = /usr/local/mysql/var  
  74. #innodb_data_file_path = ibdata1:10M:autoextend  
  75. #innodb_log_group_home_dir = /usr/local/mysql/var  
  76. #innodb_buffer_pool_size = 16M  
  77. #innodb_log_file_size = 5M  
  78. #innodb_log_buffer_size = 8M  
  79. #innodb_flush_log_at_trx_commit = 1  
  80. #innodb_lock_wait_timeout = 50  
  81.   
  82. [mysqldump]  
  83. quick  
  84. max_allowed_packet = 16M  
  85.   
  86. [mysql]  
  87. no-auto-rehash  
  88.   
  89. [myisamchk]  
  90. key_buffer_size = 20M  
  91. sort_buffer_size = 20M  
  92. read_buffer = 2M  
  93. write_buffer = 2M  
  94.   
  95. [mysqlhotcopy]  
  96. interactive-timeout  
  97. EOF  
  98.   
  99. sed -i 's:^#innodb:innodb:g' /etc/my.cnf  
  100.   
  101. mkdir -p /usr/local/mysql/var  
  102. chown -R mysql:mysql /usr/local/mysql/var  
  103.   
  104. /usr/local/mysql/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql  
  105.   
  106. chgrp -R mysql /usr/local/mysql/.  
  107. \cp support-files/mysql.server(在解压的mysql包中) /etc/init.d/mysql  
  108. chmod 755 /etc/init.d/mysql  
  109.   
  110. cat > /etc/ld.so.conf.d/mysql.conf<<EOF  
  111.     /usr/local/mysql/lib  
  112.     /usr/local/lib  
  113. EOF  
  114.   
  115. ldconfig  
  116. ln -sf /usr/local/mysql/lib/mysql /usr/lib/mysql  
  117. ln -sf /usr/local/mysql/include/mysql /usr/include/mysql  
  118.   
  119. /etc/init.d/mysql start  
  120.   
  121. ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql  
  122. ln -sf /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump  
  123. ln -sf /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk  
  124. ln -sf /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe  
  125. ln -sf /usr/local/mysql/bin/mysqlcheck /usr/bin/mysqlcheck  
  126.   
  127. /usr/local/mysql/bin/mysqladmin -u root password "自定义密码"  
  128. /etc/init.d/mysql restart  
  129. (sql)UPDATE mysql.user SET authentication_string=PASSWORD('自定义密码') WHERE User='root';  
  130. (sql)FLUSH PRIVILEGES;  
  131. /etc/init.d/mysql restart  

5、添加libjemalloc.so

  1. vim /usr/local/mysql/bin/mysqld_safe  
  2. # executing mysqld_safe  
  3. LD_PRELOAD=/usr/local/lib/libjemalloc.so  

验证 jemalloc 是否运行

  1. [root@localhost ~]# lsof -n | grep jemalloc  
  2. mysqld      3880   mysql  mem       REG              253,0   5895347    1059344 /usr/local/lib/libjemalloc.so.2  

6、一个mysql的参数优化

  1. MemTotal=`free -m | grep Mem | awk '{print  $2}'`  
  2. MySQL_Opt()  
  3. {  
  4.     if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then  
  5.         sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf  
  6.         sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf  
  7.         sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf  
  8.         sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf  
  9.         sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf  
  10.         sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf  
  11.         sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf  
  12.         sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf  
  13.         sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf  
  14.         sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 32M#" /etc/my.cnf  
  15.     elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then  
  16.         sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf  
  17.         sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf  
  18.         sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf  
  19.         sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf  
  20.         sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf  
  21.         sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf  
  22.         sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf  
  23.         sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf  
  24.         sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf  
  25.         sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf  
  26.     elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then  
  27.         sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf  
  28.         sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf  
  29.         sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf  
  30.         sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf  
  31.         sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf  
  32.         sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my
    .cnf  
  33.         sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf  
  34.         sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf  
  35.         sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf  
  36.         sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf  
  37.     elif [[ ${MemTotal} -ge 8192 && ${MemTotal} -lt 16384 ]]; then  
  38.         sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf  
  39.         sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf  
  40.         sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf  
  41.         sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf  
  42.         sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf  
  43.         sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf  
  44.         sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf  
  45.         sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf  
  46.         sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf  
  47.         sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf  
  48.     elif [[ ${MemTotal} -ge 16384 && ${MemTotal} -lt 32768 ]]; then  
  49.         sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" /etc/my.cnf  
  50.         sed -i "s#^table_open_cache.*#table_open_cache = 2048#" /etc/my.cnf  
  51.         sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" /etc/my.cnf  
  52.         sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" /etc/my.cnf  
  53.         sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" /etc/my.cnf  
  54.         sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" /etc/my.cnf  
  55.         sed -i "s#^query_cache_size.*#query_cache_size = 256M#" /etc/my.cnf  
  56.         sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" /etc/my.cnf  
  57.         sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" /etc/my.cnf  
  58.         sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 512M#" /etc/my.cnf  
  59.     elif [[ ${MemTotal} -ge 32768 ]]; then  
  60.         sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" /etc/my.cnf  
  61.         sed -i "s#^table_open_cache.*#table_open_cache = 4096#" /etc/my.cnf  
  62.         sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" /etc/my.cnf  
  63.         sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" /etc/my.cnf  
  64.         sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" /etc/my.cnf  
  65.         sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" /etc/my.cnf  
  66.         sed -i "s#^query_cache_size.*#query_cache_size = 512M#" /etc/my.cnf  
  67.         sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" /etc/my.cnf  
  68.         sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" /etc/my.cnf  
  69.         sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf  
  70.     fi  
  71. }  
» 转载请注明来源:若我若鱼 » mysql 5.7.11编译安装

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × 1 =