Immersive compilation of TiDB in TiDB source code series

1. Background

I just finished learning PCTP recently, and have a rough understanding of various reading and writing of TiDB, but if you want to study more detailed things, you must learn the source code. If you want to learn the source code, you can refer to the official TiDB source code series articles. However, the official articles were all published in 18 years, and there may be some lag, so you can refer to this and learn from the source code. Official source code interpretation blog address However, learning the source code must be the most effective way to learn to compile and debug the source code. After a few days of study and consultation with the developers, TiDB has been successfully compiled, so I share it with friends who are interested in the source code.TiDB源码系列解读文章

2. Prepare tools

Goland白嫖款一个(30天) TiDB 最新款源码tar包一份 Mac一台

3. Source code download

1. First go to github to download the source code: source code download address 2. Select the version you want to see: Here I take the latest version 5.4.0 as an example. 3. Unzip the tar packageinsert image description here

#1.打开终端
#2.到对应目录解压tar包
tar -xzvf tidb-5.4.0.tar.gz
#解压后就会得到一个文件夹

4. Compile software download configuration

Since TiDB is written in the go language, we use it to compile. 1. Download a 30-day free version, if you have the ability, you can directly buy the paid version (support genuine) Download address: goland download address Choose the one that suits your computer system to download: 2. After the installation is complete, select the corresponding open project. 3. Find the TiDB source folder you just unzipped, and open it. 4. After opening, there may be a lot of errors (as shown in the figure below), but the problem is not big, basically because the related modules of go are not installed. The required gomodule can be seen in the following file in the code. It can be seen that it is currently all red, indicating that the corresponding mod is missing.golandinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description here

5. Open the settings of goland, then download the latest version of goroot, and the required modules will be installed. Then choose the latest version to download. Then it will start to download the relevant dependencies below: during the test, it is found that only part of it will be downloaded, so the remaining missing ones can be clicked on the image to sync from github. After the download is complete, it will all turn green, and the environment is now configured.insert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description here

5. Compilation process

After the above steps, basically all the environment is ready, then we can compile the TiDB database by ourselves. 1. But there will be another problem. Here I still need to use the official source code series I started talking about: How to learn TiDB source code . As a non-professional developer, I can only learn with the help of the official website. From the address above, you can see what the TiDB source file corresponds to and where to start. 2. From the above, we can know from the official blog that running from tidb-server/main.go First, the var in main.go is the startup parameter of our system, you can modify the corresponding parameters to start从哪开始运行?insert image description hereinsert image description here

Then find the green button next to the main function below, you can choose debug mode or run directly, here I choose debug mode, you can see more information. After clicking, you need to wait for a while, and compiling will be displayed at the bottom of the screen. After compiling, you can see the corresponding information in the debug column: You can see that the startup is successful. 3. Go to visit to see if it really runs successfully. You can see that the TiDB service is already running. Use the MySQL command line to access TiDB. You can see that it is OK, and the compilation is complete.insert image description here[INFO] [server.go:246] ["server is running MySQL protocol"] [addr=0.0.0.0:4000]insert image description hereinsert image description here

6. Finally

After the operation is successful, you can break points for different functions in the future, and then see which functions in the code will be called by your own SQL. The location of the function can refer to the official blog source code analysis for breakpoint testing.

 

Original author: @Jiawei  Published: 2022/2/24
Original link: https://tidb.io/blog/5be1e094

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/5674736/blog/5513097