Here we will learn how to Compile and Install the MySQL Server from source code. After that we will Configure our server for basic functionality and Secure it for general usage. Finally we will discuss the Basics of MySQL Server.

2) Adding a MySQL User and groupRunning the MySQL Server as root user is insecure and so not advisable. It is better to run the MySQL Server under a system account with limited privileges. So we will create a User and Group for MySQL.
In the terminal, execute the following command.

# useradd -r -U mysql -M -d /usr/local/mysql/data

NOTE: The home directory of mysql user is set to the data directory /usr/local/mysql/data/. This is because we plan to install MySQL Server in the directory /usr/local/mysql/. You can use a different directory for installation or data storage, if you wish.

OPTIONS EXPLAINED
-U
Create a group with the same name as the user and add the user to this group, in case USERGROUPS_ENAB in your /etc/login.defs is set to no. (By default this is set to yes).
-r
Create as system account
-M
Do not create the user´s home directory, in case CREATE_HOME in your /etc/login.defs is set to yes. (By default this is set to no). This is used because we do not want the files and directories contained in the skeleton directory to be copied to home directory.
-d
The new user will be created using HOME_DIR as the value for the user´s login directory. The default is to append the LOGIN name to BASE_DIR and use that as the login directory name. The directory HOME_DIR does not have to exist but will not be created if it is missing.

This will create a user and group, both named as mysql. They will be created as system accounts and there will be no password set. The home directory will be set to /usr/local/mysql/data/ but it will not be created. The login shell will be /bin/bash.

3) Downloading the source codeDownload the MySQL Community Server source code
MySQL Community Server is the freely downloadable version of MySQL Server. It is available from the MySQL Developer Zone.Let us goto http://dev.mysql.com/downloads/. On the top of page, click on the Downloads tab, which will take us to the download page listing different MySQL projects. Below MySQL Community Server, click on the DOWNLOAD link, which will take us to the download page. We will see a list of Generally Available (GA) Releases. Select Platform as Source Code. Scroll to the bottom and we will see Generic Linux (Architecture Independent), Compressed TAR Archive. Click the Download button, which will take us to a page where we can Login/SignUp with an Oracle Web account. If you want, you can. But I chose to click on No thanks, just start my download.. This will start the download.The downloaded file mysql-5.6.19.tar.gz will be 31.4 MB in size.

Download the Google C++ Mocking Framework source code
Google C++ Mocking Framework(gmock) is a library for writing and using C++ mock classes. MySQL Server uses it for Google C++ Testing Framework(gtest) based unit tests. If you do not download this now, the following error will be dispalyed during the configure process.

Googlemock was not found. gtest-based unit tests will be disabled. You can run cmake . -DENABLE_DOWNLOADS=1 to automatically download and build required components from source.

Even if you enable the option ENABLE_DOWNLOADS=1 (that is, assuming you have an active internet connection), configure script will give you the following error.

Google C++ Mocking Framework is available at Google Code Project Hosting. Goto http://code.google.com/p/googlemock/. On the top of page, click on the Downloads tab, which will take us to the page with download listing. The version of Google C++ Mocking Framework, which configure script tries to download is 1.6.0. So click on gmock-1.6.0.zip, which will take us to the download page. Click on gmock-1.6.0.zip. This will start the download.The downloaded zip file gmock-1.6.0.zip is 2.0 MB in size.

4) Unpacking & InstallingUnpackingMake sure you have copied the downloaded files mysql-5.6.19.tar.gz and gmock-1.6.0.zip to directory /usr/src/. In the terminal, change to /usr/src/ directory.

OPTIONS EXPLAINED
-d
An optional directory to which to extract files. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the normal options), immediately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note that this may cause normal shell behavior to be suppressed. In particular, ‘‘-d ~’’ (tilde) is expanded by Unix C shells into the name of the user’s home directory, but ‘‘-d~’’ is treated as a literal subdirectory ‘‘~’’ of the current directory.

This will create a directory named source_downloads in the mysql-5.6.19 directory and extract the conents of zip file into it.

InstallingNOTE: Installation is performed based on the rules iRULE1 and iRULE3.
Now we will have a directory mysql-5.6.19 with the extracted files. Change to that directory in terminal.

# cd mysql-5.6.19

Execute cmake.NOTE:• The default install directory is /usr/local/mysql/. If you want to change it, use the option CMAKE_INSTALL_PREFIX. example: # cmake . -DCMAKE_INSTALL_PREFIX=your_custom_location• The default data directory is /usr/local/mysql/data/. If you want to change it, use the option MYSQL_DATADIR. example: # cmake . -DMYSQL_DATADIR=your_custom_location• To see the default configure options for our installation, use the below command command: # cmake . -LH• To see the complete list of configure options, see the following linkhttp://dev.mysql.com/doc/mysql-sourcebuild-excerpt/5.6/en/source-configuration-options.html

# cmake .

cmake will check our system for required dependencies, assigns values for system-dependent variables and use these values to generate the Makefile.

Execute make.

# make

make will look at our Makefile, compile our program code and create the executables in the sequence described.

Execute make install.

# make install

OPTIONS EXPLAINED
install
install will look for the target install in Makefile, and install MySQL Server to the specified location.

From this moment on, any new login shells or non-login shells spawned will have /usr/local/mysql/bin/ in the PATH variable. But the current shell on which we are working does not have it. Because it was spawned before the script was placed in the global profile directory. So we will have to source the script on our working shell.I am using the Bash shell. So I executed the following command on my current terminal.

# source /etc/profile.d/mysql.sh

Adding MySQL Server libraries to the shared library cacheMySQL Server libraries are located at /usr/local/mysql/lib/. But these are not shared yet. For other applications to find these libraries at runtime, the dynamic linker cache in Linux must be updated with the information about these libraries.

Create a file mysql.conf in the directory /etc/ld.so.conf.d/ with the entry /usr/local/mysql/lib

# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf

Run ldconfig.

# ldconfig

ldconfig scans the ld.so.conf.d folder and updates the shared library cache; so that dynamic linker can find them during runtime.

Adding MySQL Server manpages to the MANPATHMySQL Server manpages are located at /usr/local/mysql/man/. We have already added /usr/local/mysql/bin/ to the system PATH variable. So man tool will automatically search the location /usr/local/mysql/man/ for manpages. No extra changes need to be made.

Change the owner and group of /usr/local/mysql/ directory and it’s contents to mysql. This is done for installing the MySQL Server‘s system grant tables. We will be reverting the permissions after that.

OPTIONS EXPLAINED
--user
The login username to use for running mysqld. Files and directories created by mysqld will be owned by this user. You must be root to use this option. By default mysqld runs using your current login name and files and directories that it creates will be owned by you.

Change back the owner and group of /usr/local/mysql/ directory and it’s contents to root.

# chown -R root .

OPTIONS EXPLAINED
-R
operate on files and directories recursively

Change the owner of /usr/local/mysql/ directory to mysql.

# chown -R mysql data

OPTIONS EXPLAINED
-R
operate on files and directories recursively

The data directory has already it’s group ownership set to mysql. With this command both the group and owner of /usr/local/mysql/data/ directory will be set to mysql.

Remove the permissions for group and others on /usr/local/mysql/data/ directory. So that only mysql will have access to it.

Setting the configuration file
Copy the default configuration file my-default.cnf into /etc/ directory and rename it to my.cnf.

# cp support-files/my-default.cnf /etc/my.cnf

Open the MySQL Server configuration file /etc/my.cnf. We have to make certain changes for the server to work properly.NOTE: If you have changed the location of MySQL Server install directory or data directory The default install directory for MySQL Server is /usr/local/mysql/. And the default data directory for MySQL Server is /usr/local/mysql/data/. If you have changed any of them, make sure to make the appropriate changes to the values basedir and datadir.

Setting the MySQL userMySQL Server by default runs as the user root. Running MySQL Server as the root user is insecure and so not advisable. So will set MySQL Server to run as the standard user mysql. Just below the [mysqld]section add a new line as shown below.

user = mysql

Configuring the IP to which MySQL Server bindsMySQL Server by default binds to all IP addresses on all interfaces of a given system. If we want MySQL Server to bind on only a given IP address, we must configure as follows. Under the [mysqld] section, add the following line with the IP address you want to bind to.NOTE: Replace 192.168.0.100 with the IP address of your machine.

bind-address = 192.168.0.100

This makes MySQL Server to bind to IP address 192.168.0.100.NOTE: If we want MySQL Server to bind to a selected set of interfaces only, we can leave MySQL Server to it’s default behaviour, with firewalling off the interfaces we do not want to bind to.

OPTIONS EXPLAINED
--user
Run the mysqld server as the user having the name user_name or the numeric user ID user_id. (“User” in this context refers to a system login account, not a MySQL user listed in the grant tables.)

mysqld_safe is the recommended way to start a mysqld server on UNIX and NetWare. It initializes the mysql grant tables containing the privileges that determine how users are permitted to connect to the server. mysqld_safe also adds some safety features such as restarting the server when an error occurs and logging runtime information to an error log file.NOTE: Prepending & to the command, moves the command to background and gives us the terminal back for further jobs. If the command spits out standard output after the service is running, just press CTRL-C or close that terminal and open a new one.

Setting the MySQL Server service
Copy the MySQL Server daemon start/stop script mysql.server to /etc/rc.d/init.d/ directory and rename it to mysql.

# cp -v support-files/mysql.server /etc/rc.d/init.d/mysql

OPTIONS EXPLAINED
-v
explain what is being done

Add mysql as a Sys V init service.

# chkconfig --add mysql

OPTIONS EXPLAINED
--add
This option adds a new service for management by chkconfig. When a new service is added, chkconfig ensures that the service has either a start or a kill entry in every runlevel. If any runlevel is missing such an entry, chkconfig creates the appropriate entry as specified by the default values in the init script. Note that default entries in LSB-delimited ’INIT INFO’ sections take precedence over the default runlevels in the initscript; if any Required-Start or Required-Stop entries are present, the start and stop priorities of the script will be adjusted to account for these dependencies.

This option adds mysql as a Sys V init service to be managed by chkconfig. When a new service is added, chkconfig ensures that the service has either a start or a kill entry in every runlevel. If any runlevel is missing such an entry, chkconfig creates the appropriate entry as specified by the default values in the init script.chkconfig also creates the following softlinks for our mysql script to the corresponding runlevel directories./etc/rc.d/rc0.d/K36mysql /etc/rc.d/rc1.d/K36mysql /etc/rc.d/rc2.d/S64mysql /etc/rc.d/rc3.d/S64mysql /etc/rc.d/rc4.d/S64mysql /etc/rc.d/rc5.d/S64mysql /etc/rc.d/rc6.d/K36mysql

NOTE: K36mysql – K stands for Kill. 36 implies mysql will be killed in 36th of the order. This is intended for runlevels 0(shutdown), 1(single-user mode), 6(reboot). So mysql service will be killed in runlevels 0, 1 and 6. S64mysql – S stands for Start. 64 implies mysql will be started in 64th of the order. This is intended for GUI/custom runlevels 2, 3, 4, 5. So mysql service will be started in runlevels 2, 3, 4 and 5.