代码分析基于最新的5.5.21
Mysql服务器的main()在/sql/main.cc,实际的入口点在/sql/mysqld.cc。
我们直接从mysqld_main()开始阅读。
先理解一些比较重要的宏定义:
(1)宏HAVE_NPTL:
这个宏如果打开了会去读一个系统变量LD_ASSUME_KERNEL,并把他赋给一个全局变量ld_assume_kernel_is_set,这个系统变量设置了系统线程的实现模型。linux内核在2.6版本后,缺省的线程模型是NPTL。
http://blog.csdn.net/theorytree/article/details/7336523 详细情况请阅读我博客里的这篇文章。
对于以HAVE_前缀的的宏,另有如下文章阐述:
http://blog.csdn.net/theorytree/article/details/7337692 这里介绍了CMake的相关知识,通过CMake在影子编译目录的include目录里面生成了my_config.h文件。
my_config.h文件通过my_global.h文件包含到具体的需要使用HAVE_前缀的宏的源文件中。
(2)EMBEDDED_LIBRARY
该宏用于控制 嵌入式库和客户端/服务器版本不同的那部分代码。
Mysql 可以编译为一个libmysqld库(嵌入式服务器),使用嵌入式MySQL服务器库,能够在客户端应用程序中使用具备全部特性的MySQL服务器。
主要优点在于,增加了速度,并使得嵌入式应用程序的管理更简单。嵌入式服务器库是以MySQL的客户端/服务器版本为基础的,采用C/C++语言编写。 其结果是嵌入式服务器也是用C/C++语言编写的。 在其他语言中,嵌入式服务器不可用。
这个宏可以通过CMake配置,
-DWITH_EMBEDDED_SERVER=1 打开这个宏在plug.cmake函数里有它的定义:
PROPERTIES COMPILE_DEFINITIONS "MYSQL_SERVER;EMBEDDED_LIBRARY")
整个代码的流程如下:我们讲分更细的文章详细介绍每部分:
Mysql服务器监听:
Mysql的总体结构图
Mysql 源代码的目录结构 :(以下来自官方文档)
/BUILD The compilation configuration and make files for all platforms supported.
Use this folder for compilation and linking.
/client The MySQL command-line client tool.
/dbug Utilities for use in debugging (see Chapter 5 for more details).
/Docs Documentation for the current release. Linux users should usegenerate-text-files.pl in the support subfolder to generate the documentation. Windows users are provided with a manual.chm file.
/include The base system include files and headers.
/libmysql The C client API used for creating embedded systems. (See Chapter 6 formore details.)
/libmysqld The core server API files. Also used in creating embedded systems.(See Chapter 6 for more details.)
/mysql-test The MySQL system test suite. (See Chapter 4 for more details.)
/mysys The majority of the core operating system API wrappers and helper functions.
/regex A regular expression library. Used in the query optimizer and execution to resolve expressions.
/scripts A set of shell script-based utilities.
/sql The main system code. You should start your exploration from this folder.
/sql-bench A set of benchmarking utilities.
/SSL A set of Secure Socket Layer utilities and definitions.
/storage The MySQL pluggable storage engine source code is located inside this folder. Also included is the storage engine example code. (See Chapter 7 for more details.)
/strings The core string handling wrappers. Use these for all of your string handling needs.
/support-files A set of preconfigured configuration files for compiling with different options.
/tests A set of test programs and test files.
/vio The network and socket layer code.
/zlib Data compression tools.