Compiling MySQL (Desmistifying)

Today we are going to talk about a topic that many people are afraid of. We know that MySQL is open-source, but there are some steps from the code until we have the database binary running in our server. Many may ask why doing this since there are packages available, but it wouldn’t be funny if we didn’t know how to do it, correct?

Jokes aside, it might be interesting to compile MySQL because it is possible to add certain flags to enable additional libraries that are very useful to investigate certain parts of the code.

Let’s compile MySQL for Ubuntu 20.04 (Focal Fossa):

First, we are going to create our directories. We will create a directory that will have the source code, another one to place the compiled binaries, and the latest one is where we are going to place the boost library.

Next, we need to install the additional Linux packages required to compile MySQL:

Next, we will download the source code. For this, we will use the MySQL repository in GitHub:

Next, we will go to our build directory and we will run CMAKE with our chosen flags:

After we run CMAKE, we are going to compile MySQL de facto, using the make command. To optimize the compiling process, we will use the -j option. The -j indicates how many threads we are going to use to compile MySQL.

Note that we will not find a mysqld binary in the /compile/build/bin/, but instead, we will see a mysqld-debug. This is because of the DWITH_DEBUG option we set previously.

Now, we can test our binary. For this, we are going to manually create the directories, configure the permission and the my.cnf:

And add these settings to the /etc/my.cnf:

Next step , we are going to initialize MySQL data dictionary:

And now, MySQL is ready to be started:

A temporary password will be created and we can extract from the error log:

Now we can validate connecting using the MySQL client of our preference:

And we did it! We have our own custom MySQL running. Our next adventure will be changing a little the source code to start getting familiar with it!

This topic will be present in the book that my colleague and I are writing about MySQL. The book is called Learning MySQL. Stay tuned!

See ya!

Leave a Reply