MySQL源码编译安装
Linux环境:CentOS 7.6
MySQL版本:MySQL 5.7.37
安装路径:/usr/local/mysql
1. 创建相关目录
# 创建用户
useradd -s /sbin/nologin mysql
# 创建安装目录并进入
cd /usr/local
mkdir mysql
cd mysql
# 创建数据存放目录
mkdir data
2. 下载依赖库
yum -y install initscripts wget libaio ncurses ncurses-devel bison gcc gcc-c++ openssl openssl-devel
3. 升级gcc和g++
yum install -y centos-release-scl-rh
yum install -y centos-release-scl
# 安装gcc7
yum install devtoolset-7-gcc.x86_64
yum install devtoolset-7-gcc-c++.x86_64
# 启用
scl enable devtoolset-7 bash
# 查看版本
gcc --version
g++ --version
# 防止失效方法1:修改软连接(推荐)
mv /usr/bin/gcc /usr/bin/gcc4.8.5
ln -s /opt/rh/devtoolset-7/root/usr/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++4.8.5
ln -s /opt/rh/devtoolset-7/root/usr/bin/g++ /usr/bin/g++
mv /usr/bin/cc /usr/bin/cc4.8.5
ln -s /opt/rh/devtoolset-7/root/usr/bin/cc /usr/bin/cc
mv /usr/bin/c++ /usr/bin/c++4.8.5
ln -s /opt/rh/devtoolset-7/root/usr/bin/c++ /usr/bin/c++
# 防止失效方法2:修改环境变量
echo "source /opt/rh/devtoolset-7/enable" >>/etc/profile
4. 安装最新版cmake
# 下载
wget -c https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz
# 解压
tar zxvf cmake-3.20.2.tar.gz
# 进入解压目录
cd cmake-3.20.2
# 构建
./bootstrap
# 编译
gmake
# 安装
gmake install
# 链接 目的是添加到环境变量中
ln -s /usr/local/bin/cmake /usr/bin/cmake
5. 下载MySQL源码并解压
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.37.tar.gz
tar -zxvf mysql-boost-5.7.37.tar.gz
6. 构建、编译、安装
cd /usr/local/mysql/mysql-5.7.37
cmake -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost
make && make install
7. 更改配置文件
7.1. 打开配置文件
vim /etc/my.cnf
7.2. 写入如下内容
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
pid-file = /usr/local/mysql/data/mysql.pid
socket = /tmp/mysql.sock
log_error = /usr/local/mysql/data/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /usr/local/mysql/data/mysql-slow.log
8. 修改用户权限
cd /usr/local/
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/data
9. 初始化MySQL
cd /usr/local/mysql/bin
./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
10. 复制相关文件
cd /usr/local/mysql/support-files
cp mysql.server /etc/init.d/mysql
11. 配置环境变量
11.1. 打开环境变量文件
vim /etc/profile
11.2. 修改PATH属性,并保存退出
export MYSQL_HOME=/usr/local/mysql/
export PATH=${MYSQL_HOME}/bin:${MYSQL_HOME}/lib:$PATH
11.3. 使环境变量生效
source /etc/profile
12. 在vscode中进行构建
按下Ctrl + ,
,选择workspace
,在左侧选择extension
,再选中Cmake
,找到Configuration Args
,添加以下参数:
-DDOWNLOAD_BOOST=1
-DWITH_BOOST=/usr/local/mysql/mysql-5.7.37/boost/boost_1_59_0/
随后在vscode
底栏中点击build
即可构建
13. 调试配置
在.vscode
文件夹中创建launch.json
文件,内容如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "MySQL-debug",
"type": "cppdbg",
"request": "launch",
"program": "/usr/local/mysql/bin/mysqld",
"args": ["--user=mysql --datadir=/usr/local/mysql/data"],
"stopAtEntry": true,
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"miDebuggerArgs": "gdb",
"linux": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb"
},
"logging": {
"moduleLoad": false,
"engineLogging": false,
"trace": false
},
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"cwd": "${workspaceFolder}",
}
]
}
14. 虚拟机开放端口
14.1. 查看3306端口状态
firewall-cmd --zone=public --query-port=3306/tcp
如果输出no,则需要开放端口
14.2. 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
14.3. 防火墙重载
firewall-cmd --reload
15. 修改连接权限
use mysql;
update user set Host='%' where User='root';
flush privileges;
标签:bin,--,MySQL,编译,源码,usr,mysql,local
From: https://www.cnblogs.com/AnkleBreaker-ZHX/p/16939405.html