mysql为我们提供了三种安装方式:二进制、rpm、源代码编译。这里我们详细讨论mysql的源代码编译安装方式。

在5.5版本之后,mysql不再提供configure编译方式,改为使用cmake编译工具,cmake工具的一个显著特点是其编译独立于源代码,即我们可以在源代码之外的目录使用cmake来编译mysql,如

[[email protected] tmp]# ls -l | grep mysql
drwxr-xr-x  2 root root         4096 4月  29 21:11 mysql2
drwxr-xr-x 33 7161 wheel        4096 3月  15 03:07 mysql-5.6.17
-rw-r--r--  1 easy oinstall 32862539 4月  23 20:52 mysql-5.6.17.tar.gz
drwx------  2 easy oinstall     4096 4月  27 22:43 mysql-workbench.easy
[[email protected] tmp]# cd mysql2
[[email protected] mysql2]# cmake ../mysql-5.6.17

这样我们便可以在不同的目录下对源文件进行多次编译,而相互不影响。

使用cmake,可以通过 -LAH来获取所有的编译选项

[[email protected] mysql1]# cmake ../mysql-5.6.17 -LAH
-- Running cmake version 2.6.4
-- MySQL 5.6.17
-- Packaging as: mysql-5.6.17-Linux-x86_64
-- HAVE_VISIBILITY_HIDDEN
-- HAVE_VISIBILITY_HIDDEN
-- HAVE_VISIBILITY_HIDDEN
-- Using cmake version 2.6.4
-- Not building NDB
-- Library mysqlclient depends on OSLIBS -lpthread;m;rt;dl
-- Library mysqlserver depends on OSLIBS -lpthread;m;rt;crypt;dl;aio
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/mysql1
-- Cache values
// path to the executable
ACLOCAL_EXECUTABLE:FILEPATH=/usr/bin/aclocal

.....

// Missing description
WITH_ZLIB:STRING=bundled

如果要清除编译信息,可以使用如下命令:
[[email protected] mysql1]# make clean
[[email protected] mysql1]# rm CMakeCache.txt 
rm:是否删除普通文件 "CMakeCache.txt"?y

编译选项:

部分选项使用来指定编译过程中使用的可执行文件的路径,如 ACLOCAL_EXECUTABLE:FILEPATH=/usr/bin/aclocal,大家可以自己研究,这里不再獒述。

-DBUILD_CONFIG=mysql_release
使用该选项,可以促使编译器按照oracle生成官方版本的选项来编译程序,例子:

cmake -DBUILD_CONFIG=mysql_release
?
-DCMAKE_BUILD_TYPE=type
该选项具有两个选择值
? RelWithDebInfo: 默认值,启用optimizations,同时生成调试信息
? Debug: 禁用optimizations, 同时生成调试信息,与-DWITH_DEBUG=1 具有相同的效果
?
-DCPACK_MONOLITHIC_INSTALL=bool

该选项影响make package操作的行为,如果设置为真,生成单个文件,否则生成多个文件。默认是生成多个文件

The CMAKE_INSTALL_PREFIX option indicates the base installation directory. Other options with
names of the form INSTALL_xxx that indicate component locations are interpreted relative to the
prefix and their values are relative pathnames. Their values should not include the prefix.
?
-DCMAKE_INSTALL_PREFIX=dir_name

该选项指定了mysql的安装目录,除了在编译时指定此值,我们也可月使用--basedir在mysql启动时设置基准目录。在编译时,INSTALL_XXX格式的选项是相对于PREFIX的相对路径,这是需要注意的一点
?
-DINSTALL_BINDIR=dir_name
bin文件的目录
?
-DINSTALL_LAYOUT=name
使用何种已经预设好的文件布局,我们可以在这些的布局的基础上,再手工设定某些目录的位置
? STANDALONE: Same layout as used for .tar.gz and .zip packages. This is the default.
? RPM: Layout similar to RPM packages.
? SVR4: Solaris package layout.
? DEB: DEB package layout (experimental).
如下
shell> cmake . -DINSTALL_LAYOUT=SVR4 -DMYSQL_DATADIR=/var/mysql/data
?
-DINSTALL_MYSQLTESTDIR=dir_name
Where to install the mysql-test directory. As of MySQL 5.6.12, to suppress installation of this
directory, explicitly set the option to the empty value (-DINSTALL_MYSQLTESTDIR=).
?
-DINSTALL_SQLBENCHDIR=dir_name
Where to install the sql-bench directory. To suppress installation of this directory, explicitly set the
option to the empty value (-DINSTALL_SQLBENCHDIR=).
?
-DMYSQL_DATADIR=dir_name
指定数据文件的位置,也可以安装后使用--datadir来设置

?
-DODBC_INCLUDES=dir_name
The location of the ODBC includes directory, and may be used while configuring Connector/ODBC.(配置odbc时可能会使用)
?
-DODBC_LIB_DIR=dir_name
The location of the ODBC library directory, and may be used while configuring Connector/ODBC.
?
-DSYSCONFDIR=dir_name
The default my.cnf option file directory. This location cannot be set at server startup, but you can start the server with a given option file using the --defaults-file=file_name option, where file_name is the full path name to the file.

在编译mysql时,我们可以指定如何将存储引擎编译进mysql:静态编译和动态编译。个人认为静态编译的服务器效率会高一些。

默认情况下MyISAM, MERGE, MEMORY, CSV 四种存储引擎是必须的,并且会静态编译进服务器中。如果我们需要静态编译其他存储引擎可以使用如下参数

-DWITH_engine_STORAGE_ENGINE=1

这里的engine可以取值ARCHIVE, BLACKHOLE, EXAMPLE, FEDERATED, INNOBASE,NDB or NDBCLUSTER (NDB), PARTITION (partitioning support), and PERFSCHEMA(Performance Schema).

我们也可以显示排除某种存储引擎,如

-DWITHOUT_engine_STORAGE_ENGINE=1.

如果对于某种引擎即没有显示引入也没有显示排除,mysql会将该引擎编译成共享模块,如果无法编译成共享模块,则排除该引擎。

-DDEFAULT_CHARSET=charset_name

指定数据库的默认字符集,字符集的取值可以在cmake/charset-sets.cmake下找到

-DDEFAULT_COLLATION=collation_name

指定数据库的排序方式,可以在数据库启动时通过--collation_server选项来指定,通过show collation命令可以查看可选的排序方式

-DENABLE_DEBUG_SYNC=bool

用于开启mysql的同步调试功能,默认情况下,该功能是开启的,但是只有在mysql配置为启用调试时才会生效。既是启用了该功能,在运行时该功能亦是关闭的,需要使用--debug-sync-timeout=N 选项来启动mysql服务器才可以,默认n为0,即该功能禁用,如果n大于0,在同步调试会等待n时间单位,否则会超时。

?
-DENABLE_DOWNLOADS=bool
用来一些可选文件,如google的测试分支等。

-DENABLE_DTRACE=bool
是否启用dtrace
?
-DENABLE_GCOV=bool
Whether to include gcov support (Linux only).
?
-DENABLE_GPROF=bool
Whether to enable gprof (optimized Linux builds only). This option was added in MySQL 5.6.6.
?
-DENABLED_LOCAL_INFILE=bool
使客户端具有load data infile的功能,该功能具有安全隐患,

?
-DENABLED_PROFILING=bool
是否启用PROFILING功能,建议开启,用于性能监视


?
-DIGNORE_AIO_CHECK=bool
If the -DBUILD_CONFIG=mysql_release option is given on , the libaio library must be
linked in by default. If you do not have libaio or do not want to install it, you can suppress the
check for it by specifying -DIGNORE_AIO_CHECK=1. This option was added in MySQL 5.6.1.
?
-DINNODB_PAGE_ATOMIC_REF_COUNT=bool
Whether to enable or disable atomic page reference counting. Fetching and releasing pages from the buffer pool and tracking the page state are expensive and complex operations. Using a page
mutex to track these operations does not scale well. With INNODB_PAGE_ATOMIC_REF_COUNT=ON (default), fetch and release is tracked using atomics where available. For platforms that do not
support atomics, set INNODB_PAGE_ATOMIC_REF_COUNT=OFF to disable atomic page reference counting.
When atomic page reference counting is enabled (default), “[Note] InnoDB: Using atomics
to ref count buffer pool pages” is printed to the error log at server startup. If atomic page reference counting is disabled, “[Note] InnoDB: Using mutexes to ref count buffer pool pages” is printed instead.
This build option was introduced with the fix for MySQL Bug #68079.
?
-DMYSQL_MAINTAINER_MODE=bool
Whether to enable a MySQL maintainer-specific development environment. If enabled, this option causes compiler warnings to become errors.
?
-DMYSQL_TCP_PORT=port_num
The port number on on which the server listens for TCP/IP connections. The default is 3306. This value can be set at server startup with the --port option.


-DMYSQL_UNIX_ADDR=file_name
The Unix socket file path on which the server listens for socket connections. This must be an absolute path name. The default is /tmp/mysql.sock.
This value can be set at server startup with the --socket option.

?
-DOPTIMIZER_TRACE=bool
Whether to support optimizer tracing. See MySQL Internals: Tracing the Optimizer. This option was added in MySQL 5.6.3.
?
-DWITH_ASAN=bool
Whether to enable AddressSanitizer, for compilers that support it. The default is off. This option was added in MySQL 5.6.15.
?
-DWITH_DEBUG=bool
Whether to include debugging support.
Configuring MySQL with debugging support enables you to use the --debug="d,parser_debug" option when you start the server. This causes the Bison parser that is used to process SQL statements to dump a parser trace to the server's standard error output. Typically, this output is written to the error log.

?
-DWITH_DEFAULT_FEATURE_SET=bool
Whether to use the flags from cmake/build_configurations/feature_set.cmake. This
option was added in MySQL 5.6.6.
?
-DWITH_EDITLINE=value
Which libedit/editline library to use. The permitted values are bundled (the default) and
system.
WITH_EDITLINE was added in MySQL 5.6.12. It replaces WITH_LIBEDIT, which has been
removed.
?
-DWITH_EMBEDDED_SERVER=bool
Whether to build the libmysqld embedded server library.

?
-DWITH_EXTRA_CHARSETS=name
Which extra character sets to include:
? all: All character sets. This is the default.
? complex: Complex character sets.
? none: No extra character sets.
?
-DWITH_INNODB_MEMCACHED=bool
Whether to generate memcached shared libraries (libmemcached.so and innodb_engine.so).

?
-DWITH_LIBEVENT=string
Which libevent library to use. Permitted values are bundled (default), system, and yes. If
you specify system or yes, the system libevent library is used if present. If the system library
is not found, the bundled libevent library is used. The libevent library is required by InnoDB
memcached.

-DWITH_LIBEDIT=bool
Whether to use the libedit library bundled with the distribution.
WITH_LIBEDIT was removed in MySQL 5.6.12. Use WITH_EDITLINE instead.
?
-DWITH_LIBWRAP=bool
Whether to include libwrap (TCP wrappers) support.
?
-DWITH_READLINE=bool
Whether to use the readline library bundled with the distribution. This option was removed in
MySQL 5.6.5 because readline is no longer bundled.

?
-DWITH_UNIXODBC=1
Enables unixODBC support, for Connector/ODBC.

?
-DWITH_ZLIB=zlib_type
Some features require that the server be built with compression library support, such as the
COMPRESS() and UNCOMPRESS() functions, and compression of the client/server protocol. The
WITH_ZLIB indicates the source of zlib support:
? bundled: Use the zlib library bundled with the distribution.
? system: Use the system zlib library. This is the default.
?
-DWITHOUT_SERVER=bool
Whether to build without the MySQL server. The default is OFF, which does build the server.

了解了各个选项的意义,下面就可以执行安装了

# Preconfiguration setup
shell> groupadd mysql
shell> useradd -r -g mysql mysql
# Beginning of source-build specific instructions
shell> tar zxvf mysql-VERSION.tar.gz
shell> cd mysql-VERSION
shell> cmake .
shell> make
shell> make install
# End of source-build specific instructions
# Postinstallation setup
shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server