原码笔记

原码笔记

MySQL 5.7.13 源码编译安装配置方法图文教程

小诸哥 0

安装环境:CentOS7 64位 MINI版

官网源码编译安装文档:http://dev.mysql.com/doc/refman/5.7/en/source-installation.html

一、系统安装条件

官方文档说明:http://dev.mysql.com/doc/refman/5.7/en/source-installation.html

1> cmake

MySQL使用cmake跨平台工具预编译源码,用于设置mysql的编译参数。如:安装目录、数据存放目录、字符编码、排序规则等。安装最新版本即可。

2> make3.75

mysql源代码是由C和C++语言编写,在linux下使用make对源码进行编译和构建,要求必须安装make 3.75或以上版本

3> gcc4.4.6

GCC是Linux下的C语言编译工具,mysql源码编译完全由C和C++编写,要求必须安装GCC4.4.6或以上版本

4> Boost1.59.0

mysql源码中用到了C++的Boost库,要求必须安装boost1.59.0或以上版本

5> bison2.1

Linux下C/C++语法分析器

6> ncurses

字符终端处理库

所以在安装前,需先安装相关的依赖库:

  1. shell> sudo yum install -y cmake,make,gcc,gcc-c++,bison, ncurses,ncurses-devel
  2.  
  3.  

下载Boost1.59.0源代码,并解压到/usr/local/目录下:

  1. shell> wget -O https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
  2. shell> tar -zxvf boost_1_59_0.tar.gz -/usr/local/
  3.  
  4.  

二、下载MySQL源码

从github上下载mysql的源码

  1. shell> cd /opt
  2. shell> git clone https://github.com/mysql/mysql-server.git
  3. shell> ls mysql-server

如果没安装git客户端,执行yum install -y git安装。

  1. shell> git branch -r
  2. origin/5.5
  3. origin/5.6
  4. origin/5.7
  5. origin/HEAD -> origin/5.7
  6. origin/cluster-7.2
  7. origin/cluster-7.3
  8. origin/cluster-7.4
  9. origin/cluster-7.5
  10.  
  11.  

当前分支默认为5.7版本,如果要安装其它版本,切换到相应的分支即可。如安装5.6版本:git checkout 5.6,这里以安装5.7为例。

搜狐镜像下载地址:
http://mirrors.sohu.com/mysql/MySQL-5.5/
http://mirrors.sohu.com/mysql/MySQL-5.6/
http://mirrors.sohu.com/mysql/MySQL-5.7/

三、安装

1> 添加mysql用户

  1. shell>; cd /opt/mysql-server
  2. shell> groupadd mysql #添加mysql用户组
  3. shell> useradd --g mysql -/bin/false mysql #添加mysql用户
  4.  
  5.  

2> 配置mysql预编译参数

  1. shell> cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  2. -DMYSQL_DATADIR=/usr/local/mysql/data \
  3. -DWITH_BOOST=/usr/local/boost_1_59_0 \
  4. -DSYSCONFDIR=/etc \
  5. -DEFAULT_CHARSET=utf8mb4 \
  6. -DDEFAULT_COLLATION=utf8mb4_general_ci \
  7. -DENABLED_LOCAL_INFILE=1 \
  8. -DEXTRA_CHARSETS=all

-DCMAKE_INSTALL_PREFIX:安装路径
-DMYSQL_DATADIR:数据存放目录
-DWITH_BOOST:boost源码路径
-DSYSCONFDIR:my.cnf配置文件目录
-DEFAULT_CHARSET:数据库默认字符编码
-DDEFAULT_COLLATION:默认排序规则
-DENABLED_LOCAL_INFILE:允许从本文件导入数据
-DEXTRA_CHARSETS:安装所有字符集

更多预编译配置参数请参考mysql官方文档说明:http://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html#cmake-general-options

3> 编译并安装

  1. shell> make -`grep processor /proc/cpuinfo | wc -l`
  2. shell> make install

-j参数表示根据CPU核数指定编译时的线程数,可以加快编译速度。默认为1个线程编译,经测试单核CPU,1G的内存,编译完需要将近1个小时。

4> 初始化系统数据库

  1. shell> cd /usr/local/mysql
  2. shell> chown -R mysql:mysql .
  3. # 注意:MySQL 5.7.6之前的版本执行这个脚本初始化系统数据库
  4. shell> ./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
  5. # 5.7.6之后版本初始系统数据库脚本(本文使用此方式初始化)
  6. shell> ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
  7. shell> ./bin/mysql_ssl_rsa_setup
  8. shell> chown -R root .
  9. shell> chown -R mysql data

注意:如果使用–initialize参数初始化系统数据库之后,会在~/.mysql_secret文件中生成root用户的一个临时密码,同时也在初始化日志中打印出来了,如下图红圈中所示:


5、配置文件及参数优化

  1. shell> cp support-files/my-default.cnf /etc/my.cnf
  2. shell> vim /etc/my.cnf
  3.  
  4. [client]
  5. port=3306
  6. socket=/usr/local/mysql/mysql.sock
  7. [mysqld]
  8. character-set-server=utf8
  9. collation-server=utf8_general_ci
  10.  
  11. skip-external-locking
  12. skip-name-resolve
  13.  
  14. user=mysql
  15. port=3306
  16. basedir=/usr/local/mysql
  17. datadir=/usr/local/mysql/data
  18. tmpdir=/usr/local/mysql/temp
  19. # server_id = .....
  20. socket=/usr/local/mysql/mysql.sock
  21. log-error=/usr/local/mysql/logs/mysql_error.log
  22. pid-file=/usr/local/mysql/mysql.pid
  23. open_files_limit=10240
  24. back_log=600
  25. max_connections=500
  26. max_connect_errors=6000
  27. wait_timeout=605800
  28. #open_tables=600
  29. #table_cache = 650
  30. #opened_tables = 630
  31.  
  32. max_allowed_packet=32M
  33. sort_buffer_size=4M
  34. join_buffer_size=4M
  35. thread_cache_size=300
  36. query_cache_type=1
  37. query_cache_size=256M
  38. query_cache_limit=2M
  39. query_cache_min_res_unit=16k
  40.  
  41. tmp_table_size=256M
  42. max_heap_table_size=256M
  43.  
  44. key_buffer_size=256M
  45. read_buffer_size=1M
  46. read_rnd_buffer_size=16M
  47. bulk_insert_buffer_size=64M
  48.  
  49. lower_case_table_names=1
  50.  
  51. default-storage-engine=INNODB
  52.  
  53. innodb_buffer_pool_size=2G
  54. innodb_log_buffer_size=32M
  55. innodb_log_file_size=128M
  56. innodb_flush_method=O_DIRECT
  57. #####################
  58. thread_concurrency=32
  59. long_query_time=2
  60. slow-query-log=on
  61. slow-query-log-file=/usr/local/mysql/logs/mysql-slow.log
  62.  
  63. [mysqldump]
  64. quick
  65. max_allowed_packet=32M
  66.  
  67. [mysqld_safe]
  68. log-error=/var/log/mysqld.log
  69. pid-file=/var/run/mysqld/mysqld.pid
  70.  

6、配置mysql服务

  1. shell> cp support-files/mysql.server /etc/init.d/mysqld
  2. shell> chkconfig --add mysqld # 添加到系统服务
  3. shell> chkconfig mysqld on # 开机启动

7、启动服务

  1. shell> service mysqld start # 启动mysql服务
  2. shell> service mysqld stop # 停止mysql服务
  3. shell> service mysqld restart # 重新启动mysql服务
  4.  
  5.  

8、设置数据库密码

  1. shell> /usr/local/mysql/bin/mysql -"grant all privileges on *.* to root@'127.0.0.1' identified by "root" with grant option;"
  2. shell> /usr/local/mysql/bin/mysql -"grant all privileges on *.* to root@'localhost' identified by "root" with grant option;"
  3. # 开启远程登录(将host设为%即可)
  4. /usr/local/mysql/bin/mysql -"grant all privileges on *.* to root@'%' identified by "root" with grant option;"
  5.  
  6.  

9、配置mysql环境变量

  1. shell> vim /etc/profile
  2. shell> export PATH=/usr/local/mysql/bin:$PATH
  3. shell> source /etc/profile
  4.  
  5.  

四、其它注意事项

如果中途编译失败了,需要删除cmake生成的预编译配置参数的缓存文件和make编译后生成的文件,再重新编译。

  1. shell> cd /opt/mysql-server
  2. shell> rm -CMakeCache.txt
  3. shell> make clean

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

标签: 源码编译