1.总体描述与准备
通语过网络请求获取客户端的SQL 将SQL转换成LEX语法树 将语法树转换成具体执行 从存储空间加载到Page并找到记录
创建表格:
查询语句:select * from sbtest1 where id=1;
2. 连接认证
死循环,监听客户端请求并创建处理线程mysqld.cc
mysqld_main和network_int
Mysqld_socket_listener *mysqld_socket_listener 此类表示Mysqld_socket_listener,它准备侦听器套接字以接收来自客户端的连接事件。Mysqld_socket_listener可以由通过Mysqld命令行侦听默认Mysqld tcp端口或用户指定端口的tcp套接字和绑定到Mysqld实际路径名的unix套接字中的一个或两个组成。
Connection_acceptor<Mysqld_socket_listener> mysqld_socket_acceptor 此类提供了一个通用接口,用于初始化和运行不同类型侦听器的连接事件循环,以及一个回调函数,用于从侦听连接的侦听器调用连接事件。侦听器类型应该是一个提供方法setup_Listener、listen_for_connection_event和close_Listener的类。Connection事件回调函数对象将在接收到来自客户端的连接事件时处理连接。
处理连接事件的函数中,在线程初始化后会调用connection_event_loop( )。
connection_event_loop():connection_acceptor.h中 Connection acceptor loop to accept connections from clients.(死循环)
Template class Connection_acceptor: 此类提供了一个通用接口,用于初始化和运行不同类型侦听器的连接事件循环,以及一个回调函数,用于从侦听连接的侦听器调用连接事件。侦听器类型应该是一个提供方法setup_Listener、listen_for_connection_event和close_Listener的类。Connection事件回调函数对象将在接收到来自客户端的连接事件时处理连接。
Channel_info channel_info channel_info.h 文件中:这个抽象基类表示有关新连接的连接通道信息。它的子类封装了不同连接通道类型之间的差异。
Connection_handler_manager::process_new_connection(Channel_info channel_info) 这是一个提供各种连接管理相关功能的单例类,最重要的是将新连接调度到当前激活的connection_handler
Connection handler:This abstract base class represents how connections are processed, most importantly how they map to OS threads.
bool Per_thread_connection_handler::add_connection(Channel_info channel_info) 此类表示连接处理功能,即每个连接都是有一个独立线程处理的。
函数具体处理逻辑:线程缓存,有空闲就拿过来,没有就再创建一个加进去
handle_connection()函数:连接线程的管理
初始化thd时三种特殊情况的操作:thd=NULL、HAVE_PSI_THREAD_INTERFACE时,是否resued,要复用和不复用两种子情况
设置mysql_thread_set_psi的id和THD,通过socker设置线程所有者,将thd添加到thd_manger
关键函数:thd_prepare_connection(thd) sql_connect.cc中
关键函数:lex_start(thd) 在sql_lex中 The function creates a select_lex and a select_lex_unit object. These objects should rather be created by the parser bottom-up.
关键函数:login_connection(thd) 在sql_connect中 Autenticate user, with error reporting
关键函数:error= check_connection(thd):在sql_connect中 Perform handshake, authorize client and update thd ACL variables.
关键调用函数:acl_check_host() 在sql_authentication中 确认host和ip是否匹配 Return true if there is no users that can match the given host
关键调用函数:vio_keepalive() 设置keepalive
关键调用函数:acl_authenticate(thd, COM_CONNECT) 密码认证
Perform the handshake, authorize the client and update thd sctx variables
关键函数:do_auth_once() 第一步认证尝试、向server发送handshake包
thd->send_statement_status() 在sql_class中 Send the status of the current statement execution over network
关键函数:prepare_new_connection_state(THD* thd) 在sql_connect中 Initialize THD to handle queries
关键调用函数: thd_manager->add_thd(thd) 在connection_handler_per_thread.cc中
void Global_THD_manager::add_thd(THD *thd) 在mysql_thd_manager.cc中 将该线程加入list中
关键调用函数:do_command(thd) 和end_connection(thd) 死循环接受用户请求、关闭已有连接
do_command(THD *thd) 在sql_parse中 Read one command from connection and execute it (query or simple command)
最后释放资源、关闭连接