欢迎光临
我们一直在努力

MySQL连接查询流程源码

来源: 互联网
版本: 不详, 仅做参考用

初始化:

点击(此处)折叠或打开

  1. main
  2.     |mysqld
  3.         |my_init // 初始话线程变量,互斥量
  4.         |load_defaults // 获取配置
  5.         |init_common_variables // 初始化变量
  6.         |init_server_components // 初始化插件
  7.         | |plugin_init
  8.         | | |plugin_initialize
  9.         | |initialize_storage_engine
  10.         |network_init // 监听网络
  11.         |grant_init
  12.         |servers_init
  13.         |udf_init

插件启动:

点击(此处)折叠或打开

  1. main
  2.     |mysqld_main
  3.         |init_server_components
  4.             |plugin_init
  5.                 |plugin_initialize
  6.                     |ha_initialize_handlerton
  7.                         |innobase_init

登录过程:

点击(此处)折叠或打开

  1. main
  2.     |mysqld_main
  3.         |network_init // 建立socket监听,一个针对网络,一个针对unix域
  4.         |handle_connections_sockets
  5.             |poll
  6.             |mysql_socket_accept // 和客户端建立连接
  7.             |create_new_thread // 针对每个socket连接建立一个新的线程
  8.                 |create_thread_to_handle_connection
  9.                     |waiting_thd_list>push_back(thd);mysql_cond_signal(&COND_thread_cache); // 已有连接处理线程时,通过信号唤醒,处理线程函数为pfs_spawn_thread
  10.                     |mysql_thread_create(启动的线程执行函数,inline_mysql_thread_create)
  11.                         |spawn_thread_v1
  12.                             |pthread_create(pfs_spawn_thread)

处理连接:

点击(此处)折叠或打开

  1. pfs_spawn_thread
  2.     |handle_one_connection
  3.         |do_handle_one_connection
  4.             |MYSQL_CALLBACK_ELSE(thread_scheduler, init_new_connection_thread, (), 0)
  5.             | |init_new_connection_handler_thread
  6.             |thd_prepare_connection
  7.             | |login_connection // 判断是否可以login,不可以则断开连接返回错误
  8.             | | |check_connection
  9.             | | | |acl_authenticate
  10.             | | | |do_auth_once
  11.             | | | |native_password_authenticate
  12.             | | | |server_mpvio_write_packet
  13.             | | | | |send_server_handshake_packet // 发送handshake包到客户端
  14.             | | | | |my_net_write
  15.             | | | | | |net_write_buff // 将数据写入到内存
  16.             | | | | |net_flush // 将内存中数据发送到网络
  17.             | | | |server_mpvio_read_packet // 从客户端接收Login Request信息
  18.             | | | |my_net_read
  19.             | | |Protocol::end_statement
  20.             | | |Protocol::send_ok
  21.             | | |net_send_ok // 发送response ok
  22.             | | |my_net_write
  23.             | |prepare_new_connection_state
  24.             |do_command
  25.                 |dispatch_command
  26.                     |mysql_parse

select命令:

点击(此处)折叠或打开

  1. pfs_swpawn_thread
  2.     |handle_one_connection
  3.         |do_handle_one_connection
  4.             |do_command
  5.                 |dispatch_command
  6.                     |mysql_parse
  7.                         |parse_sql
  8.                         | |MYSQLparse
  9.                         |mysql_execute_command
  10.                             |select_precheck
  11.                             | |check_table_access
  12.                             |execute_sqlcom_select
  13.                             | |open_normal_and_derived_tables
  14.                             | |open_tables
  15.                             | | |open_and_process_table
  16.                             | | |open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx)
  17.                             | | |Table_cache::get_table
  18.                             | | |get_table_share_with_discover
  19.                             | | | |get_table_share
  20.                             | | | |open_table_def
  21.                             | | |my_malloc // 申请表数据结构
  22.                             | | |open_table_from_share
  23.                             | | |handler::ha_open
  24.                             | | |ha_innobase::open
  25.                             | | |dict_table_open_on_name
  26.                             | | |dict_load_table
  27.                             | | |btr_pcur_is_on_user_rec
  28.                             | | |dict_load_table_low
  29.                             | | | |dict_mem_table_create
  30.                             | | |fil_space_for_table_exists_in_mem
  31.                             | | |fil_open_single_table_tablespace // 打开表空间文件
  32.                             | |mysql_handle_derived
  33.                             |handle_select
  34.                                 |mysql_select
  35.                                     |mysql_prepare_select
  36.                                     | |JOIN::prepare
  37.                                     |mysql_execute_select
  38.                                         |JOIN::exec
  39.                                             |select_send::send_result_set_metadata
  40.                                             | |Protocol::send_result_set_metadata
  41.                                             |do_select
  42.                                                 |sub_select
  43.                                                     |evaluate_join_record
  44.                                                         |end_send
  45.                                                             |select_send::send_data
  46.                                                                 |Protocol::write

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。