适用环境:
数据库版本:MySQL 5.7.29 操作系统:CentOS 7
制作思路:
将数据库初始化和配置工作放到安装脚本中方便定制: 1、打包MySQL应用目录 2、不自动生成配置文件 3、不自动生成数据目录 4、不自动初始化数据
制作步骤:
1、安装依赖包
yum -y install gperf zlib-devel time ncurses-devel gcc* cmake libaio-devel rpm-build
2、创建打包用户和目录
## 创建打包用户 ##
useradd rpmbuilder
## 切换到 rpmbuilder用户 ##
su rpmbuilder
## 使用rpmbuild创建相关文件夹 ##
rpmbuild ~
3、上传SRC包文件并安装
## 安装RPM包后会将
rpm -ivh mysql-community-5.7.29-1.el7.src.rpm
## 查看源码目录大小
## 因为包含mysql-5.6.45+mysql-5.7.29+boost_1_59_0三个包,因此整个SOURCE目录较大
cd /home/rpmbuilder/rpmbuild
du -sh *
4、下载Cmake包并上传至目标服务器并安装
## Cmake下载路径:https://cmake.org/download/ ## 上传至随意目标路径并解压 ## 本例中解压到/tmp/cmake-3.17.1-Linux-x86_64 ## mysql.spec文件中有使用到该路径 tar -xvf cmake-3.17.1-Linux-x86_64.tar.gz
5、编辑mysql.spec文件并替换
## 切换到 SPECS 目录,然后上传 mysql.spec 文件 cd /home/rpmbuilder/rpmbuild/SPECS
## 主要修改mysql.spec的各类文件路径
6、制作RPM包
## 制作 RPM 包 cd /home/rpmbuilder/rpmbuild/SPECS rpmbuild -bb mysql.spec
7、制作完成后,会在/home/rpmbuilder/rpmbuild/RPMS/x86_64目录下生成安装包
Spec代码:
# ========================================= # # MySQL参数配置 # mysql源码路径 # 源码包版本 %global src_base mysql %global version 5.7.29 %global src_dir %{src_base}-%{version} %global cmake_bin /tmp/cmake-3.17.1-Linux-x86_64/bin/cmake Name: %{src_base} Version: %{version} # 安装版本 Release: 1 # 安装包介绍 Summary: MySQL Server # 遵循协议 License: GPLv2 # 打包需要 # BuildRequires: cmake /sbin/useradd /sbin/groupadd /bin/bash /bin/sh /bin/chown # 安装需要 # Requires: ncurses ncurses-devel pcre numactl-libs numactl bison libaio libaio-devel # 安装的虚拟环境,在rpmbuild/BUILDROOT目录下 BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-XXXXXX) # Sorce信息 Source0: %{name}-%{version}.tar.gz Source10: boost_1_59_0.tar.bz2 Source90: filter-provides.sh Source91: filter-requires.sh %if 0%{?rhel} > 6 # For rpm => 4.9 only: https://fedoraproject.org/wiki/Packaging:AutoProvidesAndRequiresFiltering %global __requires_exclude ^perl\((GD|hostnames|lib::mtr|lib::v1|mtr_|My::) %global __provides_exclude_from ^(/usr/share/(mysql|mysql-test)/.*|%{_libdir}/mysql/plugin/.*\.so)$ %else # https://fedoraproject.org/wiki/EPEL:Packaging#Generic_Filtering_on_EPEL6 %global __perl_provides %{SOURCE90} %global __perl_requires %{SOURCE91} %endif # mysql安装目录 %define mysql_server_path /apps/srv/mysql/server # mysql数据目录 %define mysql_data_path /apps/data/mysql/data %define MYSQL_USER mysql %define MYSQL_GROUP mysql # mysql配置文件目录 %define mysql_conf_path /apps/srv/mysql/server/etc/my.cnf # mysql描述 %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. # ========================================= # # 环境准备 %prep rm -rf $RPM_BUILD_ROOT/%{name}-%{version} # setup把boost和mysql压缩包解压到下面路径 # ./rpmbuild/BUILD/mysql-5.7.28/mysql-5.7.28/ # ./rpmbuild/BUILD/mysql-5.7.28/boost_1_59_0/ %setup -q -T -a 0 -a 10 -c -n %{src_dir} groupadd %{MYSQL_GROUP} >/dev/null 2>&1 || : useradd -g %{MYSQL_GROUP} %{MYSQL_USER} -s /bin/nologin >/dev/null 2>&1 || : # ========================================= # # 编译 %build %{cmake_bin} ./%{src_dir} -DCMAKE_INSTALL_PREFIX=%{mysql_server_path} -DMYSQL_DATADIR=%{mysql_data_path} -DSYSCONFDIR=%{mysql_server_path}/etc -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=%{mysql_data_path}/tmp/mysql.sock -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=. make -j3 %{?_smp_mflags} # ========================================= # # 安装 %install rm -rf %{buildroot} make DESTDIR=%{buildroot} install # ========================================= # # 制作RPM包 %files %defattr(-, %{MYSQL_USER}, %{MYSQL_GROUP}) %attr(755, %{MYSQL_USER}, %{MYSQL_GROUP}) %{mysql_server_path}/* %changelog
安装脚本(主要修改账号):
#!/bin/bash ##========================================================## ## variables: mysql_rpm_package_name="mysql-5.7.28-el7.rpm" mysql_data_path="/apps/data/mysql/data" mysql_server_path="/apps/srv/mysql/server" mysql_cnf_path=${mysql_server_path}/etc/my.cnf install_script_dir="$( cd "$( dirname "$0" )" && pwd )" install_log="${install_script_dir}/install.log" ##========================================================## ## return code error_input_parameters=10 error_check_enviorment=11 error_install_mysql_dependence=12 error_install_mysql_package=13 error_mysql_install_db=14 error_start_mysql_service=15 error_grant_mysql_user=15 ##========================================================## function echo_info() { message=$1 echo -e "