First look at the MySQL source code mysql.cc

Source: Internet
Author: User
Tags mysql in

In the MySQL source code directory, the SQL directory is one of the most frequently-changed directories in MySQL sources, and is also the core and important directory of the MySQL server kernel.

In addition to the file containing the MySQL main function of mysqld.cc, the SQL directory also includes parsing/implementation of various SQL statements.

A mysqld_main () is called in main.cc in the SQL directory, but there seems to be a win_main () and Mysqld_main () in MySQL,

At that time, I was very strange, just a corresponding to the Windows one corresponding to Linux AH. Thought for a long time, did not understand. Later looked again mysqld_main.cc, only then to see understand.

In fact, under Windows and the Linuxmysql server started the environment and the way is not the same, but MySQL in order to lazy only a set of source code, so release the project is convenient, and the Windows and Linux as long as a set of source code on the line, but read complex , a variety of conditional compilation commands dazzling, so you can not see the truth.

The process of MySQL starting services on Windows and Linux is a part of the code that is similar, depending on whether _WIN32 is defined to be named Win_main () or Mysqld_main (), and if it is named Win_main () Then we will have a mysqld_main () in the back to call this win_main (), and if it is Mysqld_main (), then the other Mysqld_main () will not need to be redefined.

#ifdef _win32//If _WIN32 is defined, it compiles win_main () int win_main (int argc, char **argv) #elseint mysqld_main (int argc, char **argv) If _WIN32 is not defined, it compiles mysqld_main () {//This is the same part of the code that is on Windows and Linux Server Startup//And of course there are many compilation preprocessing commands} #endif # if defined (_WIN32) int mysqld_main (int argc, char **argv) {//Here is Mysqld_main for Windows ();} #endif//_win32     This endif corresponds to 5021 rows of if defined (_WIN32)

It's just these few compile commands that decide whether to go to Windows or Linux, let's look at how Mysqld_main () calls Win_main (),

If you're looking for win_main () directly in Mysqld_main (), there's a function called mysql_service (void *p):

#if defined (_WIN32)//If you define _WIN32   int mysql_service (void *p)//actually call Win_main () {if  (My_thread_init ())  {    flush_error_log_messages ();    return 1;  }  if (Use_opt_args)    win_main (OPT_ARGC, OPT_ARGV);//Call to Win_main ()  else    Win_main (SERVICE.MY_ARGC, SERVICE.MY_ARGV);//For Win_main () call  my_thread_end ();  return 0;}

Haha, found the call to Win_main (), so be sure Mysqld_main () inside also called mysql_service (void *p):

Look for it, indeed.

if (Service.getos ())//True NT family *///meaning this is for NT {char File_path[fn_reflen];          My_path (File_path, argv[0], ""); /* Find name in Path */Fn_format (File_path,argv[0],file_path, "", My_replace_dir | My_unpack_filename |    My_resolve_symlinks);                                    if (argc = = 2) {if (!default_service_handling (argv, Mysql_servicename, Mysql_servicename, File_path, "", NULL)) return 0;if (Service.isservice (argv[1))/* Start an optional Service */{/*only add the Service name to the groups read from the config Fileif it's not "MySQL". (The default service name should is ' mysqld ' but we started a bad tradition by calling it MySQL from the Startand we is no W stuck with it.*/if (my_strcasecmp (System_charset_info, argv[1], "MySQL")) load_default_groups[load_default_groups_ Sz-2]= argv[1];windows_service= true;    Service.init (argv[1], mysql_service);//This inside calls the Mysql_service, that is, the indirect call to Win_main () return 0;} } else if (argc = = 3)/* Install or Remove any optional service */{if (!default_service_handling (argv, argv[2], argv[2], File_path, "", NULL) return 0;if (Service.isservice (argv[2])) {/*mysqld was started asmysqld--defaults-file=my_path\my.        INI service-name*/use_opt_args=1;opt_argc= 2; Skip service-nameopt_argv=argv;windows_service= true;if (my_strcasecmp (System_charset_info, argv[2], "MySQL")) Load_default_groups[load_default_groups_sz-2]= argv[2];    Service.init (argv[2], mysql_service);//This inside calls the Mysql_service, that is, the indirect call to Win_main () return 0;}  } else if (argc = = 4 | | argc = = 5) {/*this may seem strange, because we handle--local-service whilepreserving ' s Behavior of allowing any one other argument that ispassed to the service on startup. (The assumption is, and that's is--defaults-file=file, but it's not a enforced in 4.1 and so we don ' tenforce it's here.) */const Char *extra_opt= nulls;const char *account_name = nulls;int index;for (index = 3; index < ARGC; index++) {iF (!strcmp (Argv[index], "--local-service")) account_name= "NT authority\\localservice"; elseextra_opt= Argv[index];}                                      if (argc = = 4 | | account_name) if (!default_service_handling (argv, argv[2], argv[2], File_path,    Extra_opt, account_name)) return 0;} else if (argc = = 1 && service.isservice (mysql_servicename)) {/* start the default Service */windows_service= TR Ue    Service.init (Mysql_servicename, mysql_service);//This inside calls the Mysql_service, that is to say the indirect call to Win_main () return 0; }}

That's all I'm going to read?

First look at the MySQL source code mysql.cc

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.