自制mysql的rpm包
MySQL安装一般使用RPM或者源码安装的方式。
RPM安装的优点是快速,方便.缺点是不能自定义安装目录.如果需要调整数据文件和日志文件的存放位置,还需要进行一些手动调整。
源码安装的优点是可以自定义安装目录,缺点是编译时间长,过程复杂
其实还有一种方式,定制RPM包.
它相当于用源码安装的方式定制了一个RPM包.一次打包,可以多次使用
它可以定制路径,安装时自动创建帐号,自动配置服务,环境变量等,并且安装过程快速,简单。
在大规模部署的场景下,优点十分突出.
缺点是制作RPM包,需要自己编写Red Hat定义的spec文件,而编写完成后,也需要进行编译
现在开始定制mysql的rpm包
1、定制rpm包,首先需要安装一个包
yum -y install rpm-build
2、定制rpm包只需要用到普通用户便可以完成,所以这里推荐使用普通用户
useradd wadeson
3、创建自制rpm包需要用到的基本仓库目录
在用户的家目录下面执行[wadeson@testdb ~]$ rpmbuild ~,即可,会自动生成需要用到的一些目录
相应的目录创建好了之后,开始进行真正的操作,首先下载mysql的源码包,这里以mysql-5.6.37为例
将下载好的mysql-5.6.37存放在相应的目录:
[wadeson@testdb ~]$ cd rpmbuild/SOURCES/
[wadeson@testdb SOURCES]$ ll
total 31448
-rw-rw-r--. 1 wadeson wadeson 32200158 Aug 23 22:19 mysql-5.6.37.tar.gz
mkdir -p /data/mysql
if ! id %{MYSQL_USER} > /dev/null 2>&1;then
useradd -M -s /sbin/nologin %{MYSQL_USER}
fi
chown -R mysql:mysql /data/mysql
/usr/local/mysql-5.6.37/scripts/mysql_install_db --basedir=/usr/local/mysql-5.6.37 --datadir=/data/mysql --user=mysql
cp /usr/local/mysql-5.6.37/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig mysqld on
cp /usr/local/mysql-5.6.37/my.cnf /etc/my.cnf
Name: mysql Version: 5.6.37 Release: 1%{?dist} Summary: MySQL-5.6.37 RPM Group: applications/database License: GPL URL: http://www.mysql.com Source0: mysql-5.6.37.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root BuildRequires: cmake,ncurses-devel #Requires: AutoReqProv: no %description this is rpmbuilding mysql5.6.37 rpm %define MYSQL_USER mysql %define MYSQL_GROUP mysql %prep %setup -q -n mysql-%{version} %build cmake . \ -DCMAKE_INSTALL_PREFIX:=/usr/local/mysql-5.6.37 \ -DMYSQL_DATADIR=/data/mysql \ -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} %clean rm -rf %{buildroot} %files %defattr(-, %{MYSQL_USER}, %{MYSQL_GROUP}) %attr(755, %{MYSQL_USER}, %{MYSQL_GROUP}) /usr/local/mysql-5.6.37/* %pre mkdir -p /data/mysql if ! id %{MYSQL_USER} > /dev/null 2>&1;then useradd -M -s /sbin/nologin %{MYSQL_USER} fi chown -R mysql:mysql /data/mysql %post /usr/local/mysql-5.6.37/scripts/mysql_install_db --basedir=/usr/local/mysql-5.6.37 --datadir=/data/mysql --user=mysql cp /usr/local/mysql-5.6.37/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld chkconfig mysqld on cp /usr/local/mysql-5.6.37/my.cnf /etc/my.cnf %preun service mysqld stop chkconfig --del mysqld userdel mysql rm -rf /home/mysql rm -rf /data/mysql rm -rf /etc/init.d/mysqld rm -rf /usr/local/mysql-5.6.37 %changelog
完成了spec文件后,于是使用如下命令进行编译:
[wadeson@testdb SPECS]$ rpmbuild --help
Usage: rpmbuild [OPTION...]
--quiet
Build options with [ <specfile> | <tarball> | <source package> ]:
-bp build through %prep (unpack sources and apply patches) from <specfile>
-bc build through %build (%prep, then compile) from <specfile>
-bi build through %install (%prep, %build, then install) from <specfile>
-bl verify %files section from <specfile>
-ba build source and binary packages from <specfile>
-bb build binary package only from <specfile>
-bs build source package only from <specfile>
-tp build through %prep (unpack sources and apply patches) from <tarball>
-tc build through %build (%prep, then compile) from <tarball>
-ti build through %install (%prep, %build, then install) from <tarball>
-ta build source and binary packages from <tarball>
$rpmbuild -bb mysql.spec
这里会输出相应的编译过程,如遇到错误,可根据提示做相应修改
最终没有错误的话,会在如下目录生成rpm包: