How to install MySQL from source on CentOS 6

To install MySQL from source on CentOS 6

MySQL is a database server which can be installed from either source code or by using yum package manager. Installing an application directly from its source contains a lot of benefits, and in this tutorial, you are going to learn about the installation of MySQL from its source on CentOS 6. MySQL's source code is available on it's official website, you can download it from there and install it on your server.


Installing MySQL from Source Code

Before compiling MySQL from it's source, make sure you install the following dependency packages by running the following command.

[root@linuxhelp ~]# yum groupinstall "Development Tools" -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Group Process
Determining fastest mirrors
* base:
* extras:
* updates:
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Package patch-2.6-6.el6.x86_64 already installed and latest version
Package 1:pkgconfig-0.23-9.1.el6.x86_64 already installed and latest version
Package gettext-0.17-18.el6.x86_64 already installed and latest version
Package elfutils-0.164-2.el6.x86_64 already installed and latest version
Package cvs-1.11.23-16.el6.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package autoconf.noarch 0:2.63-5.1.el6 will be installed
---> Package automake.noarch 0:1.11.1-4.el6 will be installed
---> Package binutils.x86_64 0: will be updated
---> Package binutils.x86_64 0: will be an update
---> Package bison.x86_64 0:2.4.1-5.el6 will be installed
---> Package byacc.x86_64 0:1.9.20070509-7.el6 will be installed
---> Package cscope.x86_64 0:15.6-7.el6 will be installed
Dependency Installed:
cloog-ppl.x86_64 0:0.15.7-1.2.el6 cpp.x86_64 0:4.4.7-18.el6 gettext-devel.x86_64 0:0.17-18.el6 gettext-libs.x86_64 0:0.17-18.el6
kernel-devel.x86_64 0:2.6.32-696.13.2.el6 libgcj.x86_64 0:4.4.7-18.el6 libgfortran.x86_64 0:4.4.7-18.el6 libstdc++-devel.x86_64 0:4.4.7-18.el6
mpfr.x86_64 0:2.4.1-6.el6 perl-Compress-Raw-Zlib.x86_64 1:2.021-144.el6 perl-Compress-Zlib.x86_64 0:2.021-144.el6 perl-Error.noarch 1:0.17015-4.el6
perl-Git.noarch 0:1.7.1-9.el6_9 perl-HTML-Parser.x86_64 0:3.64-2.el6 perl-HTML-Tagset.noarch 0:3.20-4.el6 perl-IO-Compress-Base.x86_64 0:2.021-144.el6
perl-IO-Compress-Zlib.x86_64 0:2.021-144.el6 perl-URI.noarch 0:1.40-2.el6 perl-XML-Parser.x86_64 0:2.36-7.el6 perl-libwww-perl.noarch 0:5.833-5.el6
ppl.x86_64 0:0.10.2-11.el6 systemtap-client.x86_64 0:2.9-7.el6 systemtap-devel.x86_64 0:2.9-7.el6

binutils.x86_64 0:


Since the MySQL source code can be installed by using cmake command, let's install Cmake package as follows.

[root@linuxhelp ~]# yum install cmake ncurses-devel -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base:
* extras:
* updates:
Resolving Dependencies
--> Running transaction check
---> Package cmake.x86_64 0: will be installed
---> Package ncurses-devel.x86_64 0:5.7-4.20090207.el6 will be installed
--> Finished Dependency Resolution
Running Transaction
Installing : ncurses-devel-5.7-4.20090207.el6.x86_64 1/2
Installing : cmake- 2/2
Verifying : cmake- 1/2
Verifying : ncurses-devel-5.7-4.20090207.el6.x86_64 2/2

cmake.x86_64 0: ncurses-devel.x86_64 0:5.7-4.20090207.el6


MySQL also requires boost, so it should be installed on your machine for the source code compilation. You can install it by using yum command or by using source code for boost. Or else by simply downloading the MySQL package along its boost package by including it in the command as follows.

[root@linuxhelp ~]# wget
--2017-11-12 23:58:25--
Connecting to||:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: [following]
--2017-11-12 23:58:27--
Connecting to||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 48833145 (47M) [application/x-tar-gz]
Saving to: “mysql-boost-5.7.20.tar.gz”

100%[=================================================================================================================================================================>] 48,833,145 951K/s in 59s

2017-11-12 23:59:27 (807 KB/s) - “mysql-boost-5.7.20.tar.gz” saved [48833145/48833145]


You shall now extract downloaded package.

[root@linuxhelp ~]# tar -xzf mysql-boost-5.7.20.tar.gz


Now, move to extracted directory so that you can install MySQL source. Also, run the cmake command as in the following way. 

[root@linuxhelp ~]# cd mysql-5.7.20/
[root@linuxhelp mysql-5.7.20]# ls
boost cmake config.h.cmake dbug extra libbinlogevents libmysql man mysys_ssl rapid scripts storage testclients vio
BUILD CMakeLists.txt configure.cmake Docs include libbinlogstandalone libmysqld mysql-test packaging README sql strings unittest win
client cmd-line-utils COPYING Doxyfile-perfschema INSTALL libevent libservices mysys plugin regex sql-common support-files VERSION zlib

[root@linuxhelp mysql-5.7.20]# cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql -DWITH_BOOST=/root/mysql-5.7.20/boost/
-- Running cmake version
-- Found Git: /usr/bin/git (found version "1.7.1")
-- Configuring with MAX_INDEXES = 64U
-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- CMAKE_GENERATOR: Unix Makefiles
-- Looking for SHM_HUGETLB
-- Looking for SHM_HUGETLB – found
-- CMAKE_C_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Wwrite-strings -Wdeclaration-after-statement
-- CMAKE_CXX_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Woverloaded-virtual -Wno-unused-parameter
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- Configuring done
-- Generating done
-- Build files have been written to: /root/mysql-5.7.20



Now run make command, this command may take a while to process, may be more than 2 hours, but it depends on the system performance. So, wait until the process gets over.

[root@linuxhelp mysql-5.7.20]# make
Scanning dependencies of target INFO_BIN
[ 0%] Built target INFO_BIN
Scanning dependencies of target INFO_SRC
[ 0%] Built target INFO_SRC
Scanning dependencies of target abi_check
[ 0%] Built target abi_check
Scanning dependencies of target zlib
[ 0%] Building C object zlib/CMakeFiles/zlib.dir/adler32.c.o
[ 0%] Building C object zlib/CMakeFiles/zlib.dir/compress.c.o
[ 0%] Building C object zlib/CMakeFiles/zlib.dir/crc32.c.o
[ 0%] Building C object zlib/CMakeFiles/zlib.dir/deflate.c.o
[ 0%] Building C object zlib/CMakeFiles/zlib.dir/gzio.c.o
[ 0%] Building C object zlib/CMakeFiles/zlib.dir/infback.c.o
[ 0%] Building C object zlib/CMakeFiles/zlib.dir/inffast.c.o
[ 0%] Building C object zlib/CMakeFiles/zlib.dir/inflate.c.o
[ 0%] Building C object zlib/CMakeFiles/zlib.dir/inftrees.c.o
[ 0%] Building C object zlib/CmakeFiles/zlib.dir/trees.c.o
-- Up-to-date: /usr/local/mysql/mysql-test/mtr
-- Up-to-date: /usr/local/mysql/mysql-test/mysql-test-run
-- Installing: /usr/local/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Up-to-date: /usr/local/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Installing: /usr/local/mysql/mysql-test/lib/My/SafeProcess/
-- Installing: /usr/local/mysql/support-files/mysqld_multi.server
-- Installing: /usr/local/mysql/support-files/mysql-log-rotate
-- Installing: /usr/local/mysql/support-files/magic
-- Installing: /usr/local/mysql/share/aclocal/mysql.m4
-- Installing: /usr/local/mysql/support-files/mysql.server


All the MySQL files will be located under /usr/local/mysql directory where you can find bin directory for MySQL daemon files. Now create my.cnf file on MySQL installed path.

[root@linuxhelp mysql-5.7.20]# cd /usr/local/mysql/
[root@linuxhelp mysql]# vim my.cnf




Once it is done, create a user for MySQL with no login shell as follows.

[root@linuxhelp mysql]# useradd mysql -s /sbin/nologin

And then, change ownership for mysql directory by making use of the following command.

[root@linuxhelp mysql]# chown -R mysql.mysql /usr/local/mysql/

And after that, you should initialize mysqld daemon, and for that make sure you run the following command.

[root@linuxhelp mysql]# bin/mysqld --initialize --user=mysql
2017-11-13T09:41:23.751963Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-11-13T09:41:26.623584Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-11-13T09:41:26.882632Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-11-13T09:41:27.014087Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d1c4626e-c856-11e7-a67a-000c292f80f2.
2017-11-13T09:41:27.017861Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-11-13T09:41:27.022076Z 1 [Note] A temporary password is generated for root@localhost: qSkPXuVgy3%w

A temporary password is generated for MySQL root user, copy it as you'll need it when you log in for the first time in the MySQL database.


Now start MySQL daemon using the following command. The daemon is started from the directory in which MySQL is installed(/usr/local/mysql).

[root@linuxhelp mysql]# bin/mysqld_safe --user=mysql &
[1] 22616
[root@linuxhelp mysql]# 2017-11-13T09:43:38.765561Z mysqld_safe Logging to '/var/log/mysqld.log'.
2017-11-13T09:43:38.883409Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql


You shall login to your MySQL database with the help of the following command and enter the copied temporary root password in there.

[root@linuxhelp mysql]# bin/mysql -u root -p -S /usr/local/mysql/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.20

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

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



Now we can change root password using below command inside mysql shell prompt

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> exit


You can now re-login to MySQL database with your new password. Once you are done with that, you need to configure MySQL service daemon, hence use the following command to copy init script for mysqld daemon to /etc/init.d/ directory.

[root@linuxhelp mysql]# cp /root/mysql-5.7.20/support-files/mysql.server /etc/init.d/mysqld
[root@linuxhelp mysql]# chmod +x /etc/init.d/mysqld


Since the MySQL service is already running, you need to kill it before you use the service command, and that can be done by using the following command.

[root@linuxhelp mysql]# ps aux | grep mysql
root 22616 0.0 0.1 106220 1580 pts/0 S 04:43 0:00 /bin/sh bin/mysqld_safe --user=mysql
mysql 22816 0.1 19.3 1173384 194532 pts/0 Sl 04:43 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/usr/local/mysql/ --socket=/usr/local/mysql/mysql.sock --port=3306
root 22920 0.0 0.0 103328 888 pts/0 S+ 04:51 0:00 grep mysql
[root@linuxhelp mysql]# kill -9 22616
[root@linuxhelp mysql]# kill -9 22816
[1]+ Killed bin/mysqld_safe –user=mysql


Now start MySQL daemon with service command as follows.

[root@linuxhelp mysql]# service mysqld start
Starting MySQL.. [ OK ]
[root@linuxhelp mysql]# service mysqld status
MySQL running (23151)

With this, the installation of MySQL  from its source code comes to an end. 



Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: CentOS MySQL
user image Author :  Matthew