Installing MySQL Server

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.

Installing MySQL Server from source code involves the following steps.
1) Installing missing dependencies – 1st
2) Adding a MySQL User and Group
3) Downloading the source code
4) Unpacking & Installing
5) Post Installation Procedures
6) Installing missing dependencies – 2nd
7) Installation Review

After installing MySQL Server, it is recommended to view the following sections.
Securing MySQL Server
Customizing MySQL Server
Basics of MySQL Server

 

 

1) Installing missing dependencies
Perl-GD support for MySQL Server requires installation of Perl-GD package. Installation instructions here.

 

 

2) Adding a MySQL User and group
Running 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 code
Download 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.

CMake Error: Problem with tar_extract_all(): Invalid argument
CMake Error: Problem extracting tar: /usr/src/mysql-5.6.19/source_downloads/gmock-1.6.0.zip

This occurs due to MySQL Bug 69854, which will be fixed on MySQL 5.7.4 Milestone 14. For the current time, we have to fix this by hand.

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 & Installing
Unpacking
Make 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.

# cd /usr/src


Extract the gzipped tarball containing MySQL Server.

# tar -zxvf mysql-5.6.19.tar.gz
OPTIONS EXPLAINED

-x
extract files from an archive

-v
verbosely list files processed

-z
filter the archive through gzip

-f
use archive file or device ARCHIVE


Extract the gmock zip file into specified directory

# unzip gmock-1.6.0.zip -d mysql-5.6.19/source_downloads
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.


Installing

NOTE: 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 link
http://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.

 

 

5) Post Installation Procedures
Adding MySQL Server executables to system PATH
MySQL Server executables are located at /usr/local/mysql/bin/ and they are 43 in number. We are not going add symbolic links for each of them to /usr/bin/. Instead we will add /usr/local/mysql/bin/ to the system PATH variable.
PATH is an Environment variable and it must be available to all users; both in login and non-login shells. For this we must add a script in the /etc/profile.d/ directory.
NOTE:
Read the difference between Environment Variable and Local Variable.
Read the difference between Login Shell and Non login Shell.

Create a file mysql.sh in /etc/profile.d/ directory with the below content.
This is for the Bash shell.

if ! echo ${PATH} | /bin/grep -q /usr/local/mysql/bin ; then
PATH=/usr/local/mysql/bin:${PATH}
fi

Create a file mysql.csh in /etc/profile.d/ directory with the below content.
This is for C shell and Tenex C shell.

if ( "${path}" !~ */usr/local/mysql/bin* ) then
set path = ( /usr/local/mysql/bin $path )
endif

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 cache
MySQL 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 MANPATH
MySQL 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.

 

Creating the MySQL Server grant tables
In the terminal, change to MySQL Server install directory

# cd /usr/local/mysql


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.

# chown -R mysql:mysql .
OPTIONS EXPLAINED

-R
operate on files and directories recursively


Create the MySQL Server grant tables. Execute the script mysql_install_db as mysql user.

# scripts/mysql_install_db --user=mysql
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.

# chmod -R go-rwx data
OPTIONS EXPLAINED

-R
operate on files and directories recursively

go-rwx
Remove read,write & execute permissions for group & others

 


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 user
MySQL 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 binds
MySQL 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.

 

Initializing the MySQL Server grant tables.
Execute mysqld_safe as mysql user.

# mysqld_safe --user=mysql &
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.


Finally, start the mysql service.

# service mysql start
OPTIONS EXPLAINED

start
starts the specified service

 

 

6) Installing missing dependencies
Still, there is one thing unfinished – MySQL Benchmark Suite.
MySQL Server comes with the MySQL Benchmark Suite, which is installed in /usr/local/mysql/sql-bench/. It can benchmark our MySQL Server and tell whether a given implementation performs well or poorly. The benchmark scripts are written in Perl and uses the DBI module to access database servers.
So we need the following to run MySQL Benchmark SuitePerl, DBI and DBD::mysql. Of the above three, Perl and DBI are installed except DBD::mysql. We did not install DBD::mysql before installing MySQL Server, because DBD::mysql needs MySQL Server to be present prior to it’s installation.
To install DBD::mysql, follow the installation instructions here.
NOTE: Read more about DBI and libdbi.

 

 

7) Installation Review
Install location
/usr/local/mysql/

PID file
Syntax: /usr/local/mysql/data/HOSTNAME.pid
Example: /usr/local/mysql/data/example.com.pid

Socket file
/tmp/mysql.sock

Service file
/etc/rc.d/init.d/mysql

Default Port
3306

Executables
Listed and explained

Configuration files
Listed and explained

Log files
Listed and explained


Get information on Server status
Displays the status of MySQL service

# service mysql status

Displays the status of mysqld daemon

# mysqladmin -u root -p ping

Displays the status of server with variables

# mysqladmin -u root -p status

Display the status of server with variables and their values

# mysqladmin -u root -p extended-status



Get information on active Server threads

Displays a list of active server threads

# mysqladmin -u root -p processlist

Displays a list of active server threads with full processlist

# mysqladmin -u root -p -v processlist



Get information on Server variables

Displays a list of server system variables and their values

# mysqladmin -u root -p variables



Get information on Server version

# mysqladmin -u root -p version

 

 

After installing MySQL Server, it is recommended to view the following sections.
Securing MySQL Server
Customizing MySQL Server
Basics of MySQL Server