What is the connection process of the MySQL server?

Source: Internet
Author: User

Mysqld is the MySQL server-side main process, and it can be said that Mysqld is the true core of MySQL, all work revolves 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.cc. 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:

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 3 new threads have been created, the Handle_connectins_methods () function goes into a long cycle until the 3 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 on 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. The

also has the struct ST_VIO this structure, 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.

    Below is the deleted code

void Create_thread_to_handle_connection (THD *thd)
{
if (Cached_thread_count > Wake_thread)
{
my  Sql_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 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.

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