首先在编译之前,我们要了解相关mysql 5.7的编译选项,官网编译选项地址:http://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
1、安装编译工具cmake和其他的c语言编译环境
yum install -y make gcc gcc-c++ ncurses-devel cmake
编译选项解释
cmake指定编译选项的方式不同于make,其实现方式对比如下:
./configure --help
make |cmake
---------------------|--------------------------
./configure |cmake .
./configure --help |cmake . -LH or ccmake .
2、编译安装
(1)从MySQL 5.7.5开始Boost库是必需的,直接cmake指定选项,下面是自动下载,如果太慢可以手动下载,放在<directory> 里面,就OK。
-DDOWNLOAD_BOOST=0
-DWITH_BOOST=<directory>
(2)系统会自动下载 boost库,也可以手动。下面编译的时候我加了这个选项,自动下载。
-DDOWNLOAD_BOOST=1
-DWITH_BOOST=<directory>
对于mysql boost 库不能低于1.59.0,centos 7默认1.53.0
ERROR:MySQL currently requires boost_1_59_0 #更新到1.59版本就好
编译过程1个小时左右,视cpu性能而定。指定红色部分的目录的时候,目录必须要存在,不然编译后找不到相关文件。编译的时候建议在mysql用户下编译
mkdir /usr/local/mysql
mkdir /data/mysql #在自定义分区data下创建mysql目录
groupadd mysql
useradd mysql -g mysql #创建用户mysql属于用户组mysql
passwd mysql #设置mysql用户的密码,以后可以登录这个用户方便mysql维护。
yum install openssl openssl-devel bison*
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/usr/local/mysql/ \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_LIBWRAP=0 \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/var/lib
make
make install
chown -R mysql.mysql /usr/local/mysql /data/mysql #把这个目录权限给用户mysql,编译之后再执行
编译后你会发现在mysql的根目录没有my.cnf配置文件,这时候系统会用自带/etc/my.cnf作为你的配置文件,这和你编译的配置文件路径不一样。建议把/etc/my.cnf改名为my.cng.back,然后用cp support-files/my-default.cnf ../my.cnf 将默认配置文件复制在你编译配置文件的路径。但是需要自己更改默认设置的配置文件。
my.cnf配置
[client]
#password = [your_password]
#socket = /usr/local/mysql/mysql.sock
default-character-set= utf8
[mysqld]
port = 3306
socket = /usr/local/mysql/mysql.sock
back_log = 120
max_connections = 3000
max_connect_errors = 30
max_allowed_packet = 32M
binlog_cache_size = 4M
max_heap_table_size = 128M
sort_buffer_size = 16M
join_buffer_size = 16M
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8
transaction_isolation = REPEATABLE-READ
key_buffer_size = 128M
read_buffer_size = 8M
skip_name_resolve = 1
basedir = /usr/local/mysql
datadir = /data/mysql
tmpdir = /tmp
log_error = error.log
default-storage-engine = INNODB
character-set-server = utf8
collation-server = utf8_general_ci
server-id = 1
[mysql]
no-auto-rehash
3、初始化数据库,并更改root用户密码
./bin/mysqld --initialize --datadir=/data/mysql --user=mysql #初始化数据库的时候,最后一行输出会有一个root用户的随机密码,记住这个随机密码,第一次用root用户登录是要这个随机密码的。在进行其他命令操作之前要修改root用户的密码,命令如下:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
4、开启mysql服务,我这里是用mysql用户启动。
/usr/local/mysql/support-files/mysql.server start
附录:
[root@cloud mysql-5.7.15]# cmake . -LH
-- Running cmake version 2.8.11
-- Could NOT find Git (missing: GIT_EXECUTABLE)
-- Configuring with MAX_INDEXES = 64U
-- SIZEOF_VOIDP 8
-- MySQL 5.7.15
-- Packaging as: mysql-5.7.15-Linux-x86_64
-- Local boost dir /var/lib/boost_1_59_0
-- Local boost zip /var/lib/boost_1_59_0.tar.gz
-- Found /var/lib/boost_1_59_0/boost/version.hpp
-- BOOST_VERSION_NUMBER is #define BOOST_VERSION 105900
-- BOOST_INCLUDE_DIR /var/lib/boost_1_59_0
-- OPENSSL_INCLUDE_DIR = /usr/include
-- OPENSSL_LIBRARY = /usr/lib64/libssl.so
-- CRYPTO_LIBRARY = /usr/lib64/libcrypto.so
-- OPENSSL_MAJOR_VERSION = 1
-- SSL_LIBRARIES = /usr/lib64/libssl.so;/usr/lib64/libcrypto.so;dl
-- WITH_PROTOBUF=bundled
-- protobuf version is 2.6
-- Using cmake version 2.8.11
-- Disabling -Wunused-but-set-variable warning for building NDB
-- Disabling -Wstrict-aliasing warning for building NDB
-- Not building NDB
-- Using Boost headers from /var/lib/boost_1_59_0
-- MYSQLX - Text log of protobuf messages enabled
-- Library mysqlclient depends on OSLIBS -lpthread;/usr/lib64/libz.so;m;rt;/usr/lib64/libssl.so;/usr/lib64/libcrypto.so;dl
-- Library mysqlserver depends on OSLIBS -lpthread;/usr/lib64/libz.so;m;rt;/usr/lib64/libssl.so;/usr/lib64/libcrypto.so;dl;crypt
-- INSTALL mysqlclient.pc lib/pkgconfig
-- Skipping deb packaging on unsupported platform .
-- CMAKE_BUILD_TYPE: RelWithDebInfo
-- COMPILE_DEFINITIONS: _GNU_SOURCE;_FILE_OFFSET_BITS=64;HAVE_CONFIG_H;HAVE_LIBEVENT1
-- CMAKE_C_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Wwrite-strings -Wdeclaration-after-statement
-- CMAKE_CXX_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Woverloaded-virtual -Wno-unused-parameter
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/src/mysql-5.7.15
-- Cache values
// Path to a file.
BOOST_INCLUDE_DIR:PATH=/var/lib/boost_1_59_0
// Bundle mecab and ipadic with plugin
BUNDLE_MECAB:BOOL=ON
// Choose the type of build, options are: None(CMAKE_CXX_FLAGS or
CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel
CMAKE_BUILD_TYPE:STRING=RelWithDebInfo
// Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql
// Set to true if this is a community build
COMMUNITY_BUILD:BOOL=ON
// Path to a library.
CRYPTO_LIBRARY:FILEPATH=/usr/lib64/libcrypto.so
//
DEB_CODENAME:STRING=
// Download boost from sourceforge.
DOWNLOAD_BOOST:BOOL=1
// Timeout in seconds when downloading boost.
DOWNLOAD_BOOST_TIMEOUT:STRING=600
// Enable profiling
ENABLED_PROFILING:BOOL=ON
// Enable debug sync (debug builds only)
ENABLE_DEBUG_SYNC:BOOL=ON
// Download and build 3rd party source code components, e.g. google mock
ENABLE_DOWNLOADS:BOOL=OFF
// Enable gcov (debug, Linux builds only)
ENABLE_GCOV:BOOL=OFF
// Enable gprof (optimized, Linux builds only)
ENABLE_GPROF:BOOL=OFF
// Enable SASL on InnoDB Memcached
ENABLE_MEMCACHED_SASL:BOOL=OFF
// Enable SASL on InnoDB Memcached
ENABLE_MEMCACHED_SASL_PWDB:BOOL=OFF
// Selection of features. This option is deprecated
FEATURE_SET:STRING=community
// Installation directory layout. Options are: TARGZ (as in tar.gz installer), WIN (as in zip installer), STANDALONE, RPM, DEB, SVR4, FREEBSD, GLIBC, OSX, SLES
INSTALL_LAYOUT:STRING=STANDALONE
// Where to install mysqlclient.pc, defaults to lib/pkgconfig
INSTALL_PKGCONFIGDIR:PATH=
// Path to a file.
LOCAL_BOOST_DIR:FILEPATH=/var/lib/boost_1_59_0
// Path to a file.
LOCAL_BOOST_ZIP:FILEPATH=/var/lib/boost_1_59_0.tar.gz
// Mutex type: event, sys or futex
MUTEXTYPE:STRING=event
// default MySQL data directory
MYSQL_DATADIR:PATH=/data/mysql
// default MySQL keyring directory
MYSQL_KEYRINGDIR:PATH=/usr/local/mysql/keyring
// MySQL maintainer-specific development environment
MYSQL_MAINTAINER_MODE:BOOL=OFF
// Path to a file.
OPENSSL_INCLUDE_DIR:PATH=/usr/include
// Path to a library.
OPENSSL_LIBRARY:FILEPATH=/usr/lib64/libssl.so
// Path to a file.
OPENSSL_ROOT_DIR:PATH=/usr
// Support tracing of Optimizer
OPTIMIZER_TRACE:BOOL=ON
// PATH to MySQL TMP dir. Defaults to the P_tmpdir macro in <stdio.h>
TMPDIR:PATH=P_tmpdir
// Link ARCHIVE statically to the server
WITH_ARCHIVE_STORAGE_ENGINE:BOOL=ON
// Enable address sanitizer
WITH_ASAN:BOOL=OFF
// Link BLACKHOLE statically to the server
WITH_BLACKHOLE_STORAGE_ENGINE:BOOL=ON
// Path to boost sources: a directory, or a tarball to be unzipped.
WITH_BOOST:PATH=/var/lib
// Support for client-side protocol tracing plugins
WITH_CLIENT_PROTOCOL_TRACING:BOOL=ON
// Use dbug/safemutex
WITH_DEBUG:BOOL=OFF
// Use flags from cmake/build_configurations/compiler_options.cmake
WITH_DEFAULT_COMPILER_OPTIONS:BOOL=ON
// Use feature set in cmake/build_configurations/feature_set.cmake
WITH_DEFAULT_FEATURE_SET:BOOL=ON
// By default use bundled editline
WITH_EDITLINE:STRING=bundled
// Compile MySQL with embedded server
WITH_EMBEDDED_SERVER:BOOL=ON
// Generate shared version of embedded library (in addition to the static one)
WITH_EMBEDDED_SHARED_LIBRARY:BOOL=OFF
// Options are: none, complex, all
WITH_EXTRA_CHARSETS:STRING=all
// Link FEDERATED statically to the server
WITH_FEDERATED_STORAGE_ENGINE:BOOL=ON
// Enable extra InnoDB debug checks
WITH_INNODB_EXTRA_DEBUG:BOOL=OFF
//
WITH_INNODB_MEMCACHED:BOOL=OFF
// By default use bundled libevent on this platform
WITH_LIBEVENT:STRING=bundled
// Compile with tcp wrappers support
WITH_LIBWRAP:BOOL=0
// By default use bundled lz4 library
WITH_LZ4:STRING=bundled
// <empty> (disabled) | system (use os library) | </path/to/custom/installation> (use custom version)
WITH_MECAB:STRING=
// Enable memory sanitizer
WITH_MSAN:BOOL=OFF
// Link NGRAM_PARSER statically to the server
WITH_NGRAM_PARSER:BOOL=ON
// Explicitly set NUMA memory allocation policy
WITH_NUMA:BOOL=ON
// Link PARTITION statically to the server
WITH_PARTITION_STORAGE_ENGINE:BOOL=ON
// Generate PIC objects
WITH_PIC:BOOL=OFF
// Build additonal code(plugins) that is located in rapid directory
WITH_RAPID:BOOL=ON
//
WITH_SSL:STRING=system
// Enable installation of systemd support files
WITH_SYSTEMD:BOOL=OFF
// Have a built-in test protocol trace plugin in libmysql (requires WITH_CLIENT_PROTOCOL_TRACING option)
WITH_TEST_TRACE_PLUGIN:BOOL=OFF
// Enable undefined behavior sanitizer
WITH_UBSAN:BOOL=OFF
// Compile MySQL with unit tests
WITH_UNIT_TESTS:BOOL=ON
// Valgrind instrumentation
WITH_VALGRIND:BOOL=OFF
//
WITH_ZLIB:STRING=system
// Enable logging of protobuf messages
XPLUGIN_LOG_PROTOBUF:STRING=1