MySQL source code analysis series (1): Compilation and debugging-reprint

Source: Internet
Author: User
Tags mysql code
Document directory
  • Related Resources:
  • Download Code:
  • Compile the Code:
  • Installation and running:
  • Debugging:
For a long time, start from Mysqld source code analysis. As I continue reading, I will write some articles to record my own experiences, but it is not guaranteed to be written. In fact, it is not guaranteed that you have time to fully read all the code. The purpose of reading the code is as follows:
  • By understanding the Mysql architecture and the architecture of large-scale system software, I hope I can apply some of these technologies at work.
  • By reading the code, you can better understand the use of Mysql and make more in-depth applications, such as performance tuning, adding required functions and modules, etc.

As for writing articles, it is still a matter of time and effort. Although writing down your ideas will help you better master what you have learned, try your best, and my writing is poor.

This is the first article, starting from compiling and debugging MySQL, not in a rush to enter the code reading stage.

Related Resources:
  • Http://www.mysql.com/

Download the source code and documentation from this place.

  • Http://forge.mysql.com/wiki/MySQL_Internals

Here are a series of articles about the internal principles of MySQL. It is a good reference resource for understanding the code. We strongly recommend that you carefully read these articles. In addition to the Code itself, this is also the most authoritative information.

  • Http://www.amazon.com/Understanding-MySQL-Internals-Sasha-Pachev/dp/0596009577

This seems to be the only book I know about how MySQL works. It's not very thick either. Google this book has been downloaded in many places, but it is only e-paper. Let's take a look at it. Who told me it was made by foreigners.

Download Code:
  • Http://dev.mysql.com/downloads/

You need to register and download the latest version 5.1 or later.

  • Download from Bazaar.

Bazaar is a source code management tool used by MySQL. It is similar to CVS and has never been used. If you are interested, read the document:

  • A Contributor's Guide to Launchpad-Part 1-Getting Started
  • A Contributor's Guide to Launchpad-Part 2-Code Management
Compile code: Mysql code is compiled using shell scripts. After downloading and decompressing the code, there is BuildContents, go in and have a look. There are compilation scripts used by various platforms. We need the debug version to make it easier for us to use gdb, therefore, the script used is BUILD/compile-pentium-debug. It is actually very easy to open. It mainly adds the configure option "-- with-debug = full" and uses debug_cflags and debug_configs. The script content is as follows:

Build/compile-Pentium-Debug
<PRE>
#! /Bin/sh

Path = 'dirname $ 0' # Find the directory where the file is located
Set -- "$ @" -- with-DEBUG = full # Add "-- with-DEBUG = Full" to the command line"
. "$ Path/setup. Sh" # import setup. SH Content and set some variables

Extra_flags = "$ pentium_cflags $ debug_cflags"
Extra_configs = "$ pentium_configs $ debug_configs"

. "$ Path/finish. Sh" # configure and compile
</PRE>

"-- With-Debug" is actually defined in the Autoconf script. OpenConfigure. inTo find "with-Debug", you can see what are the differences between the parameters.

Run the Script: $BUILD/Compile-pentium-debug -- prefix = $ HOME/mysql-bin
Here, the target installation directory is $ home/MySQL-bin. You can specify other directories and try not to use the default directory. Otherwise, it may conflict with the installed MySQL file in the system.
According to the machine speed, the time spent is also different. It took me less than 10 minutes to use the machine. After compilation is completedSQLDirectory.SQL/mysqldCongratulations, :-) this is the MySQL server execution file. Here, I really want to write something about the MySQL compilation system.

Installation and running:

Installation: $Make & make install
After a lot of information, you should be able ~ /Many files are found under mysql-bin.
$Ls/home/liuli/mysql-bin/
Bin include info lib libexec man mysql-test share SQL-Example
Important directories:
* Bin: client programs and scripts. You can find familiar mysql, mysqld_safe, mysqldump, and mysqladmin.
* Libexec: server program, including mysqld
* Share: configuration file, including some instance configuration files such as my-small.cnf and initial database files required for initial installation and running, such as mysql_system_tables. SQL.

Mysql still needs some configuration files and data files to run. Execute this command:

$ ~ /Mysql-bin/mysql_install_db -- defaults-file = my. cnf -- basedir = ~ /Mysql-bin -- datadir = ~ /Mysql-bin/varThis command runs the bootstrap Process of Mysqld, which is mainly used to install the Mysql system table. For details, refer ~ /Mysql-bin/share/mysql directory fill_help_tables. SQL, mysql_system_tables. SQL, and mysql_system_tables_data. SQL. Here I particularly emphasize the "-- defaults-file" parameter, which is used to specify the "my. cnf" configuration file required for system operation. Mysql_install_db needs to know the system configuration before importing the preceding three SQL files. Therefore, you must create a configuration file ~ /Mysql-bin/share/mysql below the "my-small.cnf" as a configuration file, you can also choose their own corresponding configuration file, if not specified, will be from/etc/mysqld/my. cnf import configuration. Note:-defaults-file = my. the cnf option must be placed at the location of the first parameter. Otherwise, it is invalid. Most mysql programs or scripts accept this parameter, but it must also be placed at the location of the first parameter.
Well, if no error occurs, we can run mysqld! $ ~ /Mysql-bin/libexec/mysqld -- defaults-file = my. cnf -- basedir = ~ /Mysql-bin -- datadir = ~ /Mysql-bin/var -- skip-networking
PS: You can see this process. You can use MySQL to practice: $ mysql-S/tmp/MySQL. Sock $ show databases;
We can see that: + ------------------ + | database | + -------------------- + | information_schema | test | + -------------------- + 2 rows in SET (0.00 Sec)

The installation and operation department ends here! Next we will go to the debugging stage.
Debugging: Code debugging:

Of course, you can use gdb or DDD. Let's look at your preferences. Here we use gdb as an example.
$ Gdb -- args ~ /Mysql-bin/libexec/mysqld -- defaults-file = my. cnf -- basedir =/home/aaa/mysql-bin -- datadir =/home/aaa/mysql-bin/var -- skip-networking $ (gdb) br handle_one_connection $ chandle_one_connection as the name suggests, is the processing function after mysqld receives a connection request. In addition, both basedir and datadir use full paths. Otherwise, the error "Can't change dir to" ~ will be reported '~ /Mysql-bin/var /'
In another shell: $ mysql-S/tmp/mysql. sock Then gdb will show: <pre> Breakpoint 1, handle_one_connection (arg = 0x86ede38) at SQL _connect.cc: 10761076 THD * thd = (THD *) arg; (gdb) bt #0 handle_one_connection (arg = 0x86ede38) at SQL _connect.cc: 1076 #1 0x4fc133a8 in start_thread () from/lib/tls/i686/cmov/libpthread. so.0 #2 0x4fb647fe in clone () from/lib/tls/i686/cmov/libc. so.6

</PRE> the next step is simple. I will not talk about it any more if you know more about GDB.

If the name and file name of the function cannot be seen here, it must have been compiled without Debug. Re-compile it. Performance debugging: many times you are interested in the current MySQL performance. If you want to know which functions occupy a lot of CPU, I recommend two tools. You do not need to re-compile mysqld: * sysprof: http://www.daimi.au.dk /~ Sandmann/sysprof/* oprofile: http://oprofile.sourceforge.net/news/ sysprof is relatively easy to use, oprofile is more powerful, it is also a little complicated to use. They can be used to display the CPU usage of some programs running on the current system. The principle is to view the current CPU process and EIP through the timer of the system core state, you can also find the alternative stack by crawling the stack and print it out. They not only profile user-State processes, but also perform kernel profile, which is particularly powerful. It is strongly recommended for readers who need to debug performance of mysqld. It is also an indispensable tool for debugging performance of other programs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.