mugshot, larkspur

g13n


Gopal Venkatesan's Former Journal

My writings on free/open source software and other technologies


Previous Entry Share Next Entry
Downloading, compiling, and installing MySQL Server from source code
mugshot, larkspur
g13n

This content has been updated and moved to a new place.


If you are running any GNU/Linux server operating system like RHEL 5 or CentOS 5, you may probably install MySQL server that comes with the operating system packages either during the initial setup or later using yum(8). The advantage being addition/removal of packages either using the GUI package manager or rpm(8), yum(8). Fair enough. But unfortunately the MySQL package (mysql-server) that comes bundled with RHEL 5.5 or CentOS 5.5 is fairly old (5.0.77). What if you want to install the latest stable version of MySQL yet have the advantage of removing/re-installing the software using rpm(8)?


In this blog post, I will guide you with compiling MySQL from source code yet installing the software through rpm(8) so that we tune and configure the software for the target machine and yet uninstall the software using RedHat package manager.


Compiling and Installing MySQL using rpmbuild(8)


First make sure you have sudo(8) access and the machine has rpmbuild(8) installed.


shell> rpm -q rpm-build

If rpm(8) reports the package is not installed, install it using yum(8).


shell> sudo yum install rpm-build

Assuming the installation is successful, let us prepare the machine by installing the dependencies for MySQL.


shell> sudo yum install gperf

You have to download the source RPM (SRPM) for MySQL since we would be building and installing from source rather than install a pre-built binary package.


shell> wget http://dev.mysql.com/get/Downloads/MySQL-5.1/MySQL-community-5.1.53-1.rhel5.src.rpm/from/http://mysql.mirrors.pair.com/

Before we begin, you need to setup the RPM build environment. This is covered in the CentOS wiki and also at several other places around the web. Basically it boils down to installing the rpmbuild(8) package and following the steps below.


shell> mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
shell> echo '%_topdir %(echo $HOME)/rpmbuild' >~/.rpmmacros
# In case gcc(1) and make(1) are not installed ...
shell> sudo yum install gcc make

Now we are set to start the build process. If you have ever compiled from source code you would know that you can configure the software by passing command line arguments to the configure script. Unfortunately for building using rpmbuild(8) the parameters have to be edited by hand in the corresponding SPEC file.


# Note, no sudo(8)
shell> rpm -ivh MySQL-community-5.1.53-1.rhel5.src.rpm

The above command extracts the source tarball into $HOME/rpmbuild/SOURCES and the SPEC file into $HOME/rpmbuild/SPECS. Normally there is no need to edit the SPEC file, but MySQL by default uses latin-1 server character set and latin1_swedish_ci as the corresponding default collation. This is preferred if the data stored is not just latin-1 characters, and it is always better over the default. So, edit the $HOME/rpmbuild/SPECS/mysql-5.1.53.rhel5.spec file and add the following lines to the configure line.


            --with-charset=utf8 \
            --with-collation=utf8_unicode_ci \
            --enable-profiling \

Here is the patch if you want to directly apply it using the patch(1) command.


--- mysql-5.1.53.rhel5.spec 2010-12-08 09:01:38.000000000 +0530
+++ mysql-5.1.53.rhel5.spec.new 2010-12-08 09:01:27.000000000 +0530
@@ -552,6 +552,9 @@
            --with-unix-socket-path=/var/lib/mysql/mysql.sock \
            --with-pic \
            --prefix=/ \
+ --with-charset=utf8 \
+ --with-collation=utf8_unicode_ci \
+ --enable-profiling \
 %if %{CLUSTER_BUILD}
            --with-extra-charsets=all \
 %else

Now everything is done to trigger the build. The build will take quite an amount of time given the number of tests that would be run after the server code is built. Trigger the build using the rpmbuild(8) command. The define parameter is necessary without which the build will not succeed.


shell> rpmbuild --define 'community 1' -bb ~/rpmbuild/SPECS/mysql-5.1.53.rhel5.spec

Once the build is successful, the binaries are placed under the RPMS directory under the appropriate architecture directory. My machine is a 64-bit machine and hence my build target is x86_64. You can use rpm(8) to install them.


shell> cd ~/rpmbuild/RPMS/x86_64
shell> sudo rpm -ivh MySQL-*

MySQL server will automatically be started after the install is successful. However note that my.cnf is not installed by default. Based on your installation and purpose you can copy one of the small, medium, large configurations as the default configuration. On my machine I chose the medium as the default.


shell> sudo cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

This completes the MySQL server installation with the default datadir and other options. If you want to move your datadir to a different directory other than /var/lib/mysql because of space constraints I will reserve that for my next post :-)


shell> mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.53-community-log MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT VERSION();
+----------------------+
| VERSION()            |
+----------------------+
| 5.1.53-community-log |
+----------------------+

1 row in set (0.01 sec)

mysql>