Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building MySQL 5.7 from Source on Ubuntu with CMake

Tech Jun 6 1

Prerequisites

The following components are required for compilation:

  • CMake 3.16.3
  • OpenSSL 1.1.1k
  • MySQL 5.7.36 source code
  • Boost 1.59.0

Environment: Ubuntu 16.04 virtual machine

Installing CMake

Two installation methods are available:

Method 1: Package manager installation

sudo apt-get install cmake

Method 2: Binary package installation

Download from: https://cmake.org/download

After transferring to the VM, extract the archive containing cmake-${version}-Linux-x86_64.sh. Execute directly for system-wide installation:

./cmake-${version}-Linux-x86_64.sh

To avoid conflicts with existing installations, specify a custom path:

./cmake-${version}-Linux-x86_64.sh --prefix=/opt/cmake

Source Preparation

Extract all three packages:

  1. MySQL 5.7.36 source code
  2. OpenSSL 1.1.1k
  3. Boost 1.59.0

Move the extracted OpenSSL and Boost directories into the MySQL source directory.

Compilation Process

Navigate to the MySQL source directory and execute CMake configuration:

cmake . -DCMAKE_INSTALL_PREFIX=${PWD}/__install \
        -DWITH_SSL=${PWD}/openssl-1.1.1k \
        -DDOWNLOAD_BOOST=1 \
        -DWITH_BOOST=${PWD}/boost_1_59_0

Configuration parameters explained:

  • -DCMAKE_INSTALL_PREFIX: Destination directory for compiled libraries
  • -DWITH_SSL: Path to OpenSSL installation
  • -DDOWNLOAD_BOOST: Enable Boost dependency handling
  • -DWITH_BOOST: Path to Boost library

Proceed with compilation:

make

Note: Avoid parallel compilation (make -j) as it may cause build failures.

Install the compiled libraries:

make install

Verification

Navigate to the installation directory specified by CMAKE_INSTALL_PREFIX and create test_client.c:

#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>

int main(int argc, char* argv[]) {
    MYSQL* connection = mysql_init(NULL);
    
    if (connection == NULL) {
        printf("MySQL initialization failed\n");
        return 1;
    }
    
    printf("MySQL client library loaded successfully\n");
    mysql_close(connection);
    return 0;
}

Compile the test program:

gcc -o client_test test_client.c \
    -I./include/ \
    ./lib/libmysqlclient.a \
    ./lib/libmysqld.a \
    ./lib/libmysqlservices.a \
    -lstdc++ -ldl -lpthread

Important: The system libraries -lstdc++, -ldl, and -lpthread must appear after the MySQL libraries in the linking command.

Execute the test:

./client_test

Successful execution confirms proper compilation and linking of the MySQL client libraries.

Tags: MySQLcmake

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.