Analysis of Mysql server connection process _mysql

Source: Internet
Author: User
Tags socket

MYSQLD is the MySQL server-side main process, it can be said that Mysqld is the true core of MySQL, all work is done around the mysqld process. So to dissect MySQL this behemoth, Mysqld's code is the best breach.

Everything starts with the familiar main () function, actually starting with the Mysqld_main () function. These codes are in Mysqld_main () then called the Win_main) (). The Win_main () function mainly does some initialization work.

After initialization is complete, MySQL is ready to accept the connection. Then our protagonist Handle_connections_methods () function. The main job of this function is to create 3 new child processes, which accept TCP/IP, named pipes, and shared memory connections in three different ways. In general, customers are using TCP/IP (socket) to connect to the MySQL server, which is the most flexible way of communication. However, in the application environment of embedded software, the latter two kinds of communication methods need to be adopted.

Simplified Handle_connections_methods () function:

Copy Code code as follows:

static void Handle_connections_methods ()
  Mysql_mutex_lock (&lock_thread_count);
  Mysql_cond_init (Key_cond_handler_count, &cond_handler_count, NULL);
  Mysql_thread_create (key_thread_handle_con_namedpipes, &hthread, &connection_attrib, Handle_ Connections_namedpipes, 0);
  Mysql_thread_create (key_thread_handle_con_sockets, &hthread, &connection_attrib, Handle_ Connections_sockets_thread, 0);
  Mysql_thread_create (Key_thread_handle_con_sharedmem, &hthread, &connection_attrib, Handle_ Connections_shared_memory, 0))
  while (Handler_count > 0)
    mysql_cond_wait (&con D_handler_count, &lock_thread_count);
  Mysql_mutex_unlock (&lock_thread_count);

After 3 new threads have been created, the Handle_connectins_methods () function goes into a long loop until 3 of the connecting threads exit. Here I mainly look at the socket connection thread, our research object is this handle_connections_sockets_thread. After this thread initializes itself, it directly invokes the handle_connections_sockets ();

The Handle_connections_sockets () function uses a select () call to listen for the port of the mysqld, and then wait for the client to connect. When a client connects, a new THD type variable is created in this function, which is a "butterfly", starting with connection creation, SQL parsing, query execution, result return, and so on. This variable is always there, and it's a very important variable anyway.

There is also the struct St_vio, the structural body, which is a staging point for the command. A vio type structure is also defined in the THD of "The Butterfly". The function of this structure is to read the communication content from the socket and then assign its own value to the THD vio variable. A request is described in detail in the vio type, including the content of the request, the time, the requested socket address, and so on. The next thing that happens is to pass this "butterfly" to the service Thread, create_thread_to_handle_connection () to implement this function.

Here is the code after the deletion

Copy Code code as follows:

void Create_thread_to_handle_connection (THD *thd)
if (Cached_thread_count > Wake_thread)
Mysql_cond_signal (&cond_thread_cache);
Mysql_thread_create (Key_thread_one_connection, &thd->real_id, &connection_attrib, Handle_one_ Connection, (void*) THD));

This function will see if there is no idle cache thread (MySQL does not immediately destroy the disconnected service thread, but caches it), if there is a caching thread, if there is no new thread service connection. At this point, a connection enters the service thread, and the connection thread returns to continue waiting for the connection.

The following content is implemented in the service thread, "in-depth understanding of MySQL" in a very detailed code tracking, interested students can see. I enclose the function call order for reference.

Copy Code code as follows:

Handle_one_connection ()
Mysql_thread_create ()
Handle_one_connection ()
Do_handle_one_connection ()
Init_new_connection_thread ()
Init_new_connection_handler_thread ()
Do_command ()
Dispatch_command ()
Mysql_parse ()
Mysql_execuate_command ()

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: 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.