1. 首页
  2. >
  3. 数据库技术
  4. >
  5. MySQL

使用源码编译安装MySQL

  • 前言
  • 环境准备
  • 源码编译安装
    • 下载MySQL源码
    • 解压源码文件
    • 开始编译操作
    • 安装MySQL数据库
    • 生成二进制安装包
  • 初始化MySQL
    • 创建系统用户
    • 执行数据库的初始化
    • 启动MySQL服务
    • 登录MySQL数据库
  • 总结

前言

前面的文章中,我们介绍了MySQL使用二进制文件安装的大致过程。今天我们来看下另外一种安装方式,那就是使用MySQL的源码自己编译安装MySQL。

使用源码编译安装MySQL

环境准备

使用源码编译安装MySQL

我们使用docker运行一个最新版本的centos镜像文件,然后基于这个运行中的容器进行编译,进入到容器中进行编译安装MySQL。在docker容器层面涉及到命令如下:

# 从docker hub上拉取最新的centos镜像文件 docker pull centos  # 运行centos镜像文件 docker run -it -d --name centos-mysql --hostname centos.mysqlserver centos:latest  # 进入centos容器内 docker exec -it centos-mysql /bin/bash 

下面,我们就把这个用docker启动的centos容器来作为我们的MySQL编译安装的服务器,所有的操作都是基于这个容器服务器来操作的。

源码编译安装

下载MySQL源码

在MySQL官网中,按照下面的步骤来找到下载链接,然后再下载MySQL的源码包。

地址为:https://dev.mysql.com/downloads/,选择页面中的MySQL Community Server链接。

使用源码编译安装MySQL


地址为:https://dev.mysql.com/downloads/mysql/,选择页面中的Archives页签。

使用源码编译安装MySQL


地址为:https://downloads.mysql.com/archives/community/,这里我们选择MySQL5.7.32版本,然后选择源码,再选择Linux通用版本的源码。这里我们选择包含Boost的源码包,这所以选择这个是因为在编译安装的过程中会用到Boost包,所以这里一并下载好了,就不用在单独去下载Boost包了。

使用源码编译安装MySQL


我们使用复制好的连接地址,在服务器上面使用wget命令下载源码压缩包。如下所示:

[root@centos ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.32.tar.gz [root@centos ~]# ls -lstr mysql-boost-5.7.32.tar.gz 51648 -rw-rw-r-- 1 501 games 52882168 Sep 23 10:13 mysql-boost-5.7.32.tar.gz [root@centos ~]# 

解压源码文件

使用源码编译安装MySQL

针对我们下载好的源码包,我们进行解压操作,具体步骤和命令如下:

[root@centos ~]# tar -zxf mysql-boost-5.7.32.tar.gz [root@centos ~]# ls -lstr 51648 -rw-rw-r--  1  501 games 52882168 Sep 23 10:13 mysql-boost-5.7.32.tar.gz     4 drwxr-xr-x 37 7161 31415     4096 Jan 20 10:40 mysql-5.7.32 [root@centos ~]# 

在我们解压出来的MySQL源码文件中,有一个boost文件夹目录,这个文件夹需要放到/usr/local目录下面。我们使用下面的命令来完成这个操作。

[root@centos ~]# mkdir /usr/local/boost [root@centos ~]# mv ~/mysql-5.7.32/boost /usr/local 

在编译的时候会用到这个文件夹下面的内容。也可以把这个boost文件目录放到其他的位置,但是在执行编译的时候需要使用参数声明boost文件夹目录。放在/usr/local下面,是MySQL编译的时候使用的默认的位置。

开始编译操作

使用源码编译安装MySQL

下面我们开始编译源码的步骤,这个步骤中会遇到各种错误。我们遇到每一个错误都挨个排查,一个个解决。最后我们再进行总结,以便于下次再次编译的时候不会遇到相同的错误。

进入MySQL源码目录下面,创建一个bld的文件夹,在这个文件夹下面开始执行编译操作。

[root@centos ~]# cd mysql-5.7.32 [root@centos mysql-5.7.32]# pwd /root/mysql-5.7.32 [root@centos mysql-5.7.32]# mkdir bld [root@centos mysql-5.7.32]# cd bld [root@centos bld]# cmake .. 

执行make ..命令的时候,出现如下错误提示:

[root@centos bld]# cmake .. bash: cmake: command not found [root@centos bld]# 

错误原因:没有安装cmake命令。解决方法,使用如下yum命令来安装cmake命令:

[root@centos bld]# yum install cmake -y 

安装完成cmake命令之后,我们来尝试编译,结果出现如下错误:

-- Running cmake version 3.11.4 -- Could NOT find Git (missing: GIT_EXECUTABLE) -- Configuring with MAX_INDEXES = 64U CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool. CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage -- Configuring incomplete, errors occurred! See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeOutput.log". [root@centos bld]# 

错误原因:没有安装git。解决方法,使用如下yum命令来安装git

[root@centos bld]# yum install git -y 

安装完成git命令之后,我们再次尝试使用cmake来编译MySQL源码,结果又出现如下错误:

-- Running cmake version 3.11.4 -- Found Git: /usr/bin/git (found version "2.27.0") -- Configuring with MAX_INDEXES = 64U CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool. CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage -- Configuring incomplete, errors occurred! See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeOutput.log". [root@centos bld]# 

错误原因:没有安装make命令。解决方法:使用如下yum命令来安装make命令:

[root@centos bld]# yum install make -y 

安装完成make命令之后,再次尝试使用cmake命令来编译,结果出现如下错误:

-- Running cmake version 3.11.4 -- Configuring with MAX_INDEXES = 64U -- The C compiler identification is unknown -- The CXX compiler identification is unknown CMake Error at CMakeLists.txt:146 (PROJECT):   No CMAKE_C_COMPILER could be found.    Tell CMake where to find the compiler by setting either the environment   variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to   the compiler, or to the compiler name if it is in the PATH.   CMake Error at CMakeLists.txt:146 (PROJECT):   No CMAKE_CXX_COMPILER could be found.    Tell CMake where to find the compiler by setting either the environment   variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path   to the compiler, or to the compiler name if it is in the PATH.   -- Configuring incomplete, errors occurred! See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeOutput.log". See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeError.log". [root@centos bld]# ls 

错误原因:没有安装gcc等库文件,解决方法:使用如下yum命令来安装gcc等库文件:

[root@centos bld]# yum install gcc -y [root@centos bld]# yum install gcc-c++ -y 

然后我们继续编译,结果又遇到如下错误:

CMake Error at cmake/boost.cmake:88 (MESSAGE):   You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>    This CMake script will look for boost in <directory>.  If it is not there,   it will download and unpack it (in that directory) for you.    If you are inside a firewall, you may need to use an http proxy:    export http_proxy=http://example.com:80  Call Stack (most recent call first):   cmake/boost.cmake:245 (COULD_NOT_FIND_BOOST)   CMakeLists.txt:548 (INCLUDE)   -- Configuring incomplete, errors occurred! See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeOutput.log". See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeError.log". [root@centos bld]# 

错误原因:没有指定boost目录。boost库文件,在MySQL编译的时候需要使用到。这个库文件通过参数-DDOWNLOAD_BOOST=ON在编译的时候指定是否自己动下载,同时可以通过参数-DDOWNLOAD_BOOST_TIMEOUT=seconds指定下载等待的时长,如果在编译的过程中,下载boost库文件的时间超过了这个等待时间,则下载失败,编译失败。为了避免网络问题导致编译失败,建议下载MySQL源码的时候,选择带有boost库文件的源码包。这样就可以在便于的时候,指定已经下载好的boost库文件,避免下载失败。

解决方式,前面我们在解压MySQL源码文件包之后,把解压出来的文件目录下面的boost目录移动到了/usr/local目录下面,所以此时我们使用如下编译命令指定boost目录。

[root@centos bld]# cmake . -DWITH_BOOST=/usr/local/boost 

继续编译的时候,又出现如下错误:

Cannot find appropriate system libraries for WITH_SSL=system. Make sure you have specified a supported SSL version. Valid options are : system (use the OS openssl library), yes (synonym for system), </path/to/custom/openssl/installation>  CMake Error at cmake/ssl.cmake:63 (MESSAGE):   Please install the appropriate openssl developer package.  Call Stack (most recent call first):   cmake/ssl.cmake:280 (FATAL_SSL_NOT_FOUND_ERROR)   CMakeLists.txt:580 (MYSQL_CHECK_SSL)   -- Configuring incomplete, errors occurred! See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeOutput.log". See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeError.log". [root@centos bld]# 

错误原因:没有安装openssl相关库文件。解决方式:使用如下yum命令安装openssl

[root@centos bld]# yum install openssl* -y 

继续编译,又遇到如下错误:

-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH) CMake Error at cmake/readline.cmake:71 (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:102 (FIND_CURSES)   cmake/readline.cmake:195 (MYSQL_USE_BUNDLED_EDITLINE)   CMakeLists.txt:582 (MYSQL_CHECK_EDITLINE)   -- Configuring incomplete, errors occurred! See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeOutput.log". See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeError.log". [root@centos bld]# 

错误原因:没有安装ncurses包。解决方式:使用如下yum命令来安装ncurses-devel

[root@centos bld]# yum install ncurses-devel -y 

继续编译,又出现如下错误:

-- Checking for module 'libtirpc' --   Package 'libtirpc', required by 'virtual:world', not found CMake Error at cmake/rpc.cmake:76 (MESSAGE):   Could not find rpc/rpc.h in /usr/include or /usr/include/tirpc Call Stack (most recent call first):   rapid/plugin/group_replication/configure.cmake:60 (MYSQL_CHECK_RPC)   rapid/plugin/group_replication/CMakeLists.txt:25 (INCLUDE)   -- Configuring incomplete, errors occurred! See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeOutput.log". See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeError.log". [root@centos bld]# 

错误原因:没有libtirpc包,解决方式:使用下面的命令和步骤来安装这个包:

[root@centos bld]# yum install libtirpc* -y 

安装完成后,继续编译,又出现如下错误:

CMake Error at rapid/plugin/group_replication/rpcgen.cmake:100 (MESSAGE):   Could not find rpcgen Call Stack (most recent call first):   rapid/plugin/group_replication/CMakeLists.txt:36 (INCLUDE)   -- Configuring incomplete, errors occurred! See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeOutput.log". See also "/root/mysql-5.7.32/bld/CMakeFiles/CMakeError.log". [root@centos bld]# 

原因:没有rpcgen包,因为yum仓库中没有这个包,所以自己下载,编译来安装这个包。使用如下方式来安装这个包:

# 到root的home目录下面 [root@centos rpcsvc-proto-1.4]# cd ~  # 从github上面,下载rpcgen源码文件 [root@centos rpcsvc-proto-1.4]# wget https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4/rpcsvc-proto-1.4.tar.gz  # 解压rpcgen源码文件 [root@centos rpcsvc-proto-1.4]# tar -zxvf rpcsvc-proto-1.4.tar.gz  # 进入到rpcgen源码文件目录下面,执行编译命令 [root@centos rpcsvc-proto-1.4]# cd rpcsvc-proto-1.4 [root@centos rpcsvc-proto-1.4]# ./configure [root@centos rpcsvc-proto-1.4]# make  # 安装编译好的rpcgen [root@centos rpcsvc-proto-1.4]# make install 

安装完成rcgen之后,我们重新新进入MySQL源码目录下面的bld文件夹,使用cmake . -DWITH_BOOST=/usr/local/boost进行编译,此时的编译总算是成功了。cmake有很多其他参数,我们可以根据自己的时间情况来选择配置具体的参数值,这里我们都使用默认的选项。具体的参数参考这个页面https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html,里面记录了现象的内容。

下面是cmake常用的参数列表:

使用源码编译安装MySQL

cmake的编译过程如下所示:

[root@centos rpcsvc-proto-1.4]# cd ~/mysql-5.7.32/bld [root@centos bld]# cmake . -DWITH_BOOST=/usr/local/boost -- Running cmake version 3.11.4 -- Found Git: /usr/bin/git (found version "2.27.0") -- Configuring with MAX_INDEXES = 64U -- CMAKE_GENERATOR: Unix Makefiles -- SIZEOF_VOIDP 8 -- MySQL 5.7.32 -- Packaging as: mysql-5.7.32-Linux-x86_64 -- Local boost dir /usr/local/boost/boost_1_59_0 -- Found /usr/local/boost/boost_1_59_0/boost/version.hpp -- BOOST_VERSION_NUMBER is #define BOOST_VERSION 105900 -- BOOST_INCLUDE_DIR /usr/local/boost/boost_1_59_0 -- NUMA library missing or required version not available -- OPENSSL_INCLUDE_DIR = /usr/include -- OPENSSL_LIBRARY = /usr/lib64/libssl.so -- CRYPTO_LIBRARY = /usr/lib64/libcrypto.so -- OPENSSL_MAJOR_VERSION = 1 -- OPENSSL_MINOR_VERSION = 01 -- OPENSSL_FIX_VERSION = 01 -- SSL_LIBRARIES = /usr/lib64/libssl.so;/usr/lib64/libcrypto.so;dl -- AWK_EXECUTABLE is /usr/bin/gawk -- Found Git: /usr/bin/git -- LIBEVENT_VERSION_STRING 2.1.11-stable -- LIBEVENT_VERSION (bundled) 2.1.11 -- WITH_PROTOBUF=bundled -- protobuf version is 2.6 -- You need to set WITH_CURL. This variable needs to point to curl library. -- Creating LDAP authentication SASL client library. -- Required SASL header is missing.Skipping the LDAP SASL client authentication plugin. -- Library mysqlclient depends on OSLIBS -lpthread;m;rt;/usr/lib64/libssl.so;/usr/lib64/libcrypto.so;dl -- MERGE_CONVENIENCE_LIBRARIES TARGET mysqlclient -- MERGE_CONVENIENCE_LIBRARIES LIBS clientlib;dbug;strings;vio;mysys;mysys_ssl;zlib -- MERGE_CONVENIENCE_LIBRARIES MYLIBS clientlib;dbug;strings;vio;mysys;mysys_ssl;zlib -- RPC_INCLUDE_DIRS /usr/include/tirpc -- Using Boost headers from /usr/local/boost/boost_1_59_0 -- Performing Test CXX_HAVE_SIGN_COMPARE -- Performing Test CXX_HAVE_SIGN_COMPARE - Success -- Performing Test CXX_HAVE_UNUSED_VARIABLE -- Performing Test CXX_HAVE_UNUSED_VARIABLE - Success -- MYSQLX - Text log of protobuf messages enabled -- Performing Test HAVE_UNUSED_PARAMETER -- Performing Test HAVE_UNUSED_PARAMETER - Success -- Googletest was not found. gtest-based unit tests will be disabled. You can run cmake . -DENABLE_DOWNLOADS=1 to automatically download and build required components from source. -- If you are inside a firewall, you may need to use an https proxy: export https_proxy=http://example.com:80 -- Performing Test HAVE_MISLEADING_INDENTATION -- Performing Test HAVE_MISLEADING_INDENTATION - Success -- executable target mysqld debug_target /root/mysql-5.7.32/debug/sql/mysqld # ...这里省略很多输出内容... -- Configuring done -- Generating done -- Build files have been written to: /root/mysql-5.7.32/bld [root@centos bld]# pwd /root/mysql-5.7.32/bld [root@centos bld]# 

cmake执行成功后,提示编译好的文件写到了bld文件夹下面,我们看下这个里面都有什么内容,如下所示:

[root@centos bld]# pwd /root/mysql-5.7.32/bld [root@centos bld]# ls -lstr total 376   4 -rw-r--r--  1 root root     88 Jan 21 15:45 VERSION.dep   4 drwxr-xr-x  3 root root   4096 Jan 21 15:56 cmd-line-utils   4 drwxr-xr-x 13 root root   4096 Jan 21 15:57 storage   4 drwxr-xr-x 18 root root   4096 Jan 21 15:57 plugin   4 -rw-r--r--  1 root root    121 Jan 21 16:15 find_libevent_version.c   4 drwxr-xr-x  4 root root   4096 Jan 21 16:15 rapid   8 -rw-r--r--  1 root root   7242 Jan 21 16:15 make_dist.cmake   4 drwxr-xr-x  2 root root   4096 Jan 21 16:15 archive_output_directory   8 -rw-r--r--  1 root root   7030 Jan 21 16:15 info_macros.cmake   4 drwxr-xr-x  2 root root   4096 Jan 21 16:15 Docs   4 drwxr-xr-x 10 root root   4096 Jan 21 16:15 packaging   8 -rw-r--r--  1 root root   4158 Jan 21 16:15 CPackConfig.cmake   8 -rw-r--r--  1 root root   4894 Jan 21 16:15 CPackSourceConfig.cmake  80 -rw-r--r--  1 root root  80190 Jan 21 16:15 CMakeCache.txt 124 -rw-r--r--  1 root root 123266 Jan 21 16:15 Makefile   8 -rw-r--r--  1 root root   7813 Jan 21 16:15 cmake_install.cmake   4 -rw-r--r--  1 root root   2190 Jan 21 16:15 CTestTestfile.cmake   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 zlib   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 include   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 dbug   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 strings   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 vio   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 regex   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 mysys   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 mysys_ssl   4 drwxr-xr-x  4 root root   4096 Jan 21 16:15 libmysql   4 drwxr-xr-x  6 root root   4096 Jan 21 16:15 libbinlogevents   4 drwxr-xr-x  5 root root   4096 Jan 21 16:15 libbinlogstandalone   4 drwxr-xr-x  6 root root   4096 Jan 21 16:15 unittest   4 drwxr-xr-x  5 root root   4096 Jan 21 16:15 extra   4 drwxr-xr-x  5 root root   4096 Jan 21 16:15 client   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 libservices   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 man   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 testclients   4 drwxr-xr-x  4 root root   4096 Jan 21 16:15 sql   4 drwxr-xr-x  4 root root   4096 Jan 21 16:15 libmysqld   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 scripts   4 drwxr-xr-x  4 root root   4096 Jan 21 16:15 mysql-test   4 drwxr-xr-x  3 root root   4096 Jan 21 16:15 support-files   4 drwxr-xr-x 10 root root   4096 Jan 21 16:15 CMakeFiles [root@centos bld]# 

cmake执行成功后,接下来我们再来执行make命令。但是在执行的过程中,又遇到如下错误:

[root@centos bld]# make CMake Error at /root/mysql-5.7.32/cmake/do_abi_check.cmake:86 (MESSAGE):   ABI check found difference between   /root/mysql-5.7.32/include/mysql/plugin_audit.h.pp and   /root/mysql-5.7.32/bld/abi_check.out   make[2]: *** [CMakeFiles/abi_check.dir/build.make:57: CMakeFiles/abi_check] Error 1 make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/abi_check.dir/all] Error 2 make: *** [Makefile:163: all] Error 2 [root@centos bld]# make CMake Error at /root/mysql-5.7.32/cmake/do_abi_check.cmake:86 (MESSAGE):   ABI check found difference between   /root/mysql-5.7.32/include/mysql/plugin_audit.h.pp and   /root/mysql-5.7.32/bld/abi_check.out   make[2]: *** [CMakeFiles/abi_check.dir/build.make:57: CMakeFiles/abi_check] Error 1 make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/abi_check.dir/all] Error 2 make: *** [Makefile:163: all] Error 2 [root@centos bld]# 

错误原因:软件包的版本不一致导致,执行yum update更新系统中软件包。如下:

[root@centos bld]# yum update 

待执更新完成后,再次执行make命令,编译成功,这个过程是整个编译过程最费时间的,大概需要1个小时左右的数据,如果服务器配置好,应该会更快。执行过程如下所示:

[root@centos bld]# make [  0%] Built target abi_check [  0%] Built target INFO_SRC # ...此处省略编译过程的输出内容... [100%] Linking CXX executable my_safe_process [100%] Built target my_safe_process [root@centos bld]# 

安装MySQL数据库

使用源码编译安装MySQL

编译好MySQL的源码之后,会生成MySQL的二级制安装文件,此时我们需要执行安装MySQL的命令的。使用如下的命令进行MySQL的安装。此时会将编译好的mysql安装到默认的目录/usr/local/mysql下面。

安装过程如下:

[root@centos bld]# make install [  0%] Built target abi_check [  0%] Built target INFO_SRC [  0%] Built target INFO_BIN [  0%] Built target zlib [  1%] Built target edit # ...省略输出内容 [ 98%] Built target sql_embedded [100%] Built target mysqlserver [100%] Built target mysqltest_embedded [100%] Built target mysql_embedded [100%] Built target mysql_client_test_embedded [100%] Built target my_safe_process Install the project... -- Install configuration: "RelWithDebInfo" # ...省略输出内容 -- Installing: /usr/local/mysql/share/aclocal/mysql.m4 -- Installing: /usr/local/mysql/support-files/mysql.server [root@centos bld]#  # 查看安装好的MySQL目录 [root@centos bld]# ls -lstr /usr/local/mysql/ total 284   4 -rw-r--r--  1 root root    587 Sep 23 12:00 README-test   4 -rw-r--r--  1 root root    587 Sep 23 12:00 README 244 -rw-r--r--  1 root root 247914 Sep 23 12:00 LICENSE   4 drwxr-xr-x  2 root root   4096 Jan 22 06:29 docs   4 drwxr-xr-x  3 root root   4096 Jan 22 06:29 include   4 drwxr-xr-x  4 root root   4096 Jan 22 06:29 man   4 drwxr-xr-x  4 root root   4096 Jan 22 06:30 lib   4 drwxr-xr-x  2 root root   4096 Jan 22 06:30 bin   4 drwxr-xr-x 10 root root   4096 Jan 22 06:30 mysql-test   4 drwxr-xr-x 28 root root   4096 Jan 22 06:30 share   4 drwxr-xr-x  2 root root   4096 Jan 22 06:30 support-files [root@centos bld]# 

如果你想要安装到指定的目录,而不是默认的目录,需要在make install命令后面增加上参数DESTDI=/xx/xx/xx,例如下的命令是把编译好的MySQL安装在/var/lib/mysql下面。

make install DESIDI=/var/lib/mysql 

生成二进制安装包

使用下面的make package命令,可以把编译好的MySQL二进制文件生成一个.tar.gz结尾的二进制文件压缩包,便于可以安装在任何你想安装的任何位置或任何服务器上,而不是直接安装在当前执行编译过程的这台服务器上。这样,我们就可以参考前面我们介绍的使用二进制文件来安装MySQL的文章来安装我们的MySQL到任何地方。这个二进制文件安装包和我们在MySQL官网下载的二进制安装包基本一致,只是这个二进制压缩包是我们自己编译生成的。

打包生成二进制安装包的示例如下:

[root@centos bld]# make package [  0%] Built target abi_check [  0%] Built target zlib [  2%] Built target edit [100%] Built target mysql_client_test_embedded [100%] Built target my_safe_process Run CPack packaging tool... CPack: Create package using TGZ CPack: Install projects CPack: - Run preinstall target for: MySQL CPack: - Install project: MySQL CPack: Create package CPack: - package: /root/mysql-5.7.32/bld/mysql-5.7.32-linux-x86_64.tar.gz generated. [root@centos bld]# 

使用源码编译安装MySQL

初始化MySQL

MySQL已经根据源码编译,并且安装在了/usr/local/mysql目录下面,接下来我们就要初始化MySQL数据库了。初始化的操作,和前面我们分享的使用二进制压缩包安装MySQL文章中的初始化步骤一样,详细步骤可以参考前面的文章。这里简单列举一下重要的执行步骤。

创建系统用户

使用源码编译安装MySQL

前面的步骤我们已经编译好了MySQL源码。接下来就是安装了,此时的安装和使用二进制文件安装MySQL的步骤一样,在使用源码编译安装MySQL的时候,也需要创建一个基于操作系统层级的用户mysql,因为这个用户在执行MySQL初始化的时候,需要用到。

具体添加用户和组的命令如下:

[root@centos bld]# /usr/local/mysql/ [root@centos mysql]# [root@centos mysql]# groupadd mysql [root@centos mysql]# useradd -r -g mysql -s /bin/false mysql 

执行数据库的初始化

使用源码编译安装MySQL

初始化MySQL数据库实例,如下:

[root@centos mysql]# bin/mysqld --initialize --user=mysql 2021-01-22T06:55:53.809095Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-01-22T06:55:54.038257Z 0 [Warning] InnoDB: New log files created, LSN=45790 2021-01-22T06:55:54.102262Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2021-01-22T06:55:54.182466Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: deff0e7d-5c7e-11eb-94f4-0242ac110002. 2021-01-22T06:55:54.187410Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-01-22T06:55:54.585329Z 0 [Warning] CA certificate ca.pem is self signed. 2021-01-22T06:55:54.638179Z 1 [Note] A temporary password is generated for root@localhost: W?r/f1hcwShY [root@centos mysql]# bin/mysql_ssl_rsa_setup [root@centos mysql]# 

启动MySQL服务

使用源码编译安装MySQL

后台启动MySQL服务,如下:

[root@centos mysql]# bin/mysqld_safe --user=mysql & [1] 18956 [root@centos mysql]# Logging to '/usr/local/mysql/data/centos.mysqlserver.err'. 2021-01-22T06:58:13.943886Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data  [root@centos mysql]# 

登录MySQL数据库

使用源码编译安装MySQL

第一次登录MySQL数据库,登录的时候,需要使用初始化数据库的时候生成的密码。登录之后,必须要修改root的密码。

[root@centos mysql]# bin/mysql -uroot -p"W?r/f1hcwShY" mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor.  Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.32  Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.  Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> alter user 'root'@'localhost' identified by 'root'; Query OK, 0 rows affected (0.00 sec)  mysql> show databases; +--------------------+ | Database           | +--------------------+ | information_schema | | mysql              | | performance_schema | | sys                | +--------------------+ 4 rows in set (0.00 sec)  mysql> select version(); +-----------+ | version() | +-----------+ | 5.7.32    | +-----------+ 1 row in set (0.00 sec)  mysql> 

总结

使用源码编译安装MySQL

这篇文章我们介绍了如何基于MySQL的源码来安装MySQL数据库。其中包括的服务器的准备,MySQL数据库源码的下载,解压、编译、安装、创建操作系统用、初始化数据库实例、启动数据库、登录数据库等操作。

编译安装期间,涉及到步骤和和命令大概如下:

# 更新服务器系统 yum update -y # 安装编译的时候需要的包 yum install wget bison libaio-devel libtirpc* ncurses ncurses-devel openssl* gcc-c++ gcc make cmake git -y  # 因为yum仓库中没有rpcgen包,所以自己下载、编译、安装rpcgen包 cd ~ wget https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4/rpcsvc-proto-1.4.tar.gz tar -zxvf rpcsvc-proto-1.4.tar.gz cd rpcsvc-proto-1.4 ./configure make make install   # 下载源码包 wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.32.tar.gz # 解压源码包 tar -zxvf mysql-boost-5.7.32.tar.gz # 配置编译的时候使用的boost文件和目录 cd mysql-5.7.32/ mv boost /usr/local  # 创建编译时候使用的目录 mkdir bld cd bld/  # 编译 cmake .. cmake . -DWITH_BOOST=/usr/local/boost make  # 安装 make install  # 生成二级制安装包 make package  # 创建操作系统用户组和用 groupadd mysql useradd -r -g mysql -s /bin/false mysql  # 初始化MySQL数据库实例 /usr/local/mysql/bin/mysqld --initialize --user=mysql  # 启动MySQL数据库服务 /usr/local/mysql/bin/mysqld_safe --user=mysql &  # 登录MySQL数据库 /usr/local/mysql/bin/mysql -uroot -p"W?r/f1hcwShY"