mysql 源码学习笔记:mysqld启动流程

版本
CentOS release 6.7环境下mysql-5.7.16 社区版

概述
主要从源码层面梳理mysql-5.7.16启动过程中调用函数的顺序和作用。主要函数和流程包括:
1. 初始化系统变量和系统状态
2. 初始化服务器的各个模块

源码分析
main                                                                      
  mysqld_main(argc, argv)                                    
    |  pre_initialize_performance_schema();          初始化performance shcema相关内容
    |  my_init()       
    |   load_defaults(MYSQL_CONFIG_NAME, load_default_groups, &argc, &argv)      读取配置文件             
    |  init_pfs_instrument_array                              初始化performance shcema相关内容
    |  handle_early_options                                     初始化部分变量,为mysqld初始化系统表等做装备
         | my_long_early_options变量设置,包括bootstrap、skip-grant-tables、help、verbose、version、initialize、initialize-insecure
    |  init_sql_statement_names                               初始化命令,为后续status统计操作数量做准备
    |  sys_var_init()                                                     初始化系统变量hash桶,将所有的系统变量插入到hash桶中(这里的变量为sys_vars.cc中定义的变量)
    |  init_error_log                                                     初始化error log 锁
    |  mysql_audit_initialize                                         初始化audit plugin锁,为后续初始化audit plugin做准备
    |  query_logger.init();                                              初始化general log和slow log的锁
    |   init_common_variables()                                 所有变量相关内容初始化,包括变量本身的值以及部分变量控制的特性
    |   init_server_components()                               服务器各个模块的初始化,包括
    |   init_server_auto_options()                                   初始化UUID
    |   if (gtid_state->read_gtid_executed_from_table() == -1)                      从gtid_execute表中读取gtid_execute
    |   if (opt_bin_log)
        | gtid_state->get_executed_gtids()/gtid_state->get_lost_gtids() ....      如果开启了binlog,则初始化gtid相关信息
        | 将gtid 信息更新到 mysql_bin_log中
        | 将表中记录的gtid信息与binlog同步(包括purge,crash recovery恢复等)
   | init_ssl()/network_init                                           初始化ssl和网络
   |  create_pid_file();                                                   创建pid文件
   | acl_init(opt_noacl)                                               初始化用户名、密码等信息缓存,并将user表中的内容读取缓存
   | grant_init(opt_noacl)                                            初始化权限缓存,将tables_priv等表中的权限信息读取缓存
   |  servers_init(0);                                                     初始化mysql.servers表,并读入缓存
   |  udf_init();                                                              初始化用户自定义函数
   |  init_status_vars();                                                初始化系统状态,system status排序
   | init_slave()                                                            初始化slave相关的结构体
                                                                                   如存在复制相关的信息,同时skip_slave_start未设置,则启动复制  
   | execute_ddl_log_recovery                                    执行ddl语句的crash recovery
   |  start_signal_handler();                                          创建信号处理线程,处理信号SIGTERM/SIGQUIT/SIGHUP
   | if (opt_bootstrap)  error= bootstrap(mysql_stdin);                           初始化数据目录、系统表
   | if (opt_init_file && *opt_init_file)   read_init_file(opt_init_file)        从文件中初始化数据目录、系统表
   | mysqld_socket_acceptor->connection_event_loop();                  监听端口和sock文件,等待客户端连接


 init_common_variables()                                  所有变量相关的初始化
         | init_thread_environment                                初始化全局mutex和condition
         | mysql_init_variables                                      设置部分全局变量的默认值
         | if (gethostname(glob_hostname,sizeof(glob_hostname)) < 0) ......                          生成pid文件
         | 设置默认存储引擎          
         if (add_status_vars(status_vars))                  将全局变量status_vars中的系统 status存入all_status_vars中
         | get_options                                                     将设置的系统变量的值更新到系统变量中
         | set_default_auth_plugin                               设置默认的身份验证插件(通常为native_password)
         | set_server_version                                      设置mysql版本的后缀(-embedded、-log、-debug...)
         | init_errmessage/init_client_errs                   初始化error message                    
         | item_create_init/item_init                              初始化所有的item类和函数(解析和后续执行使用)
         | 初始化 charset/collation
         | lex_init                                                           初始化解析sql使用的lex
         | 初始化general log 和 slow log的日志名


get_options      将文件中、命令行中设置的参数,设置到系统参数中
    |将my_long_options存入all_options
    |将all_sys_vars存入all_options                        所有的系统变量均已存入all_options中
    |将      {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}; 存入all_option 作为结束符
    |handle_options 
    | if (Connection_handler_manager::init())            初始化连接管理模块
    | if (Global_THD_manager::create_instance())   初始化线程管理模块



   init_server_components()
         | mdl_init                                                          初始化mdl锁
         | table_def_init/hostname_cache_init/my_timer_initialize
         | init_server_query_cache                               query cache初始化
         | randominit                                                        随机数初始化                                                  
         | init_slave_list                                                  初始化从机hash桶
         | transaction_cache_init()                                 事务缓存hash桶和锁初始化
         | delegates_init                                                  delegates用于执行回调函数
         |  if (opt_bin_log)                                                如果变量中开启了binlog 
            | 生成binlog_index 和 binlog的文件名
         |  if (opt_relay_logname)                                    如果变量中开启了relaylog
            | 生成relaylog_index 和 relay log的文件名
         |  if (ha_init_errors())                                         将handler模块的错误信息注册到mysqld的my_error中
         |  if (gtid_server_init())                                       初始化gitd结构体
         |  if (plugin_init(&remaining_argc, remaining_argv ...                初始化所有的插件(包括编译、配置、命令行加载的参数)
         |  query_logger.set_handlers(log_output_options);                   初始化general log/slow log的日志处理模块
         |  if (initialize_storage_engine(default_storage_engine, ""....         初始化默认的存储引擎
         |  if (tc_log->open(opt_bin_log ? opt_bin_logname : opt_tc_log_file))            tc_log打开binlog,主要用于binlog和存储引擎的recovery使用,打开当前index中最后一个文件
         |  if (ha_recover(0))                                                                          recovery
         |  if (mysql_bin_log.open_binlog(opt_bin_logname, 0,                     mysql_bin_log打开binlog,生成新的binlog,在index文件中加入新的内容
         |  mysql_bin_log.purge_logs_before_date(purge_time, true);         如果开启了复制,同时开启了binlog定期删除时间,删除过期的binlog
         | init_max_user_conn(void)                                           初始化连接hash桶
         |  init_update_queries();                                                  初始化server_command_flags结构体,用于命令统计
     
  
以上内容为源码层面各个函数的基本说明,为后续更深入的学习各个模块做最初的概括介绍,部分函数未列出,更详细的信息请查看源文件。


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值