MySQL从源码编译安装

Posted Practitioner

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL从源码编译安装相关的知识,希望对你有一定的参考价值。

基于版本

CentOS-7-x86_64-Minimal-2009

mysql-boost-8.0.25.tar.gz

注:wget下载如果不稳定,可以手动下载好mysql-boost-8.0.25.tar.gz

下载地址见:https://dev.mysql.com/downloads/mysql/

新建用户和用户组

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

准备必要文件夹并赋予相应的权限

# 准备必要文件夹
mkdir -p /usr/local/mysql /data/mysql/

# 赋予相应的权限
chown -R mysql.mysql /usr/local/mysql/
chown -R mysql.mysql /data/mysql/
chmod -R 755 /data/mysql/
chmod -R 755 /usr/local/mysql/

安装必要依赖

yum -y install wget gcc gcc-c++ ncurses ncurses-devel libaio-devel openssl openssl-devel git
yum install -y centos-release-scl-rh 
yum install -y centos-release-scl
yum install -y devtoolset-10-gcc 
yum install -y devtoolset-10-gcc-c++

执行

source /opt/rh/devtoolset-10/enable

通过wget下载cmake3,如果不稳定,可以手动下载cmake-3.20.2-Linux-x86_64.tar.gz并上传到/usr/local/src目录下

# 卸载原有的cmake
yum -y remove cmake

cd /usr/local/src
# 下载,如果不稳定,可以手动下载好并上传到/usr/local/src目录
wget https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2-Linux-x86_64.tar.gz

解压并安装cmake3

tar -xf cmake-3.20.2-linux-x86_64.tar.gz
mv cmake-3.20.2-linux-x86_64 /usr/local/cmake
vi /etc/profile

在文件末尾加上

export CMAKE_PATH=/usr/local/cmake
export PATH=$PATH:$CMAKE_PATH/bin

然后执行

source /etc/profile

验证cmake

[root@db01 src]# cmake -version
cmake version 3.20.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

编译安装MySQL

将mysql-boost-8.0.25.tar.gz放到/usr/local/src目录下

cd /usr/local/src

# 解压
tar -xf mysql-boost-8.0.25.tar.gz

cd mysql-8.0.25/

执行cmake,注意boost的路径要修改正确

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DWITH_BOOST=/usr/local/src/mysql-8.0.25/boost -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=ON -DWITH_INNODB_MEMCACHED=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DFORCE_INSOURCE_BUILD=1

漫长等待后.......

如果执行无误,最后会提示

...
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/mysql-8.0.25

执行make

cd /usr/local/src/mysql-8.0.25/

make

漫长等待后.......

执行make可能会报如下错误

during RTL pass: vartrack
/usr/local/src/mysql-8.0.25/sql/opt_range.cc: In function ‘TRP_RANGE* get_key_scans_params(PARAM*, SEL_TREE*, bool, bool, const Cost_estimate*)’:
/usr/local/src/mysql-8.0.25/sql/opt_range.cc:5232:1: internal compiler error: Segmentation fault
 5232 | }
      | ^
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
The bug is not reproducible, so it is likely a hardware or OS problem.
make[2]: *** [sql/CMakeFiles/sql_main.dir/opt_range.cc.o] Error 1
make[1]: *** [sql/CMakeFiles/sql_main.dir/all] Error 2
make: *** [all] Error 2

可以先忽略,重新执行

make

然后make install

make install

初始化

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

修改mysql配置(可选)

cd /etc
cp my.cnf my.cnf.backup

vi /etc/my.cnf

修改成以下内容:

[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8

[mysqld]
server-id=1

port=3306
user=mysql
max_connections=200
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
datadir=/data/mysql
pid-file=/data/mysql/mysql.pid
init-connect=\'SET NAMES utf8\'
character-set-server=utf8
default-storage-engine=INNODB
log_error=/data/mysql/mysql-error.log
slow_query_log_file=/data/mysql/mysql-slow.log
[mysqldump]
quick
max_allowed_packet=16M

配置环境变量和启动脚本

环境变量

echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile

source /etc/profile

启动脚本

cp /usr/local/src/mysql-8.0.25/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

启动

service mysqld restart

登录

mysql -u root -p

按回车,即可无密码登录

如果想配置root的密码,可以在无密码登录后执行:

ALTER USER \'root\'@\'localhost\' IDENTIFIED BY \'MyNewPass\';

flush privileges;

增加到开机启动

chkconfig --level 345 mysqld on

测试是否开机启动,执行reboot

reboot

重启完毕后

ps -ef|grep mysqld

显示mysql已经启动了

root       1094      1  0 12:57 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
mysql      1452   1094  9 12:57 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql-error.log --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306

参考文档

Installing MySQL Using a Standard Source Distribution

Centos7源码编译安装mysql8

CentOS7.2 源码安装MySQL8.0

linux设置mysql开机启动

以上是关于MySQL从源码编译安装的主要内容,如果未能解决你的问题,请参考以下文章

MySQL从源码编译安装

源码编译安装Mysql5.7版本

树莓派开发笔记(十五):树莓派4B+从源码编译安装mysql数据库

Android 插件化VirtualApp 源码分析 ( 目前的 API 现状 | 安装应用源码分析 | 安装按钮执行的操作 | 返回到 HomeActivity 执行的操作 )(代码片段

linux下Mysql 8.0.19 编译安装

Linux下源码安装编译mysql数据库