MySQL source code is based on the specific implementation of relational model theory, is the combination of database theory and practice.

Reading the source code of MySQL and related tools is not only the daily work of database developers, but also the only way for DBA to advance and improve the technical level in an all-round way.

  • Tamping principle: more profound understanding of database basic theory and related theories such as transaction;
  • Performance optimization: Better understand the functions of configuration items, adapt to the environment, and improve performance.
  • Fault location: help to locate the database fault quickly, know how and why;
  • Embrace open source: Modify the source code (fix bugs, improve features, improve performance) and give back to open source.

| where to begin reading?

Read MySQL source code, mainly refers to read mysql-server[1] in the code. It requires not only in-depth study of theoretical knowledge, but also practical application and practice. Specifically, it can be started from the following perspectives, from different dimensions and different depths to understand the source code of MySQL.

1. SQL execution process

Through the environment debugging, master the complete execution process of SQL statement (DDL/DML, etc.) from the theoretical basis and practice.

2. Hierarchy

Through the MySQL source code structure level, master the underlying technical principles.

  • Access layer: MySQL protocol, connection thread pool, etc.
  • SQL layer: lexical, syntax parsing, executor, optimizer;
  • Storage engine layer: plug-in framework, storage engine design principles, such as InnoDB, RocksDB architecture, etc.

3. Data abstraction/data transformation

The database presents a two-dimensional relational table to the user. When reading the source code, you can read it from the perspective of data abstraction/data transformation.

  • Data abstraction: Two-dimensional representation of relational tables in the SQL layer/storage engine
  • Data conversion: Data conversion of two-dimensional relational tables in SQL layer to storage engine/data conversion of storage engine to disk

4. Functional realization

Familiarize yourself with the MySQL source code by observing how each specific function is implemented in the database.

| debugging environment

Is the so-called: “to good work must first sharpen its tools”, read the source code must read the “dynamic” state code, only look at the “static” state code must be boring and useless. Therefore, building an environment that can debug MySQL source code is an essential process for reading MySQL source code, practicing and mastering MySQL technical knowledge.

This article introduces the debugging process of using CLion in Ubuntu, to demonstrate the debugging process of MySQL source code environment, that is, to master the SQL execution process from practice.

CLion profile

CLion[2] is a cross-platform IDE designed specifically for developing C and C++. It includes plug-ins and smart features to help developers develop code more efficiently.

The environment

  • Operating system: 20.04.1-Ubuntu
  • CLion version: CLion 2021.1.1
  • MySQL version: 8.0.25

Compile the rely on

apt install libncurses-dev
apt install libssl-dev
apt install cmake
apt-get install bison
Copy the code

MySQL 8.0.25 relies on Boost 1.73.0 [3]

CLion debugging configuration

To debug the MySQL source code using CLion, you must configure the MySQL compilation path and MySQL installation path in the same way.

1. Place the downloaded source code in the home directory

2. Decompress the source code and create build and /build/data directories under the source code directory

3. Configure the Cmake compilation directory of CLion

4. Configure Cmake compiler parameters for CLion

-DCMAKE_BUILD_TYPE=Debug -DWITH_BOOST=/home/tool/boost_1_73_0 -dcmake_install_prefix =/home/code/mysql-server-mysql-8.0.25/build -dmysql_datadir =/home/code/mysql-server-mysql-8.0.25/build/data Home/code/mysql server - mysql - 8.0.25 / build - DMYSQL_UNIX_ADDR = / home/code/mysql server - mysql - 8.0.25 / build/data/mysql. The sockCopy the code

5. MySQL compilation

Method 1: Compile directly using CLion

Method two: manual compilation

CD /home/code/mysql-server-mysql-8.0.25/build make -j 4Copy the code

Note: When CLion imports the MySQL source code, the makefile is generated under /home/code/mysql-server-mysql-8.0.25/build. You can run the make command directly.

6. Initialize the MySQL database

Groupadd MySQL useradd -r -g MySQL -s /bin/false MySQL # MySQL database initialization CD / home/code/mysql server - mysql - 8.0.25 / build/bin/mysqld -- basedir = / home/code/mysql server - mysql - 8.0.25 / build \ - the datadir = / home/code/mysql server - mysql - 8.0.25 / build/data \ - the initialize - insecure - user = mysqlCopy the code

7. Modify file permissions

Chmod -r 777 / home/code/mysql server - mysql - 8.0.25 / build/dataCopy the code

8. Set CLion’s mysqld configuration parameters

The Program arguments parameter is set to:

- basedir = / home/code/mysql server - mysql - 8.0.25 / build - datadir = / home/code/mysql server - mysql - 8.0.25 / build/data --user=mysqlCopy the code

The Working Directory parameter is set to:

/ home/code/mysql server - mysql - 8.0.25 / build/dataCopy the code

9. Debugging mysqld

Step 1: Start mysqld and verify that mysqld is startedps -ef | grep mysql

Step 2: Add a breakpoint to the do_command function in sql_parse.cc.

Step 3: Log in to the MySQL database and check whether the environment is running properly.

/mysql -uroot -h127.0.0.1 -p3306 -pCopy the code

Note: No password is required for the first login, just press enter. The initialization parameter –initialize-insecure.

| epilogue

So far, the MySQL source code debugging environment is running properly. You can perform related operations in the debugging environment. Understand MySQL source code operation mechanism, implementation principle, configuration parameters, etc.

Next up

In the next installment, we’ll read the source code for the MySQL Plug-in Framework section using the prepared environment

reference

[1]. Mysql-server:github.com/mysql/mysql…

[2]. CLion:www.jetbrains.com/clion/

[3]. boost :www.boost.org/

About RadonDB

The RadonDB open Source community is a cloud-oriented, containerized open source database community. To provide database technology enthusiasts around the mainstream open source database (MySQL, PostgreSQL, Redis, MongoDB, ClickHouse, etc.) technology sharing platform, and provide enterprise RadonDB open source products and services. Currently the RadonDB open source database family has been Everbright bank, bank of Shanghai pudong development bank in silicon valley, hami, taikang life insurance, taiping insurance, axa, sunshine, life, anji logistics, one hundred AnChang logistics, blue moon, TianCai ShangLong, Luo Kejia China, zhe hui run sports science and technology, wuxi, Beijing telecom, jiangsu traffic holding airlines, sichuan airlines, kunming, the thousands of companies such as biological control is used and the community of users. RadonDB can be delivered based on cloud platform and Kubernetes container platform. It not only provides database product solutions covering multiple scenarios, but also provides professional cluster management and automatic operation and maintenance capabilities. The main functions and features include: High availability (HA) primary/secondary switchover, strong data consistency, read/write separation, one-click installation and deployment, multi-dimensional indicator monitoring and alarm, flexible capacity expansion and reduction, horizontal free expansion, automatic backup and recovery, same-city multi-active, and remote Dr. RadonDB only requires enterprises and community users to focus on business logic development, and does not need to pay attention to complex issues such as cluster selection, management, operation and maintenance, so as to help enterprises and community users greatly improve the efficiency of business development and value innovation!

GitHub: github.com/radondb

Wechat group: please search and add group assistant wechat id RadonDB