MySQL server connection process analysis, mysql Server Analysis

Source: Internet
Author: User

MySQL server connection process analysis, mysql Server Analysis

Mysqld is the master process on the MySQL server. It can be said that mysqld is the real core of MySQL, and all work is carried out around the mysqld process. Therefore, mysqld code is the Best Breakthrough to dissect mysql.

Everything starts with the familiar main () function. It actually starts with the mysqld_main () function. These codes are all in mysqld. cc. Mysqld_main () then calls win_main )(). The win_main () function performs initialization.

After the initialization is complete, MySQL is ready to accept the connection. Then the Handle_connections_methods () function was launched. The main task of this function is to create three sub-processes, which are connected by TCP/IP, named pipe, and shared memory. Generally, the customer uses TCP/IP (socket) to connect to the MySQL server. This is the most elastic communication method. However, in the application environment of embedded software, the last two communication methods are required.

The simplified handle_connections_methods () function:
Copy 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 three new threads are created, the handle_connectins_methods () function enters a long loop and does not exit until all three connection threads exit. Here I mainly look at the socket connection thread. 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 select () to call the listening port of mysqld, and then waits for the connection from the client. After a client connection, a THD type variable will be created in this function. This variable is an "Intercommunication flower" and starts from the connection establishment, SQL syntax analysis, query execution, and result return. This variable is always there. In short, this is a very important variable.

There is also the struct st_vio, which is a command Transfer Station. A vio type struct is also defined in the "Social flower" THD. The function of this struct is to read the communication content from the socket, and then assign its own value to the vio variable of THD. The VIO type describes a request in detail, including the request content, time, and request socket address. The next thing that happens is to pass this "intercommunication flower" to the service thread. create_thread_to_handle_connection () implements this function.

The following code is deleted:
Copy 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 threads, but cache them). If yes, it will use a cache thread, if not, create a new thread to serve the connection. So far, a connection enters the service thread, and the connection thread returns to continue waiting for the connection.

The subsequent content is implemented in the service thread. The deep understanding of MySQL contains detailed code tracking. If you are interested, you can take a look. I have attached the function call sequence for your reference.
Copy 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 ()

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.