博客迁移至:

https://www.dboop.com/

从事DBA工作多年

MYSQL源码也是头一次接触

尝试记录下自己看MYSQL5.7源码的历程

申明:个人Python编程很溜,但是C++还停在白痴水平,源码理解方面有点弱,如发现有错误的地方,轻喷

目录:

51ak带你看MYSQL5.7源码1:main入口函数 (2018-03-21)

51ak带你看MYSQL5.7源码2:编译现有的代码 (2018-03-22)

51ak带你看MYSQL5.7源码3:修改代码实现你的第一个Mysql版本 (2018-03-23)

51ak带你看MYSQL5.7源码4:实现SQL黑名单功能(2018-04-11)

去MYSQL官网下源码:https://dev.mysql.com/downloads/mysql/

选 SOURCE  CODE

下载解压。选VS code来查看,

用VS打开发现这些目录 ,根据了解到的目录结构说明,我们关注标红的两个目录,SQL是MYSQL的核心代码 ,STORGE里是各个存储引擎的代码

打开sql/main.cc 发现只有一个函数,引用了同级目录下的mysqld_main(int argc, char **argv);

F12跟过去,到了msyqld.cc下的这个过程 ,这里就是整个SERVER进程 的入口

接下来就一个巨大的代码段,来启动MYSQLD进程

我按个人的理解加了注释如下:

  1. #ifdef _WIN32
  2. int win_main(int argc, char **argv)
  3. #else
  4. int mysqld_main(int argc, char **argv)
  5. #endif
  6. {
  7. /*
  8. Perform basic thread library and malloc initialization,
  9. to be able to read defaults files and parse options.
  10. */
  11. my_progname= argv[0]; /*注: 记下mysql进程名*/
  12.  
  13. #ifndef _WIN32
  14. #ifdef WITH_PERFSCHEMA_STORAGE_ENGINE
  15. pre_initialize_performance_schema();
  16. #endif /*WITH_PERFSCHEMA_STORAGE_ENGINE */
  17. // For windows, my_init() is called from the win specific mysqld_main
  18. if (my_init()) // init my_sys library & pthreads
  19. {
  20. sql_print_error("my_init() failed.");
  21. flush_error_log_messages();
  22. return 1;
  23. }
  24. #endif /* _WIN32 */
  25.  
  26. orig_argc= argc;
  27. orig_argv= argv;
  28. my_getopt_use_args_separator= TRUE;
  29. my_defaults_read_login_file= FALSE;
  30. /*注: 这里是去读配置文件里的启动项,读的时候还带入了argv ,应该是argv优行,*/
  31. if (load_defaults(MYSQL_CONFIG_NAME, load_default_groups, &argc, &argv))
  32. {
  33. flush_error_log_messages();
  34. return 1;
  35. }
  36. my_getopt_use_args_separator= FALSE;
  37. defaults_argc= argc;
  38. defaults_argv= argv;
  39. remaining_argc= argc;
  40. remaining_argv= argv;
  41.  
  42. /* Must be initialized early for comparison of options name */
  43. system_charset_info= &my_charset_utf8_general_ci;
  44.  
  45. /* Write mysys error messages to the error log. */
  46. local_message_hook= error_log_print;
  47.  
  48. int ho_error;
  49.  
  50. #ifdef WITH_PERFSCHEMA_STORAGE_ENGINE
  51. /*
  52. Initialize the array of performance schema instrument configurations.
  53. */
  54. init_pfs_instrument_array();
  55. #endif /* WITH_PERFSCHEMA_STORAGE_ENGINE */
  56. /*注: 这里跟过去发现还是在处理启动变量*/
  57. ho_error= handle_early_options();
  58.  
  59. #if !defined(_WIN32) && !defined(EMBEDDED_LIBRARY)
  60.  
  61. if (opt_bootstrap && opt_daemonize)
  62. {
  63. fprintf(stderr, "Bootstrap and daemon options are incompatible.\n");
  64. exit(MYSQLD_ABORT_EXIT);
  65. }
  66.  
  67. if (opt_daemonize && log_error_dest == disabled_my_option &&
  68. (isatty(STDOUT_FILENO) || isatty(STDERR_FILENO)))
  69. {
  70. fprintf(stderr, "Please enable --log-error option or set appropriate "
  71. "redirections for standard output and/or standard error "
  72. "in daemon mode.\n");
  73. exit(MYSQLD_ABORT_EXIT);
  74. }
  75.  
  76. if (opt_daemonize)
  77. {
  78. if (chdir("/") < 0)
  79. {
  80. fprintf(stderr, "Cannot change to root director: %s\n",
  81. strerror(errno));
  82. exit(MYSQLD_ABORT_EXIT);
  83. }
  84.  
  85. if ((pipe_write_fd= mysqld::runtime::mysqld_daemonize()) < 0)
  86. {
  87. fprintf(stderr, "mysqld_daemonize failed \n");
  88. exit(MYSQLD_ABORT_EXIT);
  89. }
  90. }
  91. #endif
  92. /*注: 还是在处理启动变量。。。*/
  93. init_sql_statement_names();
  94. sys_var_init();
  95. ulong requested_open_files;
  96. adjust_related_options(&requested_open_files);
  97.  
  98. #ifdef WITH_PERFSCHEMA_STORAGE_ENGINE
  99. if (ho_error == 0)
  100. {
  101. if (!opt_help && !opt_bootstrap)
  102. {
  103. /* Add sizing hints from the server sizing parameters. */
  104. pfs_param.m_hints.m_table_definition_cache= table_def_size;
  105. pfs_param.m_hints.m_table_open_cache= table_cache_size;
  106. pfs_param.m_hints.m_max_connections= max_connections;
  107. pfs_param.m_hints.m_open_files_limit= requested_open_files;
  108. pfs_param.m_hints.m_max_prepared_stmt_count= max_prepared_stmt_count;
  109.  
  110. PSI_hook= initialize_performance_schema(&pfs_param);
  111. if (PSI_hook == NULL && pfs_param.m_enabled)
  112. {
  113. pfs_param.m_enabled= false;
  114. sql_print_warning("Performance schema disabled (reason: init failed).");
  115. }
  116. }
  117. }
  118. #else
  119. /*
  120. Other provider of the instrumentation interface should
  121. initialize PSI_hook here:
  122. - HAVE_PSI_INTERFACE is for the instrumentation interface
  123. - WITH_PERFSCHEMA_STORAGE_ENGINE is for one implementation
  124. of the interface,
  125. but there could be alternate implementations, which is why
  126. these two defines are kept separate.
  127. */
  128. #endif /* WITH_PERFSCHEMA_STORAGE_ENGINE */
  129.  
  130. #ifdef HAVE_PSI_INTERFACE
  131. /*
  132. Obtain the current performance schema instrumentation interface,
  133. if available.
  134. */
  135. if (PSI_hook)
  136. {
  137. PSI *psi_server= (PSI*) PSI_hook->get_interface(PSI_CURRENT_VERSION);
  138. if (likely(psi_server != NULL))
  139. {
  140. set_psi_server(psi_server);
  141.  
  142. /*
  143. Now that we have parsed the command line arguments, and have initialized
  144. the performance schema itself, the next step is to register all the
  145. server instruments.
  146. */
  147. init_server_psi_keys();
  148. /* Instrument the main thread */
  149. PSI_thread *psi= PSI_THREAD_CALL(new_thread)(key_thread_main, NULL, 0);
  150. PSI_THREAD_CALL(set_thread_os_id)(psi);
  151. PSI_THREAD_CALL(set_thread)(psi);
  152.  
  153. /*
  154. Now that some instrumentation is in place,
  155. recreate objects which were initialised early,
  156. so that they are instrumented as well.
  157. */
  158. my_thread_global_reinit();
  159. }
  160. }
  161. #endif /* HAVE_PSI_INTERFACE */
  162. /*注: ERRLOG初始化*/
  163. init_error_log();
  164.  
  165. /* Initialize audit interface globals. Audit plugins are inited later. */
  166. mysql_audit_initialize();
  167.  
  168. #ifndef EMBEDDED_LIBRARY
  169. Srv_session::module_init();
  170. #endif
  171.  
  172. /*
  173. Perform basic query log initialization. Should be called after
  174. MY_INIT, as it initializes mutexes.
  175. */
  176. /*注: QUERYLOG初始化*/
  177. query_logger.init();
  178.  
  179. if (ho_error)
  180. {
  181. /*
  182. Parsing command line option failed,
  183. Since we don't have a workable remaining_argc/remaining_argv
  184. to continue the server initialization, this is as far as this
  185. code can go.
  186. This is the best effort to log meaningful messages:
  187. - messages will be printed to stderr, which is not redirected yet,
  188. - messages will be printed in the NT event log, for windows.
  189. */
  190. flush_error_log_messages();
  191. /*
  192. Not enough initializations for unireg_abort()
  193. Using exit() for windows.
  194. */
  195. exit (MYSQLD_ABORT_EXIT);
  196. }
  197.  
  198. if (init_common_variables())
  199. unireg_abort(MYSQLD_ABORT_EXIT); // Will do exit
  200. /*注: 系统信号初始化*/
  201. my_init_signals();
  202.  
  203. size_t guardize= 0;
  204. #ifndef _WIN32
  205. int retval= pthread_attr_getguardsize(&connection_attrib, &guardize);
  206. DBUG_ASSERT(retval == 0);
  207. if (retval != 0)
  208. guardize= my_thread_stack_size;
  209. #endif
  210.  
  211. #if defined(__ia64__) || defined(__ia64)
  212. /*
  213. Peculiar things with ia64 platforms - it seems we only have half the
  214. stack size in reality, so we have to double it here
  215. */
  216. guardize= my_thread_stack_size;
  217. #endif
  218.  
  219. my_thread_attr_setstacksize(&connection_attrib,
  220. my_thread_stack_size + guardize);
  221.  
  222. {
  223. /* Retrieve used stack size; Needed for checking stack overflows */
  224. size_t stack_size= 0;
  225. my_thread_attr_getstacksize(&connection_attrib, &stack_size);
  226.  
  227. /* We must check if stack_size = 0 as Solaris 2.9 can return 0 here */
  228. if (stack_size && stack_size < (my_thread_stack_size + guardize))
  229. {
  230. sql_print_warning("Asked for %lu thread stack, but got %ld",
  231. my_thread_stack_size + guardize, (long) stack_size);
  232. #if defined(__ia64__) || defined(__ia64)
  233. my_thread_stack_size= stack_size / 2;
  234. #else
  235. my_thread_stack_size= static_cast<ulong>(stack_size - guardize);
  236. #endif
  237. }
  238. }
  239.  
  240. #ifndef DBUG_OFF
  241. test_lc_time_sz();
  242. srand(static_cast<uint>(time(NULL)));
  243. #endif
  244.  
  245. #ifndef _WIN32
  246. if ((user_info= check_user(mysqld_user)))
  247. {
  248. #if HAVE_CHOWN
  249. if (unlikely(opt_initialize))
  250. {
  251. /* need to change the owner of the freshly created data directory */
  252. MY_STAT stat;
  253. char errbuf[MYSYS_STRERROR_SIZE];
  254. bool must_chown= true;
  255.  
  256. /* fetch the directory's owner */
  257. if (!my_stat(mysql_real_data_home, &stat, MYF(0)))
  258. {
  259. sql_print_information("Can't read data directory's stats (%d): %s."
  260. "Assuming that it's not owned by the same user/group",
  261. my_errno(),
  262. my_strerror(errbuf, sizeof(errbuf), my_errno()));
  263. }
  264. /* Don't change it if it's already the same as SElinux stops this */
  265. else if(stat.st_uid == user_info->pw_uid &&
  266. stat.st_gid == user_info->pw_gid)
  267. must_chown= false;
  268.  
  269. if (must_chown &&
  270. chown(mysql_real_data_home, user_info->pw_uid, user_info->pw_gid)
  271. )
  272. {
  273. sql_print_error("Can't change data directory owner to %s", mysqld_user);
  274. unireg_abort(1);
  275. }
  276. }
  277. #endif
  278.  
  279. #if defined(HAVE_MLOCKALL) && defined(MCL_CURRENT)
  280. if (locked_in_memory) // getuid() == 0 here
  281. set_effective_user(user_info);
  282. else
  283. #endif
  284. set_user(mysqld_user, user_info);
  285. }
  286. #endif // !_WIN32
  287.  
  288. /*
  289. initiate key migration if any one of the migration specific
  290. options are provided.
  291. */
  292. /*注: 这一段应该是跟迁移有关的,不是很懂*/
  293. if (opt_keyring_migration_source ||
  294. opt_keyring_migration_destination ||
  295. migrate_connect_options)
  296. {
  297. Migrate_keyring mk;
  298. if (mk.init(remaining_argc, remaining_argv,
  299. opt_keyring_migration_source,
  300. opt_keyring_migration_destination,
  301. opt_keyring_migration_user,
  302. opt_keyring_migration_host,
  303. opt_keyring_migration_password,
  304. opt_keyring_migration_socket,
  305. opt_keyring_migration_port))
  306. {
  307. sql_print_error(ER_DEFAULT(ER_KEYRING_MIGRATION_STATUS),
  308. "failed");
  309. log_error_dest= "stderr";
  310. flush_error_log_messages();
  311. unireg_abort(MYSQLD_ABORT_EXIT);
  312. }
  313.  
  314. if (mk.execute())
  315. {
  316. sql_print_error(ER_DEFAULT(ER_KEYRING_MIGRATION_STATUS),
  317. "failed");
  318. log_error_dest= "stderr";
  319. flush_error_log_messages();
  320. unireg_abort(MYSQLD_ABORT_EXIT);
  321. }
  322.  
  323. sql_print_information(ER_DEFAULT(ER_KEYRING_MIGRATION_STATUS),
  324. "successfull");
  325. log_error_dest= "stderr";
  326. flush_error_log_messages();
  327. unireg_abort(MYSQLD_SUCCESS_EXIT);
  328. }
  329.  
  330. /*
  331. We have enough space for fiddling with the argv, continue
  332. */
  333. /*注:设置DATA变量*/
  334. if (my_setwd(mysql_real_data_home,MYF(MY_WME)) && !opt_help)
  335. {
  336. sql_print_error("failed to set datadir to %s", mysql_real_data_home);
  337. unireg_abort(MYSQLD_ABORT_EXIT); /* purecov: inspected */
  338. }
  339. /*注:设置BINLOG*/
  340. //If the binlog is enabled, one needs to provide a server-id
  341. if (opt_bin_log && !(server_id_supplied) )
  342. {
  343. sql_print_error("You have enabled the binary log, but you haven't provided "
  344. "the mandatory server-id. Please refer to the proper "
  345. "server start-up parameters documentation");
  346. unireg_abort(MYSQLD_ABORT_EXIT);
  347. }
  348.  
  349. /*
  350. The subsequent calls may take a long time : e.g. innodb log read.
  351. Thus set the long running service control manager timeout
  352. */
  353. #if defined(_WIN32)
  354. Service.SetSlowStarting(slow_start_timeout);
  355. #endif
  356. /*注:这个很重要。核心模块在这里启动了*/
  357. if (init_server_components())
  358. unireg_abort(MYSQLD_ABORT_EXIT);
  359.  
  360. /*
  361. Each server should have one UUID. We will create it automatically, if it
  362. does not exist.
  363. */
  364. if (init_server_auto_options())
  365. {
  366. sql_print_error("Initialization of the server's UUID failed because it could"
  367. " not be read from the auto.cnf file. If this is a new"
  368. " server, the initialization failed because it was not"
  369. " possible to generate a new UUID.");
  370. unireg_abort(MYSQLD_ABORT_EXIT);
  371. }
  372.  
  373. /*注:下面这段跟SID有关*/
  374. /*
  375. Add server_uuid to the sid_map. This must be done after
  376. server_uuid has been initialized in init_server_auto_options and
  377. after the binary log (and sid_map file) has been initialized in
  378. init_server_components().
  379.  
  380. No error message is needed: init_sid_map() prints a message.
  381.  
  382. Strictly speaking, this is not currently needed when
  383. opt_bin_log==0, since the variables that gtid_state->init
  384. initializes are not currently used in that case. But we call it
  385. regardless to avoid possible future bugs if gtid_state ever
  386. needs to do anything else.
  387. */
  388. global_sid_lock->wrlock();
  389. int gtid_ret= gtid_state->init();
  390. global_sid_lock->unlock();
  391.  
  392. if (gtid_ret)
  393. unireg_abort(MYSQLD_ABORT_EXIT);
  394.  
  395. // Initialize executed_gtids from mysql.gtid_executed table.
  396. if (gtid_state->read_gtid_executed_from_table() == -1)
  397. unireg_abort(1);
  398.  
  399. if (opt_bin_log)
  400. {
  401. /*
  402. Initialize GLOBAL.GTID_EXECUTED and GLOBAL.GTID_PURGED from
  403. gtid_executed table and binlog files during server startup.
  404. */
  405. Gtid_set *executed_gtids=
  406. const_cast<Gtid_set *>(gtid_state->get_executed_gtids());
  407. Gtid_set *lost_gtids=
  408. const_cast<Gtid_set *>(gtid_state->get_lost_gtids());
  409. Gtid_set *gtids_only_in_table=
  410. const_cast<Gtid_set *>(gtid_state->get_gtids_only_in_table());
  411. Gtid_set *previous_gtids_logged=
  412. const_cast<Gtid_set *>(gtid_state->get_previous_gtids_logged());
  413.  
  414. Gtid_set purged_gtids_from_binlog(global_sid_map, global_sid_lock);
  415. Gtid_set gtids_in_binlog(global_sid_map, global_sid_lock);
  416. Gtid_set gtids_in_binlog_not_in_table(global_sid_map, global_sid_lock);
  417.  
  418. if (mysql_bin_log.init_gtid_sets(&gtids_in_binlog,
  419. &purged_gtids_from_binlog,
  420. opt_master_verify_checksum,
  421. true/*true=need lock*/,
  422. NULL/*trx_parser*/,
  423. NULL/*gtid_partial_trx*/,
  424. true/*is_server_starting*/))
  425. unireg_abort(MYSQLD_ABORT_EXIT);
  426.  
  427. global_sid_lock->wrlock();
  428.  
  429. purged_gtids_from_binlog.dbug_print("purged_gtids_from_binlog");
  430. gtids_in_binlog.dbug_print("gtids_in_binlog");
  431.  
  432. if (!gtids_in_binlog.is_empty() &&
  433. !gtids_in_binlog.is_subset(executed_gtids))
  434. {
  435. gtids_in_binlog_not_in_table.add_gtid_set(&gtids_in_binlog);
  436. if (!executed_gtids->is_empty())
  437. gtids_in_binlog_not_in_table.remove_gtid_set(executed_gtids);
  438. /*
  439. Save unsaved GTIDs into gtid_executed table, in the following
  440. four cases:
  441. 1. the upgrade case.
  442. 2. the case that a slave is provisioned from a backup of
  443. the master and the slave is cleaned by RESET MASTER
  444. and RESET SLAVE before this.
  445. 3. the case that no binlog rotation happened from the
  446. last RESET MASTER on the server before it crashes.
  447. 4. The set of GTIDs of the last binlog is not saved into the
  448. gtid_executed table if server crashes, so we save it into
  449. gtid_executed table and executed_gtids during recovery
  450. from the crash.
  451. */
  452. if (gtid_state->save(&gtids_in_binlog_not_in_table) == -1)
  453. {
  454. global_sid_lock->unlock();
  455. unireg_abort(MYSQLD_ABORT_EXIT);
  456. }
  457. executed_gtids->add_gtid_set(&gtids_in_binlog_not_in_table);
  458. }
  459.  
  460. /* gtids_only_in_table= executed_gtids - gtids_in_binlog */
  461. if (gtids_only_in_table->add_gtid_set(executed_gtids) !=
  462. RETURN_STATUS_OK)
  463. {
  464. global_sid_lock->unlock();
  465. unireg_abort(MYSQLD_ABORT_EXIT);
  466. }
  467. gtids_only_in_table->remove_gtid_set(&gtids_in_binlog);
  468. /*
  469. lost_gtids = executed_gtids -
  470. (gtids_in_binlog - purged_gtids_from_binlog)
  471. = gtids_only_in_table + purged_gtids_from_binlog;
  472. */
  473. DBUG_ASSERT(lost_gtids->is_empty());
  474. if (lost_gtids->add_gtid_set(gtids_only_in_table) != RETURN_STATUS_OK ||
  475. lost_gtids->add_gtid_set(&purged_gtids_from_binlog) !=
  476. RETURN_STATUS_OK)
  477. {
  478. global_sid_lock->unlock();
  479. unireg_abort(MYSQLD_ABORT_EXIT);
  480. }
  481.  
  482. /* Prepare previous_gtids_logged for next binlog */
  483. if (previous_gtids_logged->add_gtid_set(&gtids_in_binlog) !=
  484. RETURN_STATUS_OK)
  485. {
  486. global_sid_lock->unlock();
  487. unireg_abort(MYSQLD_ABORT_EXIT);
  488. }
  489.  
  490. /*
  491. Write the previous set of gtids at this point because during
  492. the creation of the binary log this is not done as we cannot
  493. move the init_gtid_sets() to a place before openning the binary
  494. log. This requires some investigation.
  495.  
  496. /Alfranio
  497. */
  498. Previous_gtids_log_event prev_gtids_ev(&gtids_in_binlog);
  499.  
  500. global_sid_lock->unlock();
  501.  
  502. (prev_gtids_ev.common_footer)->checksum_alg=
  503. static_cast<enum_binlog_checksum_alg>(binlog_checksum_options);
  504.  
  505. if (prev_gtids_ev.write(mysql_bin_log.get_log_file()))
  506. unireg_abort(MYSQLD_ABORT_EXIT);
  507. mysql_bin_log.add_bytes_written(
  508. prev_gtids_ev.common_header->data_written);
  509.  
  510. if (flush_io_cache(mysql_bin_log.get_log_file()) ||
  511. mysql_file_sync(mysql_bin_log.get_log_file()->file, MYF(MY_WME)))
  512. unireg_abort(MYSQLD_ABORT_EXIT);
  513. mysql_bin_log.update_binlog_end_pos();
  514.  
  515. (void) RUN_HOOK(server_state, after_engine_recovery, (NULL));
  516. }
  517.  
  518. /*注: 网络相关的初始化*/
  519. if (init_ssl())
  520. unireg_abort(MYSQLD_ABORT_EXIT);
  521. if (network_init())
  522. unireg_abort(MYSQLD_ABORT_EXIT);
  523.  
  524. #ifdef _WIN32
  525. #ifndef EMBEDDED_LIBRARY
  526. if (opt_require_secure_transport &&
  527. !opt_enable_shared_memory && !opt_use_ssl &&
  528. !opt_initialize && !opt_bootstrap)
  529. {
  530. sql_print_error("Server is started with --require-secure-transport=ON "
  531. "but no secure transports (SSL or Shared Memory) are "
  532. "configured.");
  533. unireg_abort(MYSQLD_ABORT_EXIT);
  534. }
  535. #endif
  536.  
  537. #endif
  538.  
  539. /*
  540. Initialize my_str_malloc(), my_str_realloc() and my_str_free()
  541. */
  542. my_str_malloc= &my_str_malloc_mysqld;
  543. my_str_free= &my_str_free_mysqld;
  544. my_str_realloc= &my_str_realloc_mysqld;
  545.  
  546. error_handler_hook= my_message_sql;
  547.  
  548. /* Save pid of this process in a file */
  549. if (!opt_bootstrap)
  550. create_pid_file();
  551.  
  552. /* Read the optimizer cost model configuration tables */
  553. if (!opt_bootstrap)
  554. reload_optimizer_cost_constants();
  555.  
  556. if (mysql_rm_tmp_tables() || acl_init(opt_noacl) ||
  557. my_tz_init((THD *)0, default_tz_name, opt_bootstrap) ||
  558. grant_init(opt_noacl))
  559. {
  560. abort_loop= true;
  561.  
  562. delete_pid_file(MYF(MY_WME));
  563.  
  564. unireg_abort(MYSQLD_ABORT_EXIT);
  565. }
  566.  
  567. if (!opt_bootstrap)
  568. servers_init(0);
  569.  
  570. if (!opt_noacl)
  571. {
  572. #ifdef HAVE_DLOPEN
  573. udf_init();
  574. #endif
  575. }
  576.  
  577. /*注:设置SHOW STATUS时的变量*/
  578. init_status_vars();
  579. /* If running with bootstrap, do not start replication. */
  580. if (opt_bootstrap)
  581. opt_skip_slave_start= 1;
  582.  
  583. /*注:初始化BINLOG的值了*/
  584. check_binlog_cache_size(NULL);
  585. check_binlog_stmt_cache_size(NULL);
  586.  
  587. binlog_unsafe_map_init();
  588.  
  589. /* If running with bootstrap, do not start replication. */
  590. if (!opt_bootstrap)
  591. {
  592. // Make @@slave_skip_errors show the nice human-readable value.
  593. set_slave_skip_errors(&opt_slave_skip_errors);
  594.  
  595. /*
  596. init_slave() must be called after the thread keys are created.
  597. */
  598. if (server_id != 0)
  599. init_slave(); /* Ignoring errors while configuring replication. */
  600. }
  601.  
  602. #ifdef WITH_PERFSCHEMA_STORAGE_ENGINE
  603. initialize_performance_schema_acl(opt_bootstrap);
  604. /*
  605. Do not check the structure of the performance schema tables
  606. during bootstrap:
  607. - the tables are not supposed to exist yet, bootstrap will create them
  608. - a check would print spurious error messages
  609. */
  610. if (! opt_bootstrap)
  611. check_performance_schema();
  612. #endif
  613.  
  614. initialize_information_schema_acl();
  615.  
  616. execute_ddl_log_recovery();
  617. (void) RUN_HOOK(server_state, after_recovery, (NULL));
  618.  
  619. if (Events::init(opt_noacl || opt_bootstrap))
  620. unireg_abort(MYSQLD_ABORT_EXIT);
  621.  
  622. #ifndef _WIN32
  623. // Start signal handler thread.
  624. start_signal_handler();
  625. #endif
  626.  
  627. /*注:启动了*/
  628. if (opt_bootstrap)
  629. {
  630. start_processing_signals();
  631.  
  632. int error= bootstrap(mysql_stdin);
  633. unireg_abort(error ? MYSQLD_ABORT_EXIT : MYSQLD_SUCCESS_EXIT);
  634. }
  635.  
  636. if (opt_init_file && *opt_init_file)
  637. {
  638. if (read_init_file(opt_init_file))
  639. unireg_abort(MYSQLD_ABORT_EXIT);
  640. }
  641.  
  642. /*
  643. Event must be invoked after error_handler_hook is assigned to
  644. my_message_sql, otherwise my_message will not cause the event to abort.
  645. */
  646. if (mysql_audit_notify(AUDIT_EVENT(MYSQL_AUDIT_SERVER_STARTUP_STARTUP),
  647. (const char **) argv, argc))
  648. unireg_abort(MYSQLD_ABORT_EXIT);
  649.  
  650. #ifdef _WIN32
  651. create_shutdown_thread();
  652. #endif
  653. start_handle_manager();
  654.  
  655. create_compress_gtid_table_thread();
  656.  
  657. sql_print_information(ER_DEFAULT(ER_STARTUP),
  658. my_progname,
  659. server_version,
  660. #ifdef HAVE_SYS_UN_H
  661. (opt_bootstrap ? (char*) "" : mysqld_unix_port),
  662. #else
  663. (char*) "",
  664. #endif
  665. mysqld_port,
  666. MYSQL_COMPILATION_COMMENT);
  667. #if defined(_WIN32)
  668. Service.SetRunning();
  669. #endif
  670.  
  671. start_processing_signals();
  672.  
  673. #ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
  674. /* engine specific hook, to be made generic */
  675. if (ndb_wait_setup_func && ndb_wait_setup_func(opt_ndb_wait_setup))
  676. {
  677. sql_print_warning("NDB : Tables not available after %lu seconds."
  678. " Consider increasing --ndb-wait-setup value",
  679. opt_ndb_wait_setup);
  680. }
  681. #endif
  682.  
  683. if (!opt_bootstrap)
  684. {
  685. /*
  686. Execute an I_S query to implicitly check for tables using the deprecated
  687. partition engine. No need to do this during bootstrap. We ignore the
  688. return value from the query execution. Note that this must be done after
  689. NDB is initialized to avoid polluting the server with invalid table shares.
  690. */
  691. if (!opt_disable_partition_check)
  692. {
  693. sql_print_information(
  694. "Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' "
  695. "to get a list of tables using the deprecated partition "
  696. "engine.");
  697.  
  698. sql_print_information("Beginning of list of non-natively partitioned tables");
  699. (void) bootstrap_single_query(
  700. "SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES "
  701. "WHERE CREATE_OPTIONS LIKE '%partitioned%';");
  702. sql_print_information("End of list of non-natively partitioned tables");
  703. }
  704. }
  705.  
  706. /*
  707. Set opt_super_readonly here because if opt_super_readonly is set
  708. in get_option, it will create problem while setting up event scheduler.
  709. */
  710. set_super_read_only_post_init();
  711.  
  712. DBUG_PRINT("info", ("Block, listening for incoming connections"));
  713.  
  714. (void)MYSQL_SET_STAGE(0 ,__FILE__, __LINE__);
  715.  
  716. server_operational_state= SERVER_OPERATING;
  717.  
  718. (void) RUN_HOOK(server_state, before_handle_connection, (NULL));
  719.  
  720. /*注:设置连接池*/
  721. #if defined(_WIN32)
  722. setup_conn_event_handler_threads();
  723. #else
  724. mysql_mutex_lock(&LOCK_socket_listener_active);
  725. // Make it possible for the signal handler to kill the listener.
  726. socket_listener_active= true;
  727. mysql_mutex_unlock(&LOCK_socket_listener_active);
  728.  
  729. if (opt_daemonize)
  730. mysqld::runtime::signal_parent(pipe_write_fd,1);
  731.  
  732. mysqld_socket_acceptor->connection_event_loop();
  733. #endif /* _WIN32 */
  734. server_operational_state= SERVER_SHUTTING_DOWN;
  735.  
  736. DBUG_PRINT("info", ("No longer listening for incoming connections"));
  737.  
  738. mysql_audit_notify(MYSQL_AUDIT_SERVER_SHUTDOWN_SHUTDOWN,
  739. MYSQL_AUDIT_SERVER_SHUTDOWN_REASON_SHUTDOWN,
  740. MYSQLD_SUCCESS_EXIT);
  741.  
  742. terminate_compress_gtid_table_thread();
  743. /*
  744. Save set of GTIDs of the last binlog into gtid_executed table
  745. on server shutdown.
  746. */
  747. if (opt_bin_log)
  748. if (gtid_state->save_gtids_of_last_binlog_into_table(false))
  749. sql_print_warning("Failed to save the set of Global Transaction "
  750. "Identifiers of the last binary log into the "
  751. "mysql.gtid_executed table while the server was "
  752. "shutting down. The next server restart will make "
  753. "another attempt to save Global Transaction "
  754. "Identifiers into the table.");
  755.  
  756. #ifndef _WIN32
  757. mysql_mutex_lock(&LOCK_socket_listener_active);
  758. // Notify the signal handler that we have stopped listening for connections.
  759. socket_listener_active= false;
  760. mysql_cond_broadcast(&COND_socket_listener_active);
  761. mysql_mutex_unlock(&LOCK_socket_listener_active);
  762. #endif // !_WIN32
  763.  
  764. #ifdef HAVE_PSI_THREAD_INTERFACE
  765. /*
  766. Disable the main thread instrumentation,
  767. to avoid recording events during the shutdown.
  768. */
  769. PSI_THREAD_CALL(delete_current_thread)();
  770. #endif
  771.  
  772. DBUG_PRINT("info", ("Waiting for shutdown proceed"));
  773. int ret= 0;
  774. #ifdef _WIN32
  775. if (shutdown_thr_handle.handle)
  776. ret= my_thread_join(&shutdown_thr_handle, NULL);
  777. shutdown_thr_handle.handle= NULL;
  778. if (0 != ret)
  779. sql_print_warning("Could not join shutdown thread. error:%d", ret);
  780. #else
  781. if (signal_thread_id.thread != 0)
  782. ret= my_thread_join(&signal_thread_id, NULL);
  783. signal_thread_id.thread= 0;
  784. if (0 != ret)
  785. sql_print_warning("Could not join signal_thread. error:%d", ret);
  786. #endif
  787. /*注:请理退出*/
  788. clean_up(1);
  789. mysqld_exit(MYSQLD_SUCCESS_EXIT);
  790. }

  

好了,第一天,只看这一节代码就够了

51ak带你看MYSQL5.7源码1:main入口函数的更多相关文章

  1. 51ak带你看MYSQL5.7源码3:修改代码实现你的第一个Mysql版本

    从事DBA工作多年 MYSQL源码也是头一次接触 尝试记录下自己看MYSQL5.7源码的历程 目录: 51ak带你看MYSQL5.7源码1:main入口函数 51ak带你看MYSQL5.7源码2:编译 ...

  2. 51ak带你看MYSQL5.7源码2:编译现有的代码

    从事DBA工作多年 MYSQL源码也是头一次接触 尝试记录下自己看MYSQL5.7源码的历程 目录: 51ak带你看MYSQL5.7源码1:main入口函数 51ak带你看MYSQL5.7源码2:编译 ...

  3. 51ak带你看MYSQL5.7源码4:实现SQL黑名单功能

    博客迁移至: https://www.dboop.com/ 从事DBA工作多年 MYSQL源码也是头一次接触 尝试记录下自己看MYSQL5.7源码的历程 申明:个人Python编程很溜,但是C++还停 ...

  4. twemproxy源码分析1——入口函数及启动过程

    最近工作中需要写一个一致性哈希的代理,在网上找到了twemproxy,结合网上资料先学习一下源码. 一.Twemproxy简介 Twemproxy是memcache与redis的代理,由twitter ...

  5. Chrome自带恐龙小游戏的源码研究(七)

    在上一篇<Chrome自带恐龙小游戏的源码研究(六)>中研究了恐龙的跳跃过程,这一篇研究恐龙与障碍物之间的碰撞检测. 碰撞盒子 游戏中采用的是矩形(非旋转矩形)碰撞.这类碰撞优点是计算比较 ...

  6. Chrome自带恐龙小游戏的源码研究(完)

    在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每 ...

  7. Chrome自带恐龙小游戏的源码研究(六)

    在上一篇<Chrome自带恐龙小游戏的源码研究(五)>中实现了眨眼睛的恐龙,这一篇主要研究恐龙的跳跃. 恐龙的跳跃 游戏通过敲击键盘的Spacebar或者Up来实现恐龙的跳跃.先用一张图来 ...

  8. Chrome自带恐龙小游戏的源码研究(四)

    在上一篇<Chrome自带恐龙小游戏的源码研究(三)>中实现了让游戏昼夜交替,这一篇主要研究如何绘制障碍物. 障碍物有两种:仙人掌和翼龙.仙人掌有大小两种类型,可以同时并列多个:翼龙按高. ...

  9. Chrome自带恐龙小游戏的源码研究(一)

    目录 Chrome自带恐龙小游戏的源码研究(一)——绘制地面 Chrome自带恐龙小游戏的源码研究(二)——绘制云朵 Chrome自带恐龙小游戏的源码研究(三)——昼夜交替 Chrome自带恐龙小游戏 ...

随机推荐

  1. 关于Spring事务的原理,以及在事务内开启线程,连接池耗尽问题.

    主要以结果为导向解释Spring 事务原理,连接池的消耗,以及事务内开启事务线程要注意的问题. Spring 事务原理这里不多说,网上一搜一大堆,也就是基于AOP配合ThreadLocal实现. 这里 ...

  2. 电脑开机后,就会自动运行chkdsk,我想取消chkdsk,怎么取消

     每次开机都自动检查磁盘,检测通过后下次还是一样,NTFS/FAT32分区都有可能有这样的情况,即使重装系统,仍可能出现同样情况,但是硬盘可以通过Dell 随机带的检测程序解决方法:在命令行窗口中 ...

  3. 【mongodb系统学习之一】mongodb的简单安装

    linux中mongodb的安装(最简单的): 1.下载mongodb安装包,这里用mongodb-linux-x86_64-2.6.9.gz 提供一个下载地址:http://pan.baidu.co ...

  4. 超链接a标签的属性target的可选值有哪些以及区别

    超链接a标签的属性target的可选值有哪些以及区别 1.<a target="_blank"></a> 2.<a target="_par ...

  5. raid功能中spanning和striping模式有什么区别?

    RAID 0 又称为Stripe(条带化,串列)或Striping 它代表了所有RAID级别中最高的存储性能.RAID 0提高存储性能的原理是把连续的数据分散到多个磁盘上存取,这样,系统有数据请求就可 ...

  6. jQuery遍历table中的tr td并获取td中的值

    jQuery遍历table中的tr td并获取td中的值 $(function(){ $("#tableId tr").find("td").each(func ...

  7. LAMP应用部署

    LAMP+wordpress 部署博客 软件安装 yum -y install httpd yum -y install php yum -y install php-mysql yum -y ins ...

  8. SpringMVC工作流程描述

    向服务器发送HTTP请求,请求被前端控制器 DispatcherServlet 捕获. DispatcherServlet 根据 <servlet-name>-servlet.xml 中的 ...

  9. The Moving Points HDU - 4717

    There are N points in total. Every point moves in certain direction and certain speed. We want to kn ...

  10. Linux之权限管理

    一.文件基本权限 1) 基本权限的修改 第一位"-"为文件类型(-代表文件:d代表目录:l代表软链接文件即快捷方式),后面每3位一组. -rw-r--r-- rw-   u所有者 ...