适用环境:

数据库版本:MySQL 5.7.
操作系统: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.-Linux-x86_64
## mysql.spec文件中有使用到该路径
tar -xvf cmake-3.17.-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.
%global src_dir %{src_base}-%{version}
%global cmake_bin /tmp/cmake-3.17.-Linux-x86_64/bin/cmake Name: %{src_base}
Version: %{version} # 安装版本
Release: # 安装包介绍
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 %{?rhel} >
# 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./mysql-5.7./
# ./rpmbuild/BUILD/mysql-5.7./boost_1_59_0/ %setup -q -T -a -a -c -n %{src_dir} groupadd %{MYSQL_GROUP} >/dev/null >& || :
useradd -g %{MYSQL_GROUP} %{MYSQL_USER} -s /bin/nologin >/dev/null >& || : # ========================================= #
# 编译
%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= \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE= \
-DWITH_INNOBASE_STORAGE_ENGINE= \
-DWITH_ARCHIVE_STORAGE_ENGINE= \
-DWITH_BLACKHOLE_STORAGE_ENGINE= \
-DENABLE_DOWNLOADS= \
-DDOWNLOAD_BOOST= \
-DWITH_BOOST=. make -j3 %{?_smp_mflags} # ========================================= #
# 安装
%install
rm -rf %{buildroot}
make DESTDIR=%{buildroot} install # ========================================= #
# 制作RPM包
%files
%defattr(-, %{MYSQL_USER}, %{MYSQL_GROUP})
%attr(, %{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=
error_check_enviorment=
error_install_mysql_dependence=
error_install_mysql_package=
error_mysql_install_db=
error_start_mysql_service=
error_grant_mysql_user= ##========================================================## function echo_info()
{
message=$
echo -e "\033[;37;32m ${message} \033[0m"
} function echo_error()
{
message=$
echo -e "\033[;37;31m ${message} \033[0m"
} function check_env()
{
mysql_count=`rpm -qa|grep 'mysql-' |grep -v grep |wc -l`
if [ $mysql_count -eq ];then
echo_info 'check rpm install pass'
else
echo_error "mysql has been installed!!!"
exit ${error_check_enviorment}
fi if [ ! -d "${mysql_data_path}" ]
then
echo_info "${mysql_data_path} not exist"
else
echo_error "${mysql_data_path} exist!!!"
exit ${error_check_enviorment}
fi if [ ! -d "${mysql_server_path}" ]
then
echo_info "${mysql_server_path} not exist"
else
echo_error "${mysql_server_path} exist!!!"
exit ${error_check_enviorment}
fi
} function set_scheduler()
{
echo_info "Please change io scheduler by yourself."
} function set_memory_swap()
{
echo_info "set memory swap"
WCONF=`cat /etc/rc.local |grep swappiness |grep -v grep |wc -l`
if [ $WCONF -eq ];then
echo "echo '1' > /proc/sys/vm/swappiness" >> /etc/rc.local
echo '' > /proc/sys/vm/swappiness
fi
} function install_mysql_dependence()
{
echo_info "install mysql dependence"
rpm -e mysql-devel > /dev/null >&
rpm -e mysql > /dev/null >&
yum install -y bc ncurses ncurses-devel glibc gcc gcc-c++ libstdc++* libtool sysstat lrzsz cmake zlib > /dev/null >&
yum -y install perl perl-JSON perl-Time-HiRes > /dev/null >&
if [ $? -eq ]; then
echo_info "insatll mysql dependence success"
else
echo_error "insatll mysql dependence faild"
exit ${error_install_mysql_package}
fi } function create_mysql_os_user()
{
echo_info "create user mysql"
groupadd mysql > /dev/null || true
useradd mysql -g mysql > /dev/null || true
echo_info "config file limit for mysql"
echo "mysql soft nofile 65536" >> /etc/security/limits.conf
echo "mysql hard nofile 65536" >> /etc/security/limits.conf
echo "mysql soft noproc 16384" >> /etc/security/limits.conf
echo "mysql hard noproc 16384" >> /etc/security/limits.conf } function install_mysql_package()
{
# yum install -y "mysql-5.5
cd "${install_script_dir}"
rpm -ivh "${install_script_dir}/${mysql_rpm_package_name}" >>${install_log} >&
if [ $? -eq ]; then
echo_info "insatll mysql rpm package success"
else
echo_error "insatll mysql rpm package faild"
exit ${error_install_mysql_package}
fi } function set_bash_profile()
{
echo "export LANG=zh_CN.UTF-8" >> /home/mysql/.bash_profile
echo 'PATH=$PATH:'${mysql_server_path}'/bin!' >> /home/mysql/.bash_profile
echo 'export PATH' >> /home/mysql/.bash_profile
} function create_my_cnf()
{
echo_info "create mysql config"
mkdir -p "${mysql_server_path}/etc/" >/dev/null server_id=$(ip a |grep 'inet'|grep 'brd'|grep -v '127.0.0.1' |head -n | awk '{print $2}' |awk -F'/' '{print $1}' |awk -F. '{print $2$3$4}')
dockerflag=`ps -ef|grep "sleep 99999"|grep -v grep|wc -l`
if [ $dockerflag -gt ];then
dock_info=/etc/config_info
if [ -f $dock_info ];then
mem=`cat /etc/config_info|awk -F 'Memory":' '{print $2}'|awk -F '}' '{print $1}'|awk -F ',' '{print $1}'`
mem_gb=`echo "$mem/1024/1024/1024"|bc`
else
echo "Error: Docker env,can't find $dock_info.."
exit
fi
else
mem=`cat /proc/meminfo |grep MemTotal|awk '{print $2}'`
mem_gb=`echo "$mem/1024/1024"|bc`
fi if [ $mem_gb -gt ];then
let pool_size=$mem_gb-
elif [ $mem_gb -gt -a $mem_gb -le ];then
let pool_size=$mem_gb-
elif [ $mem_gb -gt -a $mem_gb -le ];then
let pool_size=$mem_gb-
elif [ $mem_gb -gt -a $mem_gb -le ];then
let pool_size=mem_gb-
elif [ $mem_gb -gt -a $mem_gb -le ];then
let pool_size=$mem_gb-
elif [ $mem_gb -gt -a $mem_gb -le ];then
let pool_size=$mem_gb-
elif [ $mem_gb -gt -a $mem_gb -le ];then
let pool_size=$mem_gb-
else
pool_size=
fi
innodb_buffer_pool_size=${pool_size}
echo_info "innodb_buffer_pool_size: ${innodb_buffer_pool_size}" cat > ${mysql_cnf_path} << EOF
[clent]
port =
socket = ${mysql_data_path}/tmp/mysql.sock [mysqld]
port =
socket = ${mysql_data_path}/tmp/mysql.sock
datadir = ${mysql_data_path}/data/ #--- GLOBAL ---#
log_timestamps = SYSTEM
character-set-server = utf8
lower_case_table_names =
log-output = FILE
log-error = ${mysql_data_path}/log/error.log
#general_log
#general_log_file = ${mysql_data_path}/log/mysql.log
pid-file = ${mysql_data_path}/mysql.pid
slow-query-log
slow_query_log_file = ${mysql_data_path}/log/slow.log
tmpdir = ${mysql_data_path}/tmp
long_query_time = 0.1
sync_binlog =
log_timestamps = SYSTEM
transaction_isolation = READ-COMMITTED
default_storage_engine = InnoDB
#--------------# #thread_concurrency =
thread_cache_size =
table_open_cache =
table_definition_cache =
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 4M
read_rnd_buffer_size = 4M
key_buffer_size = 64M
myisam_sort_buffer_size = 64M
tmp_table_size = 32M
max_heap_table_size = 32M
open_files_limit =
query_cache_size =
query_cache_type =
bulk_insert_buffer_size = 64M
binlog_rows_query_log_events =on
sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
optimizer_switch = 'index_merge=off,index_merge_union=off,index_merge_sort_union=off,index_merge_intersection=off' #--- NETWORK ---#
back_log =
max_allowed_packet = 256M
interactive_timeout =
wait_timeout =
skip-external-locking
max_connections =
max_connect_errors =
skip-name-resolve =
read_only = #--- REPL ---#
server-id = ${server_id}
log-bin = mysql-bin
master_info_repository = TABLE
binlog_format = ROW
binlog_cache_size = 4M
expire_logs_days = replicate-ignore-db = test
log_slave_updates = slave-parallel-workers =
slave-parallel-type = LOGICAL_CLOCK
slave_preserve_commit_order = skip-slave-start
gtid_mode = on
enforce-gtid-consistency = true relay-log = relay-log
relay_log_recovery = ON
sync_relay_log =
relay_log_info_repository = TABLE #--- INNODB ---#
default-storage-engine = INNODB
innodb_data_home_dir = ${mysql_data_path}/data
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_file_per_table
innodb_log_group_home_dir = ${mysql_data_path}/data
innodb_buffer_pool_size = ${innodb_buffer_pool_size}G
#innodb_additional_mem_pool_size = 128M
innodb_log_files_in_group =
innodb_log_file_size = 1024M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit =
innodb_lock_wait_timeout =
innodb_flush_method = O_DIRECT
innodb_max_dirty_pages_pct =
innodb_io_capacity =
innodb_open_files =
innodb_write_io_threads =
innodb_read_io_threads =
innodb_print_all_deadlocks =
innodb_undo_directory = ${mysql_data_path}/undo
innodb_purge_threads =
innodb_purge_batch_size =
innodb_stats_on_metadata =
innodb_page_cleaners =
innodb_purge_threads =
innodb_buffer_pool_instances = [mysqldump]
#quick
max_allowed_packet = 256M [mysql]
# auto-rehash
# Remove the next comment character if you are not familiar with SQL
# safe-updates
default-character-set=utf8 [myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M [mysqlhotcopy]
interactive-timeout EOF
echo_info "mysql config file is created" } function mysql_install_db()
{
echo_info "run mysql_install_db"
/bin/rm -rf ${mysql_data_path}
mkdir -p ${mysql_data_path}/data/ ${mysql_data_path}/tmp/ ${mysql_data_path}/log/
chown -R mysql:mysql ${mysql_server_path} ${mysql_data_path}
${mysql_server_path}/bin/mysqld --initialize --user=mysql --basedir=${mysql_server_path} --datadir=${mysql_data_path}/data/ --initialize-insecure >>${install_log} >&
if [ $? -eq ]; then
echo_info "run mysql_install_db success"
else
echo_error "run mysql_install_db faild"
exit ${error_mysql_install_db}
fi } function start_mysql_service()
{
echo_info "start mysql service"
su - mysql -c "${mysql_server_path}/bin/mysqld_safe --defaults-file=${mysql_server_path}/etc/my.cnf 1>/dev/null 2>&1 &"
for i in {..};do [ -S ${mysql_data_path}/tmp/mysql.sock ] && echo_info "mysql service is OK" && break || echo -n ". ";sleep ;done
if [ ! -S ${mysql_data_path}/tmp/mysql.sock ];then
echo_error "Start Faild,See ${mysql_data_path}/log/error.log";
exit ${error_start_mysql_service};
fi } function grant_mysql_user()
{
echo_info "grant mysql user"
"${mysql_server_path}/bin/mysql" -uroot -e " ##==============================================================================##
## DBA
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123@.com' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '123@.com' WITH GRANT OPTION; set session binlog_format = statement;
delete from mysql.user where user='';
delete from mysql.user where authentication_string='';
set session binlog_format = row;
" if [ $? -eq ]; then
echo_info "grant mysql user success"
else
echo_error "grant mysql user faild"
exit ${error_grant_mysql_user}
fi } function set_network_config()
{
echo_info "config network"
echo "
net.ipv4.tcp_tw_reuse =
net.ipv4.tcp_tw_recycle =
net.ipv4.tcp_fin_timeout =
" >>/etc/sysctl.conf
sysctl -p >/dev/null >&
} check_env
set_scheduler
set_memory_swap
set_network_config
install_mysql_dependence
create_mysql_os_user
install_mysql_package
set_bash_profile
create_my_cnf
mysql_install_db
start_mysql_service
grant_mysql_user

制作MySQL RPM安装包Spec的更多相关文章

  1. RPM安装包-Spec文件參数具体解释与演示样例分析

    spec文件是整个RPM包建立过程的中心,它的作用就如同编译程序时的Makefile文件. 1.Spec文件參数 spec文件包括建立一个RPM包必需的信息,包括哪些文件是包的一部分以及它们安装在哪个 ...

  2. rpm 安装包制作

    rpm 安装包制作 思路 参照系统自带 etcd 解压->替换掉执行文件->打包 1 预备安装工具 下载工具 yumloader #yum install -y yum-utils 解压工 ...

  3. linux rpm问题:怎样查看rpm安装包的安装路径

      x rpm问题:怎样查看rpm安装包的安装路径 2010-07-12 21:19:15 标签:rpm linux 路径 休闲 职场 rpm -qpl xxxxxx.rpm 1.如何安装rpm软件包 ...

  4. centos7下使用mysql离线安装包安装mysql5.7

    服务器环境: centos7 x64 需要安装mysql5.7+ 一.卸载CentOS7系统自带mariadb # 查看系统自带的Mariadb [root@CDH-141 ~]# rpm -qa|g ...

  5. 制作linux内核安装包

    实验基于Centos 6.2 升级linux内核 直接在一个有编译环境的设备上,编译升级内核很简单. make menuconfig 或者 拷贝现有系统的.config文件 修改.config文件  ...

  6. NSIS:制作软件升级安装包

    原文 NSIS:制作软件升级安装包 相信不是每个人写的软件都只发布一次就可以了,肯定要有修改和维护的情况发生.在这种情况下,您可能就需要一个软件的升级安装包了.   现在,我们就来一步步把这个安装包做 ...

  7. Advanced Installer 制作.NetWeb部署安装包

    原文:Advanced Installer 制作.NetWeb部署安装包 因为是.Net的Web应用程序,所以想用Advanced Installer 调用Dll实现安装部署. 因为我需要自己定制参数 ...

  8. IS2009制作Oracle 静默安装包(二) 感谢空白先生特许授权

    原文:IS2009制作Oracle 静默安装包(二) 感谢空白先生特许授权 上一篇: IS2009制作Oracle 静默安装包(一)感谢空白先生特许授权本文经原作者特许授权于海洋女神发布,转载请务必注 ...

  9. IS2009制作Oracle 静默安装包(一)感谢空白先生特许授权

    原文:IS2009制作Oracle 静默安装包(一)感谢空白先生特许授权 上一篇: MyEclipse中消除frame引起的“the file XXX can not be found.Please ...

随机推荐

  1. Android教程2020 - RecyclerView获取滑动距离

    获取RecyclerView滑动的距离. Android教程2020 - 系列总览 本文链接 前面我们已经用RecyclerView显示一些数据. 本文演示如何获取RecyclerView的滑动距离. ...

  2. vue报错Error in v-on handler: "RangeError: Maximum call stack size exceeded"

    看下面的报错 错误 看到这个错误一脸懵逼.后面了解到,是因为程序进入了死循环,后面检查了我的代码,原来在这里自己调用自己

  3. MP3播放-基于MCI-API接口

    今天整理到音频播放的部分,本来就想抽取一个简单的接口方便以后可能会用到,然而不知不觉就把常用的功能都给一起封装好了,核心其实就是调用MCI的API接口,具体的功能就是变换不同的MCI指令来实现. == ...

  4. React+Echarts简单的封装套路

    今天我们来介绍一下React中,对Echarts的一个简单的封装. 首先在我们的React项目中,想使用Echart包,首先需要先安装它,安装代码如下,任选一个就可以 cnpm install ech ...

  5. 版本控制工具-svn

    两个疑问: 1.什么是版本控制? 2.为什么要用版本控制工具? 银联卡的特征: 1.受保护的 2.受约束的 如何与银联卡对应? 1.个人的代码--口袋里的钱 2.版本控制工具中的代码--银联卡里的钱 ...

  6. vue路由核心要点(vue-router)

    目录 目录 1.vue-router 是什么? 2.如何使用v-router? 3.vue-router跳转和传参 4.vue-router实现的原理 两种模式 5.vue-router 有哪几种导航 ...

  7. ISC BIND DNS

    win10,安装BIND9.15.5.x64 安装完成后,计算机服务里启动,总是报无法登陆,但服务属性登陆里设置了密码了啊,就是named,但就是一直报错.后来用下面方法避开了该问题. 安装完成后,服 ...

  8. onboard procossor and cross-compile

    星载处理器 ERC32-TSC695F The European Space Agency’s ERC32 is a microprocessor implementing a SPARC V7 pr ...

  9. 浅谈.NET中AppDomain的理解

    一.走进.NET AppDomain 天哪,.NET Framwork的CLR真是巧妙呢!随着越来越多的对.Net底层编程的了解,一些诸如架构,处理过程的复杂难懂的细节完全的让我叹服,所以呢,再次错过 ...

  10. mongoose报错:DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

    mongoose报错:(node:15689) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes ...