mysql 源码安装

1,390 阅读5分钟

@[TOC](mysql 源码安装)

安装版本信息:

  • 操作系统:CentOS-8.4.
  • mysql 版本:5.7.25

安装资源下载

mysql几种安装资源的区别:

  • 源码:需要自己编译,进行安装,对外部操作系统的依赖弱(同一份源码在不同的操作系统上都可以安装),可控性强。可以在编译的时候加入一些mysql的属性,比如安装路径,指定字符集等等。源码安装也是实际使用中常用的安装方式。
  • 二进制安装包:就是源码编译以后的安装包,安装的时候直接解压即可,但是解压的路径必须是mysql的默认路径。
  • 其他类型的安装包,比如 rpm 包,deb 包等等,视具体的操作系统而定。 本文主要以源码的安装方式进行介绍。

源码下载

安装基础工具

  • GNU工具安装 安装源码分发版的过程中会用到如下工具:GNUgunzip、GNUtar、GNUgcc(建议使用gcc2.95.2或更新版)以及GNU make。所有Linux系统以及大多数UNIX系统通常都预装了上述除了 gcc 之外的工具。如果你的系统没有预装上述工具,可以到GNU项目的网站(www.gnu.org)下载。CentOS下gcc可以勇如下命令安装:
yum -y install gcc  
yum -y install gcc-c++  
  • cmake 安装
# 安装 cmake
yum -y install cmake 
# 查看 cmake 是否安装成功
cmake -version

安装 mysql

  • 解压源码
# 解压源码
tar zxvf mysql-5.7.25.tar.gz
  • 编译源码
cmake . -DCMAKE_INSTALL_PREFIX=/root/mysql/5.7.25 \
-DMYSQL_DATADIR=/root/mysql/5.7.25/data \
-DMYSQL_UNIX_ADDR=/root/mysql/5.7.25/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0 \
-DWITH_BOOST=/root/packages/boost_1_59_0 \
 -DWITH_SSL=system
  • 配置环境变量
# 编辑环境变量配置文件
[root@centos-8 conf]# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin

#刷新配置文件,使刚刚配置的内容生效
[root@centos-8 conf]# source /etc/profile

# 验证配置是否成功
[root@centos-8 conf]# mysql --version  
mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using  EditLine wrapper
  • 编辑配置文件 文件路径可以自定义,比如我用的路径为:/root/mysql/5.7.25/conf/my.conf,配置文件内容如下:
[client]
port    = 3306
socket  = /root/mysql/5.7.25/tmp/mysql.sock

[mysql]
prompt="\u@mysqldb \R:\m:\s [\d]> "
no-auto-rehash

[mysqld]
user    = mysql
port    = 3306
basedir = /root/mysql/5.7.25
datadir = /root/mysql/5.7.25/data
tmpdir = /root/mysql/5.7.25/tmp
socket  = /root/mysql/5.7.25/tmp/mysql.sock
pid-file = mysqldb.pid
character-set-server = utf8mb4
skip_name_resolve = 1
  • 初始化数据库
# 初始化命令
mysqld --defaults-file=/root/mysql/5.7.25/conf/my.conf --initialize

#过程内容:其中 A temporary password is generated for root@localhost: =6Y%efgppv.% 中的密码需要记录,用于首次登录mysql用
2021-08-19T07:51:51.005285Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-19T07:51:51.025009Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-19T07:51:51.084895Z 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: 5033796f-00c2-11ec-8b75-000c29e95284.
2021-08-19T07:51:51.086720Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-19T07:51:51.264324Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-19T07:51:51.374192Z 1 [Note] A temporary password is generated for root@localhost: =6Y%efgppv.%
2021-08-19T07:51:51.619097Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2021-08-19T07:51:51.619132Z 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
2021-08-19T07:51:51.619138Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2021-08-19T07:51:51.619149Z 1 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
2021-08-19T07:51:51.619152Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2021-08-19T07:51:51.619159Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2021-08-19T07:51:51.619198Z 1 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
2021-08-19T07:51:51.619213Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
  • 启动数据库
mysqld --defaults-file=/root/mysql/5.7.25/conf/my.conf
  • 登录数据库,进行初始换设置
[root@txy-server ~]# mysql -uroot -p'=6Y%efgppv.%'
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';  # 重置密码
mysql> use mysql;
mysql> update user set host ='%' where user='root';  # 开放远程登录,生产环境中不建议放开root用户的远程连接
mysql> FLUSH PRIVILEGES;

常见问题

重新编译时记得清除缓存:

rm -f CMakeCache.txt
  1. 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.
  • 原因:make 未安装
# 安装 make
yum install -y make
  1. CMake Error at cmake/boost.cmake:81 (MESSAGE):You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=
  • 原因:mysql5.7依赖于boost1.59,非系统默认的boost版本
  • 下载boost包:sourceforge.net/projects/bo…
  • 安装 boost1.59
  • cmake 命令中指定 boots 路径:
#安装 boots
tar -zxvf boost_1_59_0.tar.gz 

# camke 增加的参数
-DWITH_BOOST=/root/packages/boost_1_59_0
  1. CMake Error: The source directory "/root/packages/boost_1_59_0" does not appear to contain CMakeLists.txt.
  • 原因:没有在mysql源码的解压路径下执行命令
  • 先 cd 到 mysql 的解压路径下,再执行cmake
  1. CMake Error at cmake/ssl.cmake:244 (MESSAGE): Cannot find appropriate system libraries for SSL. Make sure you've specified a supported SSL version. Consult the documentation for WITH_SSL alternatives
  • 原因 没有安装 open-ssl
 yum install -y openssl-devel
  1. Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)
yum -y install ncurses-devel
  1. CMake Error at rapid/plugin/group_replication/rpcgen.cmake:97 (MESSAGE):
# 下载rpcsvc包路径: https://github.com/thkukuk/rpcsvc-proto/releases
tar -zxvf rpcsvc-proto-1.4.tar.gz
cd rpcsvc-proto-1.4/ 
./configure && make && make install
  1. 报错找不到rpc.h头文件 原因:这是因为,在Centos8中使用了新库libtirpc 解决方法:安装libtirpc-devel
 yum -y install libtirpc-devel