Building MySQL 5.7 from Source on Ubuntu with CMake
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:
- MySQL 5.7.36 source code
- OpenSSL 1.1.1k
- 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.