- Pre-installation Steps
- Create ‘mysql’ Group & User
- Create Directories
- Install/Update OS Packages
- Copy Source Distribution to Build Directory
- Install MySQL
- Set Environment Variables
- Create Directories
- Build MySQL
- Update Profile
- Create System Database
- Update ‘defaults’ File
- Start MySQL
- Secure MySQL
Introduction
Getting Started
This step-by-step guide shows how to build MySQL from source – version 5.5.21 – on Linux. I used a host named ‘lamp1′ running CentOS 5.7 (x64). This guide exhibits the granular control that may be achieved by building MySQL yourself. In my case, great care was taken to produce a layout that could support many MySQL daemons running different versions and so forth. Laying MySQL out this way provides a lot more flexibility especially come upgrade time.
Required Software
The following software is used in this guide:
- MySQL Community Server (Select “Platform,” then “Source Code”)
Note: 5.5.21 was the most current version at the time of this writing
Automated Scripts
This guide shows you how to build MySQL from source in a keystroke by keystroke fashion. You may also use the automated scripts that I have created to go through this process as well. I suggest downloading the scripts and modifying as appropriate for your target environment.
Pre-installation Steps
Create ‘mysql’ Group & User
Create ‘mysql’ Group & User
First, we need to create the Operating System group and user that will own the MySQL software as well as well as run the MySQL processes:
[root@lamp1]$ groupadd -g 505 mysql
[root@lamp1]$ useradd -u 505 -g mysql mysql
Set the password for the newly created MySQL user:
[root@lamp1]$ passwd mysql
Create Directories
Recursively create the initial directories:
[root@lamp1]$ mkdir -p /u01/app/LAMP/mysql/build
chown -R mysql:mysql /u01/app/LAMP/mysql
Install/Update OS Packages
To build MySQL there are at least 3 Operating System package dependencies; install/update as appropriate:
[root@lamp1]$ yum install -y ncurses-devel libaio-devel cmake
Copy Source Distribution to Build Directory
Reference the Required Software section. Download the source distribution of MySQL, make sure the newly created ‘mysql’ user has access to it and then copy it to the ‘build’ directory:
[root@lamp1]$ chown mysql:mysql mysql-5.5.21.tar.gz
[root@lamp1]$ cp mysql-5.5.21.tar.gz /u01/app/LAMP/mysql/build/.
Install MySQL
Set Environment Variables
Change ownership to the newly created ‘mysql’ user:
[root@lamp1]$ su - mysql
In order to streamline the installation, we will set a series of environment variables that may be used throughout the installation process:
[mysql@lamp1]$ MYSQL_PORT=3309
[mysql@lamp1]$ MYSQL_VERSION=5.5.21
[mysql@lamp1]$ MYSQL_BASE=/u01/app/LAMP/mysql
[mysql@lamp1]$ MYSQL_HOME=$MYSQL_BASE/$MYSQL_VERSION
[mysql@lamp1]$ MYSQL_BUILD_DIR=$MYSQL_BASE/build/mysql-$MYSQL_VERSION
[mysql@lamp1]$ MYSQL_DEFAULT_CHARSET=utf8
[mysql@lamp1]$ MYSQL_DEFAULT_COLLATION=utf8_general_ci
[mysql@lamp1]$ MYSQL_DATA_DIR=$MYSQL_BASE/data/$MYSQL_VERSION
[mysql@lamp1]$ MYSQL_TMP_DIR=$MYSQL_BASE/tmp/$MYSQL_VERSION
[mysql@lamp1]$ MYSQL_LOG_DIR=$MYSQL_BASE/logs/$MYSQL_VERSION
[mysql@lamp1]$ MYSQL_PID_DIR=$MYSQL_BASE/pid/$MYSQL_VERSION
[mysql@lamp1]$ MYSQL_LOGBIN_DIR=$MYSQL_BASE/log-bin/$MYSQL_VERSION
[mysql@lamp1]$ MYSQL_SOCKET_DIR=$MYSQL_BASE/socket/$MYSQL_VERSION
[mysql@lamp1]$ MYSQL_SOCKET=$MYSQL_SOCKET_DIR/mysql-$MYSQL_VERSION.sock
Create Directories
One of the primary reasons for building MySQL from source is to have control over the default locations for files and so forth. Create the target directories that we will use for our server:
[mysql@lamp1]$ mkdir -p $MYSQL_HOME
[mysql@lamp1]$ mkdir -p $MYSQL_DATA_DIR
[mysql@lamp1]$ mkdir -p $MYSQL_TMP_DIR
[mysql@lamp1]$ mkdir -p $MYSQL_LOG_DIR
[mysql@lamp1]$ mkdir -p $MYSQL_LOGBIN_DIR
[mysql@lamp1]$ mkdir -p $MYSQL_PID_DIR
[mysql@lamp1]$ mkdir -p $MYSQL_SOCKET_DIR
Additionally, create the socket file used to connect to MySQL locally:
[mysql@lamp1]$ touch $MYSQL_SOCKET
Build MySQL
MySQL is built from source using a cross-platform, open-source tool called ‘cmake.’ When building MySQL using cmake there are a variety of configuration options; reference the MySQL reference manual under “MySQL Source-Configuration Options” for a comprehensive list. In my case, I am basically changing the default port, installation prefix & file locations, and installing storage engines over and above the defaults (MyISAM, Merge, Memory, and CSV storage engines are always included).
Cmake
Make sure ‘cmake’ is in the path:
[mysql@lamp1]$ export PATH=/usr/bin/cmake:$PATH
Change directory to the ‘build’ directory where you placed the source distribution:
[mysql@lamp1]$ cd $MYSQL_BASE/build
Extract the source distribution:
[mysql@lamp1]$ tar -xvf mysql-$MYSQL_VERSION.tar.gz
Change directory to the newly created version-specific ‘build’ directory:
[mysql@lamp1]$ cd $MYSQL_BUILD_DIR
Run ‘cmake’ providing the options specific to your environment:
[mysql@lamp1]$ cmake -DCMAKE_INSTALL_PREFIX=$MYSQL_HOME -DMYSQL_TCP_PORT=$MYSQL_PORT -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=$MYSQL_DEFAULT_CHARSET -DDEFAULT_COLLATION=$MYSQL_DEFAULT_COLLATION -DMYSQL_UNIX_ADDR=$MYSQL_SOCKET
Make
Run ‘make’ (this will take some time!)
[mysql@lamp1]$ make
Run ‘make install’:
Make Install
[mysql@lamp1]$ make install
Update Profile
This guide assumes that there is a ‘.bash_profile’ script being used for the ‘mysql’ user’s environment. Modify the ‘.bash_profile’ script to add an environment variable named ‘MYSQL_HOME,’ adding it to the ‘PATH’ as well:
[mysql@lamp1]$ sed -i -e '/MYSQL_HOME/d' $HOME/.bash_profile
[mysql@lamp1]$ echo "export MYSQL_HOME=$MYSQL_HOME" >> $HOME/.bash_profile
[mysql@lamp1]$ echo 'export PATH=$MYSQL_HOME/bin:$MYSQL_HOME/scripts:$PATH' >> $HOME/.bash_profile
Source the environment:
[mysql@lamp1]$ . ~/.bash_profile
Create System Database
Create the system database and tables:
[mysql@lamp1]$ mysql_install_db --basedir=$MYSQL_HOME --datadir=$MYSQL_DATA_DIR --tmpdir=$MYSQL_TMP_DIR --user=$USER
Update ‘defaults’ File
Update the ‘defaults’ (.cnf) file as appropriate for your environment. In my case, I use ‘sed’ to add and modify numerous lines starting with a stock ‘defaults’ (.cnf) file.
First copy the stock ‘defaults’ file of your choice from the ‘support-files’ directory to your ‘$MYSQL_HOME’:
[mysql@lamp1]$ cp $MYSQL_HOME/support-files/my-small.cnf $MYSQL_HOME/my.cnf
Update the newly copied ‘defaults’ file’s values as appropriate; I am changing the location of files, enabling ‘innodb’ storage engine options, etc.:
[mysql@lamp1]$ sed -i -e 's/^#inno/inno/g' $MYSQL_HOME/my.cnf
[mysql@lamp1]$ sed -i -e "s|^#log-bin=|log-bin=$MYSQL_LOGBIN_DIR/|g" $MYSQL_HOME/my.cnf
[mysql@lamp1]$ sed -i -e 's/#binlog_format/binlog_format/g' $MYSQL_HOME/my.cnf
[mysql@lamp1]$ sed -i -e "s|$MYSQL_HOME/data|$MYSQL_DATA_DIR|g" $MYSQL_HOME/my.cnf
[mysql@lamp1]$ sed -i -e "s|\[mysqld\]|[mysqld]\ndatadir=$MYSQL_DATA_DIR|g" $MYSQL_HOME/my.cnf
[mysql@lamp1]$ sed -i -e "s|\[mysqld\]|[mysqld]\ngeneral-log-file=$MYSQL_LOG_DIR/mysql-$MYSQL_VERSION.log|g" $MYSQL_HOME/my.cnf
[mysql@lamp1]$ sed -i -e "s|\[mysqld\]|[mysqld]\ngeneral-log=ON|g" $MYSQL_HOME/my.cnf
[mysql@lamp1]$ sed -i -e "s|\[mysqld\]|[mysqld]\nlog-error=$MYSQL_LOG_DIR/mysql-$MYSQL_VERSION.err|g" $MYSQL_HOME/my.cnf
[mysql@lamp1]$ sed -i -e "s|\[mysqld\]|[mysqld]\npid-file=$MYSQL_PID_DIR/mysql-$MYSQL_VERSION.pid|g" $MYSQL_HOME/my.cnf
Start MySQL
Start the ‘MySQL’ daemon:
[mysql@lamp1]$ mysqld_safe --defaults-file=$MYSQL_HOME/my.cnf &
Secure MySQL
Secure the server by running the approved utility, ‘mysql_secure_installation.’ Since the server was just installed the root password will be blank (simply press ‘enter’ when prompted by this script). When prompted, choose ‘Y’ for all options and also provide (and confirm) a password for the ‘root’ user:
[mysql@lamp1]$ mysql_secure_installation --socket=$MYSQL_SOCKET -u root
Post-installation Steps
Enable Automatic Startup/Shutdown
We will set-up a version-specific service – mysql-5.5.21 – using the script provided with the MySQL software.
Set a few environment variables to streamline the process:
[root@lamp1]$ MYSQL_VERSION=5.5.21
[root@lamp1]$ MYSQL_BASE=/u01/app/LAMP/mysql
[root@lamp1]$ MYSQL_HOME=$MYSQL_BASE/$MYSQL_VERSION
Copy/rename the script provided with the MySQL software:
[root@lamp1]$ cp $MYSQL_HOME/support-files/mysql.server /etc/init.d/mysql-$MYSQL_VERSION
Run ‘chkconfig’:
[root@lamp1]$ chkconfig --add mysql-$MYSQL_VERSION
Stop & Start the service to test:
[root@lamp1]$ service mysql-$MYSQL_VERSION stop
[root@lamp1]$ service mysql-$MYSQL_VERSION start
At this point, you should have a fully-functioning MySQL server which will automatically shutdown and start whenever the system is rebooted, etc.