Building MySQL from Source  /  Installing MySQL Using a Standard Source Distribution

Chapter 2 Installing MySQL Using a Standard Source Distribution

To install MySQL from source, first configure, build, and install from a source package. Then follow the same postinstallation setup sequence as for a binary installation.

If you start from a source RPM, use the following command to make a binary RPM that you can install. If you do not have rpmbuild, use rpm instead.

shell> rpmbuild --rebuild --clean MySQL-VERSION.src.rpm

The result is one or more binary RPM packages that you install as indicated in Installing MySQL on Linux Using RPM Packages.

The sequence for installation from a compressed tar file source distribution is similar to the process for installing from a generic binary distribution that is detailed in Installing MySQL on Unix/Linux Using Generic Binaries. For a MySQL .tar.gz source distribution, the basic installation command sequence looks like this:

# Preconfiguration setup
shell> groupadd mysql
shell> useradd -g mysql -s /bin/false mysql
# Beginning of source-build specific instructions
shell> tar zxvf mysql-VERSION.tar.gz
shell> cd mysql-VERSION
shell> ./configure --prefix=/usr/local/mysql
shell> make
shell> make install
# End of source-build specific instructions
# Postinstallation setup
shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> bin/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql var
# Next command is optional
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server

This procedure does not set up any passwords for MySQL accounts. After following the procedure, proceed to Postinstallation Setup and Testing, for postinstallation setup and testing.

A more detailed version of the preceding description for installing MySQL from a source distribution follows:

  1. Add a login user and group for mysqld to run as:

    shell> groupadd mysql
    shell> useradd -g mysql -s /bin/false mysql

    These commands add the mysql group and the mysql user. The syntax for useradd and groupadd may differ slightly on different versions of Unix, or they may have different names such as adduser and addgroup.

    You might want to call the user and group something else instead of mysql. If so, substitute the appropriate name in the following steps.

  2. Perform the following steps as the mysql user, except as noted.

  3. Pick the directory under which you want to unpack the distribution and change location into it.

  4. Obtain a distribution file using the instructions in How to Get MySQL.

  5. Unpack the distribution into the current directory. tar can uncompress and unpack the distribution if it has z option support:

    shell> tar zxvf /path/to/mysql-VERSION.tar.gz

    This command creates a directory named mysql-VERSION.

    If your tar does not have z option support, use gunzip to unpack the distribution and tar to unpack it:

    shell> gunzip < /path/to/mysql-VERSION.tar.gz | tar xvf -
  6. Change location into the top-level directory of the unpacked distribution:

    shell> cd mysql-VERSION

    Note that currently you must configure and build MySQL from this top-level directory. You cannot build it in a different directory.

  7. Configure the release and compile everything:

    shell> ./configure --prefix=/usr/local/mysql
    shell> make

    When you run configure, you might want to specify other options. For example, if you need to debug mysqld or a MySQL client, run configure with the --with-debug option, and then recompile and link your clients with the new client library. See Debugging and Porting MySQL.

    Run ./configure --help for a list of options. Chapter 4, MySQL Source-Configuration Options, discusses some of the more useful options.

    If configure fails and you are going to send mail to a MySQL mailing list to ask for assistance, please include any lines from config.log that you think can help solve the problem. Also include the last couple of lines of output from configure. To file a bug report, please use the instructions in How to Report Bugs or Problems.

    If the compile fails, see Chapter 5, Dealing with Problems Compiling MySQL, for help.

  8. Install the distribution:

    shell> make install

    You might need to run this command as root.

    If you want to set up an option file, use one of those present in the support-files directory as a template. For example:

    shell> cp support-files/my-medium.cnf /etc/my.cnf

    You might need to run this command as root.

    If you want to configure support for InnoDB tables, you should edit the /etc/my.cnf file, removing the # character before the option lines that start with innodb_..., and modify the option values to be what you want. See Using Option Files, and Configuring InnoDB.

  9. Change location into the installation directory:

    shell> cd /usr/local/mysql
  10. If you ran the make install command as root, the installed files will be owned by root. Ensure that the installation is accessible to mysql by executing the following commands as root in the installation directory:

    shell> chown -R mysql .
    shell> chgrp -R mysql .

    The first command changes the owner attribute of the files to the mysql user. The second changes the group attribute to the mysql group.

  11. If you have not installed MySQL before, you must create the MySQL data directory and initialize the grant tables:

    shell> bin/mysql_install_db --user=mysql

    If you run the command as root, include the --user option as shown. If you run the command while logged in as mysql, you can omit the --user option.

    The command should create the data directory and its contents with mysql as the owner.

    After using mysql_install_db to create the grant tables for MySQL, you must restart the server manually. The mysqld_safe command to do this is shown in a later step.

  12. Most of the MySQL installation can be owned by root if you like. The exception is that the data directory must be owned by mysql. To accomplish this, run the following commands as root in the installation directory:

    shell> chown -R root .
    shell> chown -R mysql var
  13. If the plugin directory is writable by the server, it may be possible for a user to write executable code to a file in the directory using SELECT ... INTO DUMPFILE. This can be prevented by making plugin_dir read only to the server or by setting --secure-file-priv to a directory where SELECT writes can be made safely.

  14. If you want MySQL to start automatically when you boot your machine, you can copy support-files/mysql.server to the location where your system has its startup files. More information can be found in the support-files/mysql.server script itself; see also Starting and Stopping MySQL Automatically.

  15. You can set up new accounts using the bin/mysql_setpermission script if you install the DBI and DBD::mysql Perl modules. See mysql_setpermission — Interactively Set Permissions in Grant Tables. For Perl module installation instructions, see Perl Installation Notes.

After everything has been installed, test the distribution. To start the MySQL server, use the following command:

shell> /usr/local/mysql/bin/mysqld_safe --user=mysql &

If you run the command as root, you should use the --user option as shown. The option value is the name of the login account that you created in the first step to use for running the server. If you run the mysqld_safe command while logged in as that user, you can omit the --user option.

If the command fails immediately and prints mysqld ended, look for information in the error log (which by default is the host_name.err file in the data directory).

More information about mysqld_safe is given in mysqld_safe — MySQL Server Startup Script.

To make it more convenient to invoke programs installed in /usr/local/mysql/bin, you can add that directory to your PATH environment variable setting. That enables you to run a program by typing only its name, not its entire path name. See Setting Environment Variables.


The accounts that are listed in the MySQL grant tables initially have no passwords. After starting the server, you should set up passwords for them using the instructions in Postinstallation Setup and Testing.

Download this Excerpt
PDF (US Ltr) - 194.2Kb
PDF (A4) - 194.9Kb
EPUB - 66.3Kb
HTML Download (TGZ) - 50.7Kb
HTML Download (Zip) - 62.7Kb
User Comments
  Posted by Anoop Viswanath on July 2, 2014
Detailed instructions for compiling MySQL Server from source code on Linux.

This one worked for me.

It also fixes the following errors with Googlemock.
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.
CMake Error: Problem with tar_extract_all(): Invalid argument
CMake Error: Problem extracting tar: /usr/src/mysql-5.6.19/source_downloads/

Thanks to the author.
  Posted by Shahriyar Rzayev on June 29, 2015
With MySQL 5.7.7-rc if you try to use mysql_install_db it will throw an error indicating that, this script is deprecated:

[root@centos7_vm mysql]# bin/mysql_install_db --user=mysql
2015-06-29 11:24:11 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2015-06-29 11:24:11 [ERROR] The data directory needs to be specified.

Also, if you initialized MySQL to different datadir rather than default:

[root@centos7_vm mysql]# bin/mysqld --initialize --user=mysql --datadir=/opt/mysql/datadir

And after try to use bin/mysql_ssl_rsa_setup:

[root@centos7_vm mysql]# bin/mysql_ssl_rsa_setup
2015-06-29 11:42:28 [ERROR] Failed to normalize the argument for --datadir: /var/lib/mysql

As a workaround try to specify --datadir with mysql_ssl_rsa_setup too:

[root@centos7_vm mysql]# bin/mysql_ssl_rsa_setup --datadir=/opt/mysql/datadir
OpenSSL 1.0.1e-fips 11 Feb 2013

Sign Up Login You must be logged in to post a comment.