本文重点是spec文件,其他步骤比较简单,笔者就不写了

  • 1.yum安装rpmbuild工具 yum -y install rpm-build
  • 2.定义工作目录 vi ~/.rpmmacros %_topdir /root/rpmbuild #也可以使用其他用户制作
  • 3.创建topdir目录

mkdir -pv ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

  • 4 编写spec文件,在SPECS目录

%define MYSQL_USER mysql %define MYSQL_GROUP mysql Name: mysql Version: 5.7.18 Release: msxf_v2 Summary: MySQL-5.7.18 RPM Group: applications/database License: GPL
URL: http://www.mysql.com Source0: mysql-5.7.18.tar.gz Packager: xxxx@gmail.com BuildRoot: %{_topdir}/%{name}-%{version}-msxf BuildRequires: cmake AutoReqProv: no

%description MySQL 5.7.18

%prep %setup -n mysql-%{version}

%build cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql57
-DMYSQL_DATADIR=/home/mysql/mysql3306/data
-DSYSCONFDIR=/home/mysql/mysql3306
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_EXAMPLE_STORAGE_ENGINE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
-DMYSQL_UNIX_ADDR=/home/mysql/mysql3306/mysql3306.sock
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DENABLED_LOCAL_INFILE=ON
-DDOWNLOAD_BOOST=1
-DWITH_BOOST=../boost_1_59_0/

make %{?_smp_mflags}

%install rm -rf %{buildroot} make install DESTDIR=$RPM_BUILD_ROOT

%pre IS_CONFIG=$( cat /etc/security/limits.conf | grep mysql | wc -l ) if [ $IS_CONFIG -lt 1 ];then cat >> /etc/security/limits.conf << EOF mysql soft nproc 2047 mysql hard nproc 16384 mysql soft nofile 1024 mysql hard nofile 65536 EOF fi echo 0 > /proc/sys/vm/swappiness mkdir -p /home/mysql/mysql3306/{data,log,binlog} TOTAL_SIZE=$( free -g | grep Mem | awk '{print $2}' ) ALLOC_SIZE=$(( $TOTAL_SIZE * 64 / 100 )) BUFFER_SIZE=$( echo $ALLOC_SIZE | awk -F. '{print $1}')"G" SERVER_ID=$( ip addr | grep inet | grep "255 scope global" |head -1|awk '{print $2}'|cut -d "." -f 4|cut -d "/" -f 1 )"3306" cat > /home/mysql/mysql3306/my.cnf <<EOF [mysql] default-character-set=utf8mb4 [mysqld] socket=/home/mysql/mysql3306/mysql3306.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0

port = 3306 #extra_port=13306 basedir = /usr/local/mysql57 datadir = /home/mysql/mysql3306/data pid-file = /home/mysql/mysql3306/mysql.pid user = mysql server-id =$SERVER_ID #rpl_semi_sync_master_enabled=1 #rpl_semi_sync_master_timeout=1000 #rpl_semi_sync_slave_enabled=1 relay_log_purge=0 read_only=0 super_read_only=0 slave-skip-errors=1396 binlog_row_image=full # parallel replication binlog_group_commit_sync_delay=10 binlog_group_commit_sync_no_delay_count=1000 master_info_repository='TABLE'

slave_parallel_type = LOGICAL_CLOCK slave_parallel_workers = 10 relay_log_info_repository='TABLE'

log_slave_updates=1 lower_case_table_names = 1 character-set-server=utf8mb4 skip-name-resolve skip-external-locking back_log = 500 max_connections = 2000 max_connect_errors = 2000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 64M

key_buffer_size = 128M read_buffer_size = 64M read_rnd_buffer_size = 16M sort_buffer_size = 32M join_buffer_size = 32M tmp_table_size = 96M max_heap_table_size = 96M query_cache_size = 16M query_cache_limit = 16M thread_cache_size = 64

log_bin = /home/mysql/mysql3306/binlog/mysql-bin binlog_format = row binlog_cache_size = 16M sync_binlog = 1 max_binlog_cache_size = 1000M max_binlog_size = 1G expire_logs_days = 10

log_error = /home/mysql/mysql3306/log/mysql-error.log slow_query_log = 1 long_query_time = 0.1 slow_query_log_file = /home/mysql/mysql3306/log/mysql-slow.log relay_log=/home/mysql/mysql3306/binlog/slave-relay-bin relay_log_index=slave-relay-bin.index

default_storage_engine = InnoDB innodb_buffer_pool_size = $BUFFER_SIZE innodb_file_per_table = 1 innodb_data_home_dir = /home/mysql/mysql3306/data innodb_data_file_path = ibdata1:500M;ibdata2:1G:autoextend innodb_log_group_home_dir = /home/mysql/mysql3306 innodb_log_file_size = 500M innodb_log_buffer_size = 96M innodb_flush_log_at_trx_commit = 1 innodb_print_all_deadlocks = 1

skip-slave-start=1 gtid-mode=on enforce-gtid-consistency=true slave-parallel-workers=8 sql_mode='NO_ENGINE_SUBSTITUTION' log_timestamps=SYSTEM EOF useradd mysql chown -R mysql:mysql /home/mysql/mysql3306

%post /usr/local/mysql57/bin/mysqld --defaults-file=/home/mysql/mysql3306/my.cnf --initialize-insecure --user=mysql HAVE_PROFILE_PATH=$( cat /etc/profile | grep /usr/local/mysql57 | wc -l ) if [ $HAVE_PROFILE_PATH -lt 1 ];then echo "export PATH=/usr/local/mysql57/bin:$PATH" >> /etc/profile fi HAVE_SELF_PATH=$( cat ~/.bash_profile | grep /usr/local/mysql57 | wc -l ) if [ $HAVE_SELF_PATH -lt 1 ];then echo "export PATH=/usr/local/mysql57/bin:$PATH" >> ~/.bash_profile fi /usr/local/mysql57/bin/mysqld_safe --defaults-file=/home/mysql/mysql3306/my.cnf & IP_ADDR=$( ip addr | grep inet | grep "255 scope global" |head -1|awk '{print $2}' | cut -d "/" -f 1 ) sleep 10 /usr/local/mysql57/bin/mysql -uroot -S /home/mysql/mysql3306/mysql3306.sock <<EOF create database if not exists temp_dba_db; alter user 'root'@'localhost' identified by 'mysql@123'; flush privileges; reset master; EOF

source ~/.bash_profile %preun

%postun rm -rf /usr/local/mysql57

%clean rm -rf %{buildroot}

%files %defattr(-, %{MYSQL_USER}, %{MYSQL_GROUP}) %attr(755, %{MYSQL_USER}, %{MYSQL_GROUP}) /usr/local/mysql57/*

%changelog

  • 编译命令说明: rpmbuild
    -ba 既生成src.rpm又生成二进制rpm -bs 只生成src的rpm -bb 只生二进制的rpm -bp 执行到pre -bc 执行到 build段 -bi 执行install段 -bl 检测有文件没包含