源码包编译安装mysql & cmake

![mark](http://oqwym6cik.bkt.clouddn.com/blog/20170817/192022449.jpg)

## 源码包安装mysql
环境:  
OS:centos 7 _ x86_64  
MySQL:mysql-5.6.35

### 安装准备  
因为从MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具,所以,首先要在系统中安装cmake工具。
#### 安装cmake工具
安装方法:

```
yum快速安装:
[root@adai003 src]# yum install -y cmake

源码包安装:
[root@adai003 src]# wget https://cmake.org/files/v3.9/cmake-3.9.1.tar.gz
[root@adai003 src]# tar zxf cmake-3.9.1.tar.gz
[root@adai003 src]# cd cmake-3.9.1/
[root@adai003 cmake-3.9.1]# ./bootstrap --prefix=/home/bnu/cmake
[root@adai003 cmake-3.9.1]# make && make install

添加cmake命令到系统环境变量:
[root@adai003 local]# vim ~/.bash_profile
……
export PATH=$PATH:/home/bnu/cmake/bin/
[root@adai003 local]# source ~/.bash_profile

检查cmake是否安装成功:
[root@adai003 local]# cmake --version
cmake version 3.9.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).
```
即:cmake安装完成!

#### mysql安装包

```
[root@adai003 src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35.tar.gz
[root@adai003 src]# tar zxf mysql-5.6.35.tar.gz
[root@adai003 src]# cd mysql-5.6.35/   
[root@adai003 mysql-5.6.35]# rm -rf CMakeCache.txt

[root@adai003 mysql-5.6.35]# useradd mysql
[root@adai003 mysql-5.6.35]# mkdir -p /data/mysql
[root@adai003 mysql-5.6.35]# chown mysql:mysql /data/mysql/
```
### 环境配置
从mysql 5.5开始不再使用./configure,而是换做cmake工具  

```
[root@adai003 mysql-5.6.35]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=0
```
**注:** 以上环境配置参数根据自己的需求参考本文中‘环境配置参数解析’自定义配置。  

报错1:  

```
CMake Error at CMakeLists.txt:116 (PROJECT):
  No CMAKE_CXX_COMPILER could be found.

```
解决办法:  

```
[root@adai003 mysql-5.6.35]# yum install -y gcc gcc-c++
```
报错2:  

```
CMake Error at cmake/ssl.cmake:167 (FILE):
  file STRINGS file
  "/usr/local/src/mysql-5.6.35/OPENSSL_INCLUDE_DIR-NOTFOUND/openssl/opensslv.h"
  cannot be read.
Call Stack (most recent call first):
  CMakeLists.txt:419 (MYSQL_CHECK_SSL)
```
解决办法:

```
[root@adai003 mysql-5.6.35]# yum install -y ssl openssl openssl-devel
```

报错3:

```
CMake Error at cmake/readline.cmake:85 (message):
  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
  cmake/readline.cmake:128 (FIND_CURSES)
  cmake/readline.cmake:202 (MYSQL_USE_BUNDLED_EDITLINE)
  CMakeLists.txt:421 (MYSQL_CHECK_EDITLINE)
```
解决办法:  

```
[root@adai003 mysql-5.6.35]# yum install -y ncurses ncurses-devel

[root@adai003 mysql-5.6.35]# rm -rf CMakeCache.txt
```
报错4:  

```
Warning: Bison executable not found in PATH
```
解决办法:

```
[root@adai003 mysql-5.6.35]# yum install -y bison
```
至此mysql安装环境配置完成!  

### cmake安装mysql环境配置参数解析

```
-DCMAKE_INSTALL_PREFIX= 指向mysql安装目录
-DINSTALL_SBINDIR=sbin 指向可执行文件目录(prefix/sbin)
-DMYSQL_DATADIR=/var/lib/mysql 指向mysql数据文件目录(/var/lib/mysql)
-DSYSCONFDIR=/etc/mysql 指向mysql配置文件目录(/etc/mysql)
-DINSTALL_PLUGINDIR=lib/mysql/plugin 指向插件目录(prefix/lib/mysql/plugin)
-DINSTALL_MANDIR=share/man 指向man文档目录(prefix/share/man)
-DINSTALL_SHAREDIR=share 指向aclocal/mysql.m4安装目录(prefix/share)
-DINSTALL_LIBDIR=lib/mysql 指向对象代码库目录(prefix/lib/mysql)
-DINSTALL_INCLUDEDIR=include/mysql 指向头文件目录(prefix/include/mysql)
-DINSTALL_INFODIR=share/info 指向info文档存放目录(prefix/share/info)
prefix官方推荐设为/usr
Storage Engine相关
类型csv,myisam,myisammrg,heap,innobase,archive,blackhole
若想启用某个引擎的支持:-DWITH_<ENGINE>_STORAGE_ENGINE=1
如:
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
若想禁用某个引擎的支持:-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
如:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
Library相关
-DWITH_READLINE=1 启用readline库支持(提供可编辑的命令行)
-DWITH_SSL=system 启用ssl库支持(安全套接层)
-DWITH_ZLIB=system 启用libz库支持(zib、gzib相关)
-DWTIH_LIBWRAP=0 禁用libwrap库(实现了通用TCP包装的功能,为网络服务守护进程使用)
-DMYSQL_TCP_PORT=3306 指定TCP端口为3306
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock 指定mysql.sock路径
-DENABLED_LOCAL_INFILE=1 启用本地数据导入支持
-DEXTRA_CHARSETS=all 启用额外的字符集类型(默认为all)
-DDEFAULT_CHARSET=utf8 指定默认的字符集为utf8
-DDEFAULT_COLLATION=utf8_general_ci 设定默认排序规则(utf8_general_ci快速/utf8_unicode_ci准确)
-DWITH_EMBEDDED_SERVER=1 编译嵌入式服务器支持
-DMYSQL_USER=mysql 指定mysql用户(默认为mysql)
-DWITH_DEBUG=0 禁用debug(默认为禁用)
-DENABLE_PROFILING=0 禁用Profiling分析(默认为开启)
-DWITH_COMMENT='string' 一个关于编译环境的描述性注释
```

### 编译&安装mysql

```
[root@adai003 mysql-5.6.35]# make
```
该部分时间会很长!!!  

第一次进行环境配置时没有添加“-DWITHOUT_PARTITION_STORAGE_ENGINE=0”报错:  

```
../../../sql/libsql.a(handler.cc.o):在函数‘get_ha_partition(partition_info*)’中:
/usr/local/src/mysql-5.6.35/sql/handler.cc:460:对‘ha_partition::ha_partition(handlerton*, partition_info*)’未定义的引用
/usr/local/src/mysql-5.6.35/sql/handler.cc:462:对‘ha_partition::initialize_partition(st_mem_root*)’未定义的引用
../../../sql/libsql.a(sql_partition_admin.cc.o):在函数‘Sql_cmd_alter_table_truncate_partition::execute(THD*)’中:
/usr/local/src/mysql-5.6.35/sql/sql_partition_admin.cc:822:对‘ha_partition::truncate_partition(Alter_info*, bool*)’未定义的引用
collect2: 错误:ld 返回 1
make[2]: *** [storage/perfschema/unittest/pfs_connect_attr-t] 错误 1
make[1]: *** [storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/all] 错误 2
make: *** [all] 错误 2
```
解决办法:

重新进行环境配置,添加参数“-DWITHOUT_PARTITION_STORAGE_ENGINE=0”。  
经过半小时终于编译完成:  

```
[root@adai003 mysql-5.6.35]# echo $?
0
```
安装:  

```
[root@adai003 mysql-5.6.35]# make install
```

### 添加系统配置  

#### 初始化

```
[root@adai003 mysql-5.6.35]# cd /usr/local/mysql/

[root@adai003 mysql]# chown -R mysql.mysql /usr/local/mysql/

[root@adai003 mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql
```
报错1:  

```
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
```
解决办法:  

```
[root@adai003 mysql]# yum install -y yum install -y perl-Data-Dumper
```
#### 更改配置文件  
初始化完成后有这样一段提示:  

```
New default config file was created as /usr/local/mysql//my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
```
大概意思是说:生产功能了新的默认配置文件/usr/local/mysql/my.cnf,但是呢系统中自带一个默认配置文件/etc/my.cnf,如果要使用新的配置文件,那么需要将/etc/my.cnf删除。  
在此,使用系统自带默认配置文件,进行如下配置:  

```
[root@adai003 mysql]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
```

#### 启动脚本配置: 

```
[root@adai003 mysql]# cp support-files/mysql.server /etc/init.d/mysqld    

[root@adai003 mysql]# vim /etc/init.d/mysqld
……
basedir=/usr/local/mysql
datadir=/data/mysql
……
```
#### 启动mysql服务

```
添加到系统服务:
[root@adai003 mysql]# chkconfig --add mysqld
[root@adai003 mysql]# chkconfig start mysqld on

启动服务:
[root@adai003 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/adai003.err'.
........ SUCCESS! 
```
#### 检查mysql服务状态  

```
[root@adai003 mysql]# ps aux |grep mysql
root     60691  0.0  0.1 113256  1612 pts/1    S    09:17   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/adai003.pid
mysql    60814 21.3 44.7 997508 451500 pts/1   Sl   09:17   0:10 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/adai003.err --pid-file=/data/mysql/adai003.pid --socket=/tmp/mysql.sock
root     60856  0.0  0.0 112664   976 pts/1    S+   09:18   0:00 grep --color=auto mysql
[root@adai003 mysql]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp6       0      0 :::3306                 :::*                    LISTEN      60814/mysqld        

```

### 参考文章  

cmake install :https://cmake.org/install/    

mysql源码包安装:http://www.mamicode.com/info-detail-1220821.html  

mysql编译参数:http://www.mamicode.com/info-detail-1220821.html

更改mysql配置:  
http://blog.csdn.net/chaihuasong/article/details/19069121  
http://www.cnblogs.com/idnf/p/4590818.html

make时错误处理:http://blog.csdn.net/weiwangsisoftstone/article/details/50522780
赞 (0) 评论 分享 ()