Centos7.5源码安装mysql5.7

Centos7.5源码安装MySQL5.7

(一)、MySQL 5.7的主要特性

MySQL 5.7的主要特性:

  • 原生支持Systemd
  • 更好的性能:对于多核CPU、固态硬盘、锁有着更好的优化
  • 更好的InnoDB存储引擎
  • 更为健壮的复制功能:复制带来了数据完全不丢失的方案,传统金融客户也可以选择使用MySQL数据库。
  • 注: mysql-5.6.3已经支持了多线程的主从复制
  • 新增sys库:以后这会是DBA访问最频繁的库

MySQL官网:https://www.mysql.com/

(二)、源码安装

一、下载MySQL的相关安装包

1.系统环境:centos7.5 x86_64

[root@MySQL ~]# uname -r
3.10.0-862.el7.x86_64
[root@MySQL ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 

因为centos7.2默认安装了mariadb-libs,所以先要卸载掉
查看是否安装mariadb
rpm-qa l grep mariadb
卸载mariadb
rpm -e --nodeps mariadb-libs

[root@MySQL ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@MySQL ~]# rpm -e mariadb-libs --nodeps
[root@MySQL ~]# rpm -qa |grep mariadb
[root@MySQL ~]# 

2.安装依赖包

(1)相关依赖包的作用
  • cmake:由于从MySQL5.5版本开始弃用了常规的configure编译方法,所以需要CMake编译器,用于设置mysql的编译参数。如:安装目录、数据存放目录、字符编码、排序规则等。

  • Boost:从MySQL 5.7.5开始Boost库是必需的,mysql源码中用到了C++的Boost库,要求必须安装boost1.59.0或以上版本

  • GCC:是Linux下的C语言编译工具,mysql源码编译完全由C和CH编写,要求必须安装GCC

  • bison:Linux 下C/C++语法分析器

  • ncurses:字符终端处理库

(2)安装文件准备
1)下载mysq1-5.7.31.tar.gz

登录mysql官网https://dev.mysql.com,单击“DOWNLOADS”

Centos7.5源码安装mysql5.7_第1张图片

点击“MySQL Community (GPL) Downloads »”

Centos7.5源码安装mysql5.7_第2张图片

选择“MySQL Community Server”

Centos7.5源码安装mysql5.7_第3张图片

当前最新的版本为MySQL Community Server 8.0.21,但是企业中使用5.7的比较多

查找以前的版本,单击“[Looking for previous GA versions? ”

Centos7.5源码安装mysql5.7_第4张图片

Select Version:5.7.31;

Select Operating System:Source Code ;

Select OS Version:All

Centos7.5源码安装mysql5.7_第5张图片

选择“Generic Linux (Architecture Independent), Compressed TAR Archive(mysql-5.7.31.tar.gz)”,点击“Download”

Centos7.5源码安装mysql5.7_第6张图片

选择“No thanks, just start my download.”

Centos7.5源码安装mysql5.7_第7张图片

Centos7.5源码安装mysql5.7_第8张图片

完整下载链接:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.31.tar.gz

2)下载cmake-3.18.1.tar.gz

登录cmake官网https://cmake.org/,单击“Dwonload”

Centos7.5源码安装mysql5.7_第9张图片

选择最新版本“Unix/Linux Source (has in line feeds) cmake-3.18.1.tar.gz”

Centos7.5源码安装mysql5.7_第10张图片

Centos7.5源码安装mysql5.7_第11张图片

完整下载链接:https://github.com/Kitware/CMake/releases/download/v3.18.1/cmake-3.18.1.tar.gz

3)下载bison-3.6.3.tar.gz

百度搜索bison,点击“Bison - GNU Project - Free Software Foundation”

打开网址http://www.gnu.org/software/bison/

选择“Downloading Bison”下的,Bison可以在主GNU ftp服务器上找到:http://ftp.gnu.org/gnu/bison/

Centos7.5源码安装mysql5.7_第12张图片

选择较新的版本bison-3.6.3.tar.gz

Centos7.5源码安装mysql5.7_第13张图片

Centos7.5源码安装mysql5.7_第14张图片

完整下载链接:http://ftp.gnu.org/gnu/bison/bison-3.6.3.tar.gz

4)下载ncurses-6.2.tar.gz

百度搜索ncurses,点击“Index of /pub/gnu/ncurses”

打开网址http://ftp.gnu.org/pub/gnu/ncurses/

选择“ncurses-6.2.tar.gz 2020-02-12 04:54 3.3M”

Centos7.5源码安装mysql5.7_第15张图片

Centos7.5源码安装mysql5.7_第16张图片

完整下载链接:http://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.2.tar.gz

5)下载Boost_1_59_0.tar.gz

不能下载最新版本,因为MySQL 5.7使用的是1.59的库

百度搜索boost1_59,点击“boost version 1.59.0”

打开网址https://www.boost.org/users/history/version_1_59_0.html

点击“unix boost_1_59_0.tar.gz”

Centos7.5源码安装mysql5.7_第17张图片

Centos7.5源码安装mysql5.7_第18张图片

完整下载链接:https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

二、开始安装

1.将软件包使用xshell上传至centos7.5中

创建储存安装包的文件夹

[root@MySQL ~]# cd /opt
[root@MySQL opt]# ls
[root@MySQL opt]# mkdir mysql_install
[root@MySQL opt]# cd mysql_install/
[root@MySQL mysql_install]#

显示如下结果表示没有安装rz

[root@MySQL mysql_install]# rz -E
-bash: rz: command not found

安装rz和sz功能(下载:sz 文件 ;上传:rz -y 文件 )

[root@MySQL mysql_install]# yum -y install lrzsz
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
 
//文件过长,省略部分内容

Installed:
  lrzsz.x86_64 0:0.12.20-36.el7                                                                                                       
Complete!
[root@MySQL mysql_install]# 

查看lrzsz是否已经安装

[root@MySQL mysql_install]# rpm -qa | grep lrzsz
lrzsz-0.12.20-36.el7.x86_64
[root@MySQL mysql_install]# 

上传安装包

[root@MySQL mysql_install]# ll
total 151536
-rw-r--r-- 1 root root  4639293 Aug 13 07:19 bison-3.6.3.tar.gz
-rw-r--r-- 1 root root 83709983 Aug 13 07:44 boost_1_59_0.tar.gz
-rw-r--r-- 1 root root  8969366 Aug 13 07:03 cmake-3.18.1.tar.gz
-rw-r--r-- 1 root root 54418341 Aug 13 06:49 mysql-5.7.31.tar.gz
-rw-r--r-- 1 root root  3425862 Aug 13 07:34 ncurses-6.2.tar.gz
[root@MySQL mysql_install]# 

2.安装cmake

(1)开始安装
[root@MySQL mysql_install]# tar zxf cmake-3.18.1.tar.gz 	//解压
[root@MySQL mysql_install]# cd cmake-3.18.1/				//切换目录
[root@MySQL cmake-3.18.1]# ./bootstrap						//使用bootstrap编译安装
---------------------------------------------

CMake 3.18.1, Copyright 2000-2020 Kitware, Inc. and Contributors
---------------------------------------------

Error when bootstrapping CMake:
Cannot find appropriate C compiler on this system.
Please specify one using environment variable CC.
See cmake_bootstrap.log for compilers attempted.
#引导CMake时出错:在这个系统上找不到合适的C编译器。请使用环境变量指定一个。请参阅cmake_bootstrap.log了解所尝试的编译器。
---------------------------------------------

Log of errors: /opt/mysql_install/cmake-3.18.1/Bootstrap.cmk/cmake_bootstrap.log
---------------------------------------------

#此时报错:因为服务器没有安装c编译器
(2)安装gcc编译器
[root@MySQL cmake-3.18.1]# gcc -v							//查看服务器是否已安装gcc编译器
-bash: gcc: command not found
[root@MySQL cmake-3.18.1]# yum install gcc					//安装gcc编译器
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile

#内容过长,省略部分内容

Install  1 Package  (+6 Dependent packages)
Upgrade             ( 4 Dependent packages)

Total download size: 48 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.

#内容过长,省略部分内容

Installed:
  gcc.x86_64 0:4.8.5-39.el7                                                                                                      
Dependency Installed:
  cpp.x86_64 0:4.8.5-39.el7                         glibc-devel.x86_64 0:2.17-307.el7.1      glibc-headers.x86_64 0:2.17-307.el7.1     
  kernel-headers.x86_64 0:3.10.0-1127.18.2.el7      libmpc.x86_64 0:1.0.1-3.el7              mpfr.x86_64 0:3.1.1-4.el7                 
Dependency Updated:
  glibc.x86_64 0:2.17-307.el7.1   glibc-common.x86_64 0:2.17-307.el7.1   libgcc.x86_64 0:4.8.5-39.el7   libgomp.x86_64 0:4.8.5-39.el7  

Complete!
[root@MySQL cmake-3.18.1]# 
#再次编译安装
[root@MySQL cmake-3.18.1]# ./bootstrap
---------------------------------------------
CMake 3.18.1, Copyright 2000-2020 Kitware, Inc. and Contributors
C compiler on this system is: cc   
---------------------------------------------
Error when bootstrapping CMake:
Cannot find a C++ compiler that supports both C++11 and the specified C++ flags.
Please specify one using environment variable CXX.
The C++ flags are "".
They can be changed using the environment variable CXXFLAGS.
See cmake_bootstrap.log for compilers attempted.
#报错翻译:引导CMake时出错:
#找不到同时支持C++11和指定的C++标志的C++编译器。请使用环境变量CXX指定一个。C++标志是" "。可以使用环境变量CXXFLAGS来更改它们。
#请参阅cmake_bootstrap.log了解所尝试的编译器。
---------------------------------------------
Log of errors: /opt/mysql_install/cmake-3.18.1/Bootstrap.cmk/cmake_bootstrap.log
---------------------------------------------

[root@MySQL cmake-3.18.1]# 
#此时仍然报错:因为服务器缺少C++编译器
(3)安装gcc-c++编译器
[root@MySQL cmake-3.18.1]# yum install gcc-c++						
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn

#内容过长,省略部分内容

Installed:
  gcc-c++.x86_64 0:4.8.5-39.el7                                                                                                  
Dependency Installed:
  libstdc++-devel.x86_64 0:4.8.5-39.el7                                                                                         
  
Dependency Updated:
  libstdc++.x86_64 0:4.8.5-39.el7                                                                                                  
Complete!

#安装完后,继续安装cmake
[root@MySQL cmake-3.18.1]# ./bootstrap
---------------------------------------------
CMake 3.18.1, Copyright 2000-2020 Kitware, Inc. and Contributors
Found GNU toolchain
C compiler on this system is: gcc   
C++ compiler on this system is: g++  -std=gnu++1y  
Makefile processor on this system is: gmake
g++ has setenv

#内容过长,省略部分内容

-- Looking for connect in socket;dl - not found
-- Looking for gethostbyname in c
-- Looking for gethostbyname in c - found
-- Looking for recv in network;dl
-- Looking for recv in network;dl - not found
-- Looking for gethostname
-- Looking for gethostname - found
-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR) 
CMake Error at Utilities/cmcurl/CMakeLists.txt:485 (message):
  Could not find OpenSSL.  Install an OpenSSL development package or
  configure CMake with -DCMAKE_USE_OPENSSL=OFF to build without OpenSSL.
#报错翻译:找不到OpenSSL。安装OpenSSL开发包或使用-DCMAKE_USE_OPENSSL=OFF配置为不带有OPENSSL的构建。

-- Configuring incomplete, errors occurred!
See also "/opt/mysql_install/cmake-3.18.1/CMakeFiles/CMakeOutput.log".
See also "/opt/mysql_install/cmake-3.18.1/CMakeFiles/CMakeError.log".
---------------------------------------------
Error when bootstrapping CMake:
Problem while running initial CMake
---------------------------------------------

(4)安装OpenSSL
[root@MySQL cmake-3.18.1]# yum install openssl-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn

#内容过长,省略部分内容

Installed:
  openssl-devel.x86_64 1:1.0.2k-19.el7                                                                                                  

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.5.8-3.el7      krb5-devel.x86_64 0:1.15.1-46.el7         libcom_err-devel.x86_64 0:1.42.9-17.el7     
  libkadm5.x86_64 0:1.15.1-46.el7               libselinux-devel.x86_64 0:2.5-15.el7      libsepol-devel.x86_64 0:2.5-10.el7          
  libverto-devel.x86_64 0:0.2.5-4.el7           pcre-devel.x86_64 0:8.32-17.el7           zlib-devel.x86_64 0:1.2.7-18.el7            

Dependency Updated:
  e2fsprogs.x86_64 0:1.42.9-17.el7            e2fsprogs-libs.x86_64 0:1.42.9-17.el7        krb5-libs.x86_64 0:1.15.1-46.el7            
  libcom_err.x86_64 0:1.42.9-17.el7           libselinux.x86_64 0:2.5-15.el7               libselinux-python.x86_64 0:2.5-15.el7       
  libselinux-utils.x86_64 0:2.5-15.el7        libsepol.x86_64 0:2.5-10.el7                 libss.x86_64 0:1.42.9-17.el7                
  openssl.x86_64 1:1.0.2k-19.el7              openssl-libs.x86_64 1:1.0.2k-19.el7          zlib.x86_64 0:1.2.7-18.el7                  

Complete!
[root@MySQL cmake-3.18.1]# 

#继续安装cmake

[root@MySQL cmake-3.18.1]# ./bootstrap
---------------------------------------------
CMake 3.18.1, Copyright 2000-2020 Kitware, Inc. and Contributors
Found GNU toolchain
C compiler on this system is: gcc   
C++ compiler on this system is: g++  -std=gnu++1y  
Makefile processor on this system is: gmake
g++ has setenv

#由于内容过长,部分内容省略

-- Performing Test run_pic_test - Success
-- Performing Test run_inlines_hidden_test
-- Performing Test run_inlines_hidden_test - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /opt/mysql_install/cmake-3.18.1
---------------------------------------------
CMake has bootstrapped.  Now run gmake.
[root@MySQL cmake-3.18.1]# 
(5)安装gmake
[root@MySQL cmake-3.18.1]# gmake && gmake install
#内容过长,省略部分内容
-- Installing: /usr/local/share/emacs/site-lisp/cmake-mode.el
-- Installing: /usr/local/share/aclocal/cmake.m4
-- Installing: /usr/local/share/bash-completion/completions/cmake
-- Installing: /usr/local/share/bash-completion/completions/cpack
-- Installing: /usr/local/share/bash-completion/completions/ctest
[root@MySQL cmake-3.18.1]# history 
(6)查看cmake版本
[root@MySQL cmake-3.18.1]# cmake -version
cmake version 3.18.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).
[root@MySQL cmake-3.18.1]# 
(7)查看cmake的参数
[root@MySQL mysql-5.7.31]# cmake . -LH
CMake Deprecation Warning at CMakeLists.txt:35 (CMAKE_POLICY):
  The OLD behavior for policy CMP0018 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.


CMake Deprecation Warning at CMakeLists.txt:41 (CMAKE_POLICY):
  The OLD behavior for policy CMP0022 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.


CMake Deprecation Warning at CMakeLists.txt:48 (CMAKE_POLICY):
  The OLD behavior for policy CMP0045 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.


CMake Deprecation Warning at CMakeLists.txt:49 (CMAKE_POLICY):
  The OLD behavior for policy CMP0042 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.


-- Running cmake version 3.18.1
-- Could NOT find Git (missing: GIT_EXECUTABLE) 
-- Configuring with MAX_INDEXES = 64U
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- CMAKE_GENERATOR: Unix Makefiles
-- Looking for SHM_HUGETLB
-- Looking for SHM_HUGETLB - found
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of void *
-- Check size of void * - done
-- SIZEOF_VOIDP 8
-- Performing Test HAVE_C_SHIFT_OR_OPTIMIZATION_BUG
-- Performing Test HAVE_C_SHIFT_OR_OPTIMIZATION_BUG - Failed
-- Performing Test HAVE_CXX_SHIFT_OR_OPTIMIZATION_BUG
-- Performing Test HAVE_CXX_SHIFT_OR_OPTIMIZATION_BUG - Failed
-- Performing Test HAVE_C_FLOATING_POINT_FUSED_MADD
-- Performing Test HAVE_C_FLOATING_POINT_FUSED_MADD - Failed
-- Performing Test HAVE_CXX_FLOATING_POINT_FUSED_MADD
-- Performing Test HAVE_CXX_FLOATING_POINT_FUSED_MADD - Failed
-- Performing Test HAVE_C_FP_CONTRACT_FLAG
-- Performing Test HAVE_C_FP_CONTRACT_FLAG - Success
-- Performing Test HAVE_CXX_FP_CONTRACT_FLAG
-- Performing Test HAVE_CXX_FP_CONTRACT_FLAG - Success
-- MySQL 5.7.31
-- Packaging as: mysql-5.7.31-Linux-x86_64
-- Looked for boost/version.hpp in  and 
-- BOOST_INCLUDE_DIR BOOST_INCLUDE_DIR-NOTFOUND
-- LOCAL_BOOST_DIR 
-- LOCAL_BOOST_ZIP 
-- Could not find (the correct version of) boost.
-- MySQL currently requires boost_1_59_0

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 "/opt/mysql_install/mysql-5.7.31/CMakeFiles/CMakeOutput.log".
See also "/opt/mysql_install/mysql-5.7.31/CMakeFiles/CMakeError.log".
-- Cache values
// 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 prefix
CMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql

// Set to true if this is a community build
COMMUNITY_BUILD:BOOL=ON

// Download boost from sourceforge.
DOWNLOAD_BOOST:BOOL=OFF

// Timeout in seconds when downloading boost.
DOWNLOAD_BOOST_TIMEOUT:STRING=600

// Enable profiling
ENABLED_PROFILING:BOOL=ON

// 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

// default MySQL data directory
MYSQL_DATADIR:PATH=/usr/local/mysql/data

// default MySQL keyring directory
MYSQL_KEYRINGDIR:PATH=/usr/local/mysql/keyring

// Support tracing of Optimizer
OPTIMIZER_TRACE:BOOL=ON

// Take extra pains to make build result independent of build location and time
REPRODUCIBLE_BUILD:BOOL=OFF

// PATH to MySQL TMP dir. Defaults to the P_tmpdir macro in <stdio.h>
TMPDIR:PATH=P_tmpdir

// 
WITH_ARCHIVE_STORAGE_ENGINE:BOOL=ON

// Enable address sanitizer
WITH_ASAN:BOOL=OFF

// Enable -fsanitize-address-use-after-scope
WITH_ASAN_SCOPE:BOOL=OFF

// Report error if the LDAP authentication plugin cannot be built.
WITH_AUTHENTICATION_LDAP:BOOL=OFF

// 
WITH_BLACKHOLE_STORAGE_ENGINE:BOOL=ON

// Path to boost sources: a directory, or a tarball to be unzipped.
WITH_BOOST:PATH=

// 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

// 
WITH_EMBEDDED_SERVER:BOOL=ON

// Options are: none, complex, all
WITH_EXTRA_CHARSETS:STRING=all

// 
WITH_FEDERATED_STORAGE_ENGINE:BOOL=ON

// 
WITH_INNOBASE_STORAGE_ENGINE:BOOL=ON

// 
WITH_INNODB_MEMCACHED:BOOL=OFF

// By default use bundled lz4 library
WITH_LZ4:STRING=bundled

// Enable memory sanitizer
WITH_MSAN:BOOL=OFF

// 
WITH_PARTITION_STORAGE_ENGINE:BOOL=ON

// Build additonal code(plugins) that is located in rapid directory
WITH_RAPID:BOOL=ON

// 
WITH_SSL:STRING=system

// 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=bundled

[root@MySQL mysql-5.7.31]# 

3.安装bison

(1)开始安装bison
[root@MySQL mysql_install]# tar zxf bison-3.6.3.tar.gz 
[root@MySQL mysql_install]# cd bison-3.6.3
[root@MySQL bison-3.6.3]# ./configure && make && make install
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p

#内容过长,省略部分内容

checking for GNU M4 that supports accurate traces... configure: error: no acceptable m4 could be found in $PATH.
GNU M4 1.4.6 or later is required; 1.4.16 or newer is recommended.
GNU M4 1.4.15 uses a buggy replacement strstr on some systems.
Glibc 2.9 - 2.12 and GNU M4 1.4.11 - 1.4.15 have another strstr bug.
#报错内容:正在检查支持精确跟踪的GNU M4...配置:错误:在$PATH中找不到可接受的m4。
#需要GNU M4 1.4.6或更高版本;建议使用1.4.16或更新版本。
#GNU M4 1.4.15在某些系统上使用了一个有缺陷的替换strstr。
#Glibc 2.9 - 2.12和GNU M4 1.4.11 - 1.4.15还有另一个strstr错误。
[root@MySQL bison-3.6.3]# 
(2)安装m4
[root@MySQL bison-3.6.3]# yum install m4
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn

#内容过长,省略部分内容

Installed:
  m4.x86_64 0:1.4.16-10.el7                                                     

Complete!

#继续安装bison

[root@MySQL bison-3.6.3]# ./configure && make && make install
#内容过长,省略部分内容

make[3]: Leaving directory `/opt/mysql_install/bison-3.6.3'
make[2]: Leaving directory `/opt/mysql_install/bison-3.6.3'
make[1]: Leaving directory `/opt/mysql_install/bison-3.6.3'
[root@MySQL bison-3.6.3]# 

4.安装ncurses

[root@MySQL mysql_install]# tar zxf ncurses-6.2.tar.gz 
[root@MySQL mysql_install]# cd ncurses-6.2/
[root@MySQL ncurses-6.2]# ./configure && make && make install
#内容过长,省略部分内容

installing ./cursesw.h in /usr/include
installing ./cursslk.h in /usr/include
installing etip.h in /usr/include
make[1]: Leaving directory `/opt/mysql_install/ncurses-6.2/c++'
[root@MySQL ncurses-6.2]# 

5.安装boost

[root@MySQL mysql_install]# tar zxf boost_1_59_0.tar.gz 
[root@MySQL mysql_install]# ls
bison-3.6.3         boost_1_59_0         cmake-3.18.1         mysql-5.7.31.tar.gz  ncurses-6.2.tar.gz
bison-3.6.3.tar.gz  boost_1_59_0.tar.gz  cmake-3.18.1.tar.gz  ncurses-6.2
[root@MySQL mysql_install]# mv boost_1_59_0 /usr/local/boost			//移动目录并改名
[root@MySQL mysql_install]# cd /usr/local/boost
[root@MySQL boost]# ll
total 108
drwxr-xr-x 105 501 games  8192 Aug 12  2015 boost
-rw-r--r--   1 501 games   850 Aug 11  2015 boost-build.jam
-rw-r--r--   1 501 games 21920 Aug 11  2015 boostcpp.jam
-rw-r--r--   1 501 games   989 Aug 11  2015 boost.css
-rw-r--r--   1 501 games  6308 Aug 11  2015 boost.png
-rw-r--r--   1 501 games  2477 Aug 11  2015 bootstrap.bat
-rwxr-xr-x   1 501 games 10631 Aug 11  2015 bootstrap.sh
drwx------   7 501 games    84 Aug 12  2015 doc
-rw-r--r--   1 501 games   769 Aug 11  2015 index.htm
-rw-r--r--   1 501 games  5476 Aug 11  2015 index.html
-rw-r--r--   1 501 games   291 Aug 11  2015 INSTALL
-rw-r--r--   1 501 games 10779 Aug 11  2015 Jamroot
drwx------ 116 501 games  4096 Aug 12  2015 libs
-rw-r--r--   1 501 games  1338 Aug 11  2015 LICENSE_1_0.txt
drwxr-xr-x   4 501 games   159 Aug 11  2015 more
-rw-r--r--   1 501 games  2608 Aug 11  2015 rst.css
drwxr-xr-x   2 501 games   141 Aug 11  2015 status
drwxr-xr-x  10 501 games   190 Aug 11  2015 tools
[root@MySQL boost]# 

6.创建mysql用户和用户组及目录

#新建mysql组和mysql用户,禁止登录shell
[root@MySQL cmake-3.18.1]# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql
[root@MySQL cmake-3.18.1]# mkdir /usr/local/mysql				//创建目录
[root@MySQL cmake-3.18.1]# mkdir /usr/local/mysql/data			//创建数据库目录

7.安装mysql

配置命令的解释

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql			[MySQL安装的根目录]
-DMYSQL_DATADIR=/usr/local/mysql/data			[MySQL数据库文件存放目录]
-DSYSCONFDIR=/etc								[MySQL配置文件所在目录]
-DWITH_MYISAM_STORAGE_ENGINE=1					[添加MYISAM引擎支持]
-DWITH_INNOBASE_STORAGE_ENGINE=1				[添加InnoDB引擎支持]
-DWITH_ARCHIVE_STORAGE_ENGINE=1					[添加ARCHIVE引擎支持]
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock				[指定mysql.sock位置]
-DWITH_PARTITION_STORAGE_ENGINE=1				[安装支持数据库分区〕
-DEXTRA_CHARSETS=all							[使MySQL支持所有的扩展字符]
-DDEFAULT_CHARSET=utf8							[设置MySQL的默认字符集为utf8] 
-DDEFAULT_COLLATION=utf8_general_ci				[设置默认字符集校对规则]
-DWITH-SYSTEMD=1								[可以使用systemd 控制mysql服务]
-DWITH_BOOST=/usr/local/boost					[指向boost库所在目录]

开始安装

#执行cmake命令进行编译前的配置
[root@MySQL mysql-5.7.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_SYSTEMD=1 -DWITH_BOOST=/usr/local/boost
local/boost
CMake Warning:
  No source or binary directory provided.  Both will be assumed to be the
  same as the current working directory, but note that this warning will
  become a fatal error in future CMake releases.

#内容过长,省略部分内容

-- Configuring done
-- Generating done
-- Build files have been written to: /opt/mysql_install/mysql-5.7.31

#开始编译、编译安装
[root@MySQL mysql-5.7.31]# make -j 4 && make install
#-j:参数表示根据CPU核数指定编译时的线程数,可以加快编译速度,默认为1个线程编译,后面的数字为逻辑线程数,不宜太多,一般为CPU的核心数目的两倍为宜。
#查看逻辑CPU个数:cat /proc/cpuinfo | grep "processor" | wc -l

#内容过长,省略部分内容

-- Installing: /usr/local/mysql/support-files/magic
-- Installing: /usr/local/mysql/share/aclocal/mysql.m4
-- Installing: /usr/local/mysql/support-files/mysql.server
[root@MySQL mysql-5.7.31]# 

8.查看MySQL的文件,并进行配置

[root@MySQL mysql-5.7.31]# cd /usr/local
[root@MySQL local]# ll
total 0
drwxr-xr-x.  2 root root   70 Aug 13 11:56 bin
drwx------   8  501 games 302 Aug 12  2015 boost
drwxr-xr-x   3 root root   24 Aug 13 11:56 doc
drwxr-xr-x.  2 root root    6 Apr 11  2018 etc
drwxr-xr-x.  2 root root    6 Apr 11  2018 games
drwxr-xr-x.  2 root root    6 Apr 11  2018 include
drwxr-xr-x.  2 root root   20 Aug 13 11:37 lib
drwxr-xr-x.  2 root root    6 Apr 11  2018 lib64
drwxr-xr-x.  2 root root    6 Apr 11  2018 libexec
drwxr-xr-x  12 root root  189 Aug 13 13:42 mysql
drwxr-xr-x.  2 root root    6 Apr 11  2018 sbin
drwxr-xr-x. 13 root root  167 Aug 13 11:56 share
drwxr-xr-x.  2 root root    6 Apr 11  2018 src
[root@MySQL local]# cd mysql/
[root@MySQL mysql]# ll
total 296
drwxr-xr-x  2 root root   4096 Aug 13 13:41 bin
drwxr-xr-x  2 root root      6 Aug 13 12:07 data
drwxr-xr-x  2 root root     55 Aug 13 13:41 docs
drwxr-xr-x  3 root root   4096 Aug 13 13:41 include
drwxr-xr-x  4 root root    192 Aug 13 13:41 lib
-rw-r--r--  1 root root 275393 Jun  2 19:05 LICENSE
drwxr-xr-x  4 root root     30 Aug 13 13:41 man
drwxr-xr-x 10 root root   4096 Aug 13 13:42 mysql-test
-rw-r--r--  1 root root    587 Jun  2 19:05 README
-rw-r--r--  1 root root    587 Jun  2 19:05 README-test
drwxr-xr-x 28 root root   4096 Aug 13 13:42 share
drwxr-xr-x  2 root root     90 Aug 13 13:42 support-files
drwxr-xr-x  3 root root     17 Aug 13 13:41 usr
[root@MySQL mysql]# cd data/									//此时数据库时没有内容的
[root@MySQL data]# ll
total 0
[root@MySQL data]# cd /usr/local/mysql/bin/						//mysql的相关命令
[root@MySQL bin]# ls
innochecksum       mysql                       mysql_config_editor  mysql_install_db           mysqltest            resolveip
lz4_decompress     mysqladmin                  mysqld               mysql_plugin               mysqltest_embedded   resolve_stack_dump
myisamchk          mysqlbinlog                 mysqld_pre_systemd   mysqlpump                  mysql_tzinfo_to_sql  zlib_decompress
myisam_ftdump      mysqlcheck                  mysqldump            mysql_secure_installation  mysql_upgrade
myisamlog          mysql_client_test           mysqldumpslow        mysqlshow                  mysqlxtest
myisampack         mysql_client_test_embedded  mysql_embedded       mysqlslap                  perror
my_print_defaults  mysql_config                mysqlimport          mysql_ssl_rsa_setup        replace
[root@MySQL bin]#

三、MySQL的配置

1.设置mysql文件的属主和属组

[root@MySQL data]# chown -R mysql:mysql /usr/local/mysql		//改所有者,注意是root
[root@MySQL data]# 

2.mysql的初始化

(1)修改主配置文件
[root@MySQL data]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/data/mysqld.pid
socket=/tmp/mysql.sock
log-error=/usr/local/mysql/data/mysql.err                                       
(2)MySQL路径的优化

在最后一行添加即可

[root@MySQL bin]# vim /etc/profile

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
export PATH=$PATH:/usr/local/mysql/bin

[root@MySQL bin]# . /etc/profile
[root@MySQL bin]#
(3)查看mysql的帮助
[root@MySQL bin]# mysqld --help
mysqld  Ver 5.7.31 for Linux on x86_64 (Source distribution)
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.

Starts the MySQL database server.

Usage: mysqld [OPTIONS]

For more help options (several pages), use mysqld --verbose --help.
[root@MySQL bin]# mysqld --verbose --help				#查看全部帮助
#内容太长,部分内容省略

transaction-write-set-extraction                             OFF
updatable-views-with-limit                                   YES
validate-user-plugins                                        TRUE
verbose                                                      TRUE
wait-timeout                                                 28800

To see what values a running MySQL server is using, type
'mysqladmin variables' instead of 'mysqld --verbose --help'.
[root@MySQL bin]# mysqld --verbose --help | grep initialize		#过滤initialize的帮助
                      GLOBAL.GTID_PURGED may be initialized wrongly in two
  --initialize        Create the default database and exit. Create a super user
  --initialize-insecure 
initialize                                                   FALSE
initialize-insecure                                          FALSE
[root@MySQL bin]# 
(4)MySQL的初始化

# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

注 1:以 root 初始化操作时要加–user=mysql 参数,生成一个随机密码(注意保存登录时用)

注 2:MySQL 5.7.6 之前的版本执行这个脚本初始化系统数据库

/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

# 5.7.6 之后版本初始系统数据库脚本(本文使用此方式初始化)

#/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

[root@MySQL bin]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@MySQL bin]# cd ../data/
[root@MySQL data]# ll
total 110664
-rw-r----- 1 mysql mysql       56 Aug 13 20:16 auto.cnf
-rw------- 1 mysql mysql     1676 Aug 13 20:16 ca-key.pem
-rw-r--r-- 1 mysql mysql     1112 Aug 13 20:16 ca.pem
-rw-r--r-- 1 mysql mysql     1112 Aug 13 20:16 client-cert.pem
-rw------- 1 mysql mysql     1676 Aug 13 20:16 client-key.pem
-rw-r----- 1 mysql mysql      431 Aug 13 20:16 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Aug 13 20:16 ibdata1
-rw-r----- 1 mysql mysql 50331648 Aug 13 20:16 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Aug 13 20:16 ib_logfile1
drwxr-x--- 2 mysql mysql     4096 Aug 13 20:16 mysql
-rw-r----- 1 mysql mysql      880 Aug 13 20:16 mysql.err
drwxr-x--- 2 mysql mysql     8192 Aug 13 20:16 performance_schema
-rw------- 1 mysql mysql     1680 Aug 13 20:16 private_key.pem
-rw-r--r-- 1 mysql mysql      452 Aug 13 20:16 public_key.pem
-rw-r--r-- 1 mysql mysql     1112 Aug 13 20:16 server-cert.pem
-rw------- 1 mysql mysql     1676 Aug 13 20:16 server-key.pem
drwxr-x--- 2 mysql mysql     8192 Aug 13 20:16 sys
[root@MySQL data]# 

3.启动mysql脚本

(1)设置自启动
[root@MySQL data]# cd /usr/local/mysql/usr/lib/systemd/system/
[root@MySQL system]# ll
total 8
-rw-r--r-- 1 mysql mysql 2056 Aug 13 12:29 mysqld.service
-rw-r--r-- 1 mysql mysql 2087 Aug 13 12:29 [email protected]
[root@MySQL system]# cp mysqld.service /usr/lib/systemd/system/
[root@MySQL system]# systemctl daemon-reload			//重新加载
[root@MySQL system]# systemctl enable mysqld.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@MySQL system]# systemctl start mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@MySQL system]# 
(2)修改配置文件

将PIDFile=/var/run/mysqld/mysqld.pid 修改为 PIDFile=/usr/local/mysql/data/mysqld.pid
将–pid-file=/var/run/mysqld/mysqld.pid 修改为 --pid-file=/usr/local/mysql/data/mysqld.pid

[root@MySQL system]# vim /usr/lib/systemd/system/mysqld.service 
#内容过长,省略部分内容

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/usr/local/mysql/data/mysqld.pid					//修改

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS			//修改

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 5000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

[root@MySQL system]# systemctl start mysqld
[root@MySQL system]# ss -antlp | grep mysql
LISTEN     0      80          :::3306                    :::*                   users:(("mysqld",pid=21434,fd=17))
[root@MySQL system]# 

第二种方法:

若不修改配置文件,须将主配置文件的/etc/my.cnf的datadir=/usr/local/mysql/data修改为datadir=/var/run/mysqld/mysqld.pid

四、MySQL的使用

1.查找初始密码

[root@MySQL system]# grep 'password' /usr/local/mysql/data/mysql.err
2020-08-13T12:16:56.484920Z 1 [Note] A temporary password is generated for root@localhost: nZ/8kdj0ry7y
[root@MySQL system]#

2.登录

[root@MySQL system]# mysql -uroot -p
Enter password: 							//密码为nZ/8kdj0ry7y
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31

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.
#由于没有使用password模块,应用密码策略,此处可以设置简单密码,但是建议添加密码策略模块
mysql> alter user root@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@MySQL system]# 

3.使用MySQL

[root@MySQL system]# mysql -uroot -p
Enter password: 								#密码为123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 Source distribution

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;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database mytest;
Query OK, 1 row affected (0.01 sec)

mysql> use mytest;
Database changed

mysql> create table test (id int primary key auto_increment,name varchar(20));
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| test             |
+------------------+
1 row in set (0.00 sec)

mysql> desc test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> insert into test (name) values ("zhang"),("wang"),("zhao");
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test;
+----+-------+
| id | name  |
+----+-------+
|  1 | zhang |
|  2 | wang  |
|  3 | zhao  |
+----+-------+
3 rows in set (0.00 sec)

mysql> 

这只是我的一些浅薄的见解,望多指教!

你可能感兴趣的:(源码安装MySQL,mysql)