Centos 7 製作MySQL 5.7 RPM包

百老汇大管家發表於2024-08-31

系統:Centos 7.2

1、安裝依賴包

yum install make gcc rpm-build rpmdevtools -y
yum install make cmake gcc gcc-c++ bison libaio ncurses-devel perl perl-DBI perl-DBD-MySQL perl-Time-HiRes readline-devel numactl zlib-devel curldevel

2、rpm工具相關路徑

mkdir -p /root/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

[root@A01-R21-I102-44-A000324 soft]# cd /root/rpmbuild/
[root@A01-R21-I102-44-A000324 rpmbuild]# ll
total 0
drwxr-xr-x 2 root root 6 Aug 29 14:03 BUILD
drwxr-xr-x 2 root root 6 Aug 29 14:03 RPMS
drwxr-xr-x 2 root root 6 Aug 29 14:03 SOURCES
drwxr-xr-x 2 root root 6 Aug 29 14:03 SPECS
drwxr-xr-x 2 root root 6 Aug 29 14:03 SRPMS

3、MySQL 原始碼包

將MySQL原始碼包複製到my.cnf檔案也要複製放到 /root/rpmbuild/SOURCES 目錄下

[root@A01-R21-I102-44-A000324 SOURCES]# pwd
/root/rpmbuild/SOURCES
[root@A01-R21-I102-44-A000324 SOURCES]# ll
total 52056
-rw-r--r-- 1 root root     3702 Aug 29 14:08 my.cnf
-rw-r--r-- 1 root root 53298645 Aug 29 14:07 mysql-boost-5.7.44.tar.gz

4、boost依賴包

將boost包複製到 /root/rpmbuild/BUILD 目錄下,並進行解壓 (原始碼包中的boost解壓後才能用,即手動安裝時才不需要單獨下載)

[root@A01-R21-I102-44-A000324 BUILD]# pwd
/root/rpmbuild/BUILD
[root@A01-R21-I102-44-A000324 BUILD]# ll
total 81756
drwx------ 8  501   20     4096 Aug 12  2015 boost_1_59_0
-rw-r--r-- 1 root root 83709983 Aug 29 14:10 boost_1_59_0.tar.gz

5、spec檔案

在路徑 /root/rpmbuild/SPECS 下建立spec配置檔案

[root@A01-R21-I102-44-A000324 SPECS]# pwd
/root/rpmbuild/SPECS
[root@A01-R21-I102-44-A000324 SPECS]# cat mysql.5.7.44.spec 
Name:       mysql
Version:    5.7.44
Release:    1%{?dist}
License:    GPL
URL:        http://downloads.mysql.com/archives/get/file/mysql-5.7.44.tar.gz
Group:       applications/database
Source:      %{name}-boost-%{version}.tar.gz
BuildRoot:  %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
BuildRequires:  cmake
Packager:   abc@123.com
Autoreq:    no
prefix: /export/servers/mysql
Summary: MySQL 5.7.44


%description
The MySQL(TM) software delivers a very fast, multi-threaded, multi-user,
and robust SQL (Structured Query Language) database server. MySQL Server
is intended for mission-critical, heavy-load production systems as well
as for embedding into mass-deployed software.

%define MYSQL_USER mysql
%define MYSQL_GROUP mysql

# 準備構建環境的腳步,通常用於解壓原始碼
%prep
%setup -n mysql-%{version}

# 構建軟體包指令碼,通常用於編譯原始碼
%build

cmake \
-DCMAKE_INSTALL_PREFIX=/export/servers/mysql \
-DMYSQL_DATADIR=/export/data/mysql/data \
-DSYSCONFDIR=/export/servers/mysql/etc \
-DMYSQL_UNIX_ADDR=/export/data/mysql/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8mb4 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DMYSQL_TCP_PORT=3358 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=../boost_1_59_0

make -j `cat /proc/cpuinfo | grep processor| wc -l`

# 安裝軟體包指令碼
%install
rm -rf %{buildroot}
make install DESTDIR=%{buildroot}
cp %{_sourcedir}/my.cnf $RPM_BUILD_ROOT%{prefix}/

# 安裝前執行指令碼
%pre
groupadd mysql
useradd -g mysql -s /bin/nologin -M mysql >/dev/null 2>&1

mkdir -p /export/servers/mysql/etc
mkdir -p /export/data/mysql/data
mkdir -p /export/data/mysql/dumps
mkdir -p /export/data/mysql/log
mkdir -p /export/data/mysql/tmp
mkdir -p /export/data/mysql/undo

chown -R mysql:mysql /export/data/mysql

# 安裝後執行指令碼
%post
/bin/cp %{prefix}/support-files/mysql.server /etc/init.d/mysql
/bin/mv %{prefix}/my.cnf /export/servers/mysql/etc/my.cnf
chkconfig mysql on
%{prefix}/bin/mysqld --defaults-file=/export/servers/mysql/etc/my.cnf --initialize-insecure --user=mysql
service mysql start
echo "export PATH=$PATH:/export/servers/mysql/bin;" >> /etc/profile
source /etc/profile

# 解除安裝前執行指令碼
%preun
service mysql stop
chkconfig --del mysql
userdel -r mysql >/dev/null 2>&1
rm -rf %{prefix} >/dev/null 2>&1
rm -rf /export/data/mysql >/dev/null 2>&1
rm -rf /etc/init.d/mysql >/dev/null 2>&1

%files
%defattr(-, %{MYSQL_USER}, %{MYSQL_GROUP})
%attr(755, %{MYSQL_USER}, %{MYSQL_GROUP}) %{prefix}/*

%changelog

上述配置檔案對應的rpm包,在centos7上安裝解除安裝都ok,配置檔案中關於設定mysql服務和開機啟動相關操作,也可以參考下面

# 複製服務指令碼
cp /export/servers/mysql/support-files/mysql.server /etc/init.d/mysql

# 新增此服務到系統服務
chkconfig --add mysql

# 新增服務後,就可以使用systemctl命令進行管理了
# 啟動服務
systemctl start mysql

# 停止服務
systemctl stop mysql

# 設定服務為開機自啟動
systemctl enable mysql

# 檢視服務執行狀態
systemctl status mysql

6 打包

cd  /root
rpmbuild -bb rpmbuild/SPECS/mysql.5.7.44.spec

# 打包完畢,會在RMPS目錄下看到2個rpm包
[root@A01-R21-I102-44-A000324 x86_64]# pwd
/root/rpmbuild/RPMS/x86_64
[root@A01-R21-I102-44-A000324 x86_64]# ll
total 232408
-rw-r--r-- 1 root root  75295324 Aug 29 15:04 mysql-5.7.44-1.el7.centos.x86_64.rpm
-rw-r--r-- 1 root root 162688444 Aug 29 15:05 mysql-debuginfo-5.7.44-1.el7.centos.x86_64.rpm

相關文章