SoFunction
Updated on 2025-03-09

A brief analysis of MySQL server connection process

mysqld is the main process of MySQL server side. It can be said that mysqld is the real core of MySQL. All work revolves around the mysqld process. So to dissect the behemoth of mysql, mysqld's code is the best breakthrough.

Everything starts with the familiar main() function, but in fact it starts with the mysqld_main() function. All of these codes are there. mysqld_main() then calls win_main)(). The win_main() function mainly does some initialization work.

After the initialization work is completed, MySQL is ready to accept the connection. Then our protagonist, Handle_connections_methods() function, came on. The main job of this function is to create three new child processes, which accept connections in three ways: TCP/IP, named pipes and shared memory. Generally speaking, customers use TCP/IP (socket) to connect to MySQL server, which is the most flexible way of communication. However, in the application environment of embedded software, the latter two communication methods need to be adopted.

The simplified handle_connections_methods() function:

Copy the codeThe code is as follows:

static void handle_connections_methods()
{
  mysql_mutex_lock(&LOCK_thread_count);
  mysql_cond_init(key_COND_handler_count, &COND_handler_count, NULL);
  handler_count=0;
  handler_count++;
  mysql_thread_create(key_thread_handle_con_namedpipes, &hThread, &connection_attrib, handle_connections_namedpipes, 0));
  handler_count++;
  mysql_thread_create(key_thread_handle_con_sockets, &hThread, &connection_attrib, handle_connections_sockets_thread, 0));
  handler_count++;
  mysql_thread_create(key_thread_handle_con_sharedmem, &hThread, &connection_attrib, handle_connections_shared_memory, 0))
  while (handler_count > 0)
    mysql_cond_wait(&COND_handler_count, &LOCK_thread_count);
  mysql_mutex_unlock(&LOCK_thread_count);
}

After creating 3 new threads, the handle_connectins_methods() function enters a long loop until all 3 connection threads exit. Here I mainly look at the connection thread of socket. Our research object is handle_connections_sockets_thread. After this thread initializes itself, it directly calls handle_connections_sockets();

The handle_connections_sockets() function uses the select() call to listen for the port of mysqld, and then waits for the client to connect. After a client is connected, a new THD-type variable will be created in this function. This variable is a "communicational flower", starting from the connection establishment, to SQL syntax analysis, query execution, result return, etc. This variable has always been there, in short, this is a very important variable.

There is also the struct st_vio structure, which is a command transit station. A vio-type structure is also defined in "Communicative Flower" THD. The function of this structure is to read communication content from the socket from the storage, and then assign its own value to the vio variable of THD. The VIO type describes a request in detail, including the requested content, time, requested socket address, etc. What happened afterwards was to pass this "communication flower" to the service thread, and create_thread_to_handle_connection() implements this function.

The following is the deleted code

Copy the codeThe code is as follows:

void create_thread_to_handle_connection(THD *thd)
{
  if (cached_thread_count > wake_thread)
  {
    mysql_cond_signal(&COND_thread_cache);
  }
  else
  {
    mysql_thread_create(key_thread_one_connection, &thd->real_id, &connection_attrib, handle_one_connection, (void*) thd)));   
  }
}

This function will check whether there are idle cache threads (MySQL will not immediately destroy the disconnected service thread, but cache it). If there is, use the cache thread. If there is no, create a new thread to serve the connection. At this point, a connection enters the service thread, and the connection thread returns and continues to wait for the connection.

The following content is implemented in the service thread. There is a very detailed code tracking in "In-depth understanding of MySQL". Interested students can take a look. I've attached the order of function calls for reference.

Copy the codeThe code is 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()