编译安装一个mysql需要的时间太长了,在批量部署服务器时都是利用rpm包快速安装,网上现成的rpm是很方便,但是我更喜欢自己打包的。这里仅记录一下在Centos6系统中利用rpmbuild工具制作一个mysql-5.7.17版本的rpm包。
1、安装工具包:
yum install rpmbuild rpmdevtools -y
2、初始化一个目录结构:
rpmdev-setuptree
3、查看一下执行上面命令后的目录结构:
[root@rpm-build ~]# tree -L 3
└── rpmbuild
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
4、把原码包mysql-5.7.17.tar.gz上传到SOURCES目录下,并在SPECS目录下添加配置好的mysql.spec文件:
由于在Mysql5.6后都要依赖boost_1_59_0.tar.gz包,所以这里我直接解压到BUILD目录下,不然检测不到;
tar xf boost_1_59_0.tar.gz -C rpmbuild/BUILD/
5、配置mysql.spec文件,还需要添加一个my.cnf配置文件到SOURCES目录下;
Name: mysql
Version: 5.7.17
Release: 1%{?dist}
License: GPL
URL: http://downloads.mysql.com/archives/get/file/mysql-5.7.17.tar.gz
Group: applications/database
Source: mysql-5.7.17.tar.gz
BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
BuildRequires: cmake
Packager: hz328@qq.com
Autoreq: no
#Source: %{name}-%{version}.tar.gz
prefix: /usr/local/mysql-%{version}
Summary: MySQL 5.7.17
%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
#CFLAGS="-O3 -g -fno-exceptions -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
#CXX=g++
#CXXFLAGS="-O3 -g -fno-exceptions -fno-rtti -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
#export CFLAGS CXX CXXFLAGS
cmake \
-DCMAKE_INSTALL_PREFIX=%{prefix} \
-DMYSQL_DATADIR=/data \
-DWITH_BOOST=../boost_1_59_0 \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
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
mkdir -p /data
useradd -s /bin/nologin -M mysql >/dev/null 2>&1
%post
/bin/cp %{prefix}/support-files/mysql.server /etc/init.d/mysql
/bin/cp %{prefix}/my.cnf %{_sysconfdir}/my.cnf
chkconfig mysql on
%{prefix}/bin/mysqld --initialize-insecure --basedir=%{prefix} --datadir=/data/mysql --user=mysql
service mysql start
chown -R mysql:mysql /data/mysql
echo "export PATH=.:\$PATH:/usr/local/mysql-%{version}/bin;" >> ~/.bash_profile
source ~/.bash_profile
ln -s %{prefix}/lib %{prefix}/lib64
%preun
service mysql stop
chkconfig --del mysql
userdel -r mysql >/dev/null 2>&1
rm -rf /usr/local/mysql-%{version} >/dev/null 2>&1
rm -rf /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
6、执行命令打包,需要一点时间看机器的配置;
rpmbuild -bb rpmbuild/SPECS/mysql-5.7.17-new.spec
7、完成后在rpmbuild/RPM目录下就会两个rpm包;
[root@rhce ~]# ll rpmbuild/RPMS/x86_64/
total 267280
-rw-r--r-- 1 root root 80603892 May 28 19:47 mysql-5.7.17-1.el6.x86_64.rpm
-rw-r--r-- 1 root root 193088996 May 28 19:48 mysql-debuginfo-5.7.17-1.el6.x86_64.rpm
8、直接安装,安装完成后会直接启动Mysql:
[root@rhce ~]# rpm -ivh rpmbuild/RPMS/x86_64/mysql-5.7.17-1.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:mysql ########################################### [100%]
Starting MySQL...[ OK ]
不关闭服务直接卸载:
[root@rhce ~]# rpm -e --nodeps mysql-5.7.17-1.el6.x86_64
Shutting down MySQL..[ OK ]
注意这里配置了,在卸载服务时会把mysql目录删除,所以要先备份数据;