Mysql startup process details

Source: Internet
Author: User
Tags mysql code
One day, two people who do not know the mysql kernel want to know the mysql kernel code. The two people do not want to debug the code or find information, but think about it over there. Because I don't know the kernel, I think about it and verify it. The mysql code used is 5.1.7, And the debugging environment is vs2003 on windows. Bingxi: "alex, what do you think is the mysql startup process?

One day, two people who do not know the mysql kernel want to know the mysql kernel code. The two people do not want to debug the code or find information, but think about it over there. Because I don't know the kernel, I think about it and verify it. The mysql code used is 5.1.7, And the debugging environment is vs2003 on windows. Bingxi: "alex, what do you think is the mysql startup process?

One day, two people who do not know the mysql kernel want to know the mysql kernel code. The two people do not want to debug the code or find information, but think about it over there. Because I don't know the kernel, I think about it and verify it.

The mysql code used is 5.1.7, And the debugging environment is vs2003 on windows.

Bingxi: "alex, what do you think the mysql startup process will look like? Let's take a bank as an example ."
Alex: "Well, bingxi. When the bank opens the door in the morning, the environment will be prepared first, and then the door will be opened to welcome customers. This is also true for mysql. In Mysql, there will be a handle_connections_sockets function, which is like a number machine. Every user will get a number and then perform business processing ."

Pthread_handler_t handle_connections_sockets (void * arg _ attribute _ (unused )))
{
......
While (! Abort_loop)
{
Select (int) max_used_connection, & readFDs, 0) <0) // If a connection exists, it is executed. Otherwise, it will wait.
......
Accept (sock, my_reinterpret_cast (struct sockaddr *) (& cAddr), & length) // accept the request
......
Create_new_thread (thd );
}
// Abort_loop = 1, then execute it here for release. The business is not processed today
}


Bingxi: "Ah, there are two possibilities. 1) One user can assign a staff member for processing, and 2) Drop the employee ID into the work queue, obtain the service in the specified window according to the number of machines. The former scenario is suitable for scenarios where a large number of requests are required and the response speed is extremely fast, but there are limits on the allocation. The so-called maximum number of connections is common in the Internet industry, correspondingly, we can see that the range of changes to the server load is extremely large. Similarly, this is also a drawback. If every business is complicated (resource-based SQL statements are consumed) and processed at the same time, the machine will not be able to support it. In this case, the second method is better, this is a transactional scenario."

Alex: "Well, yes. Mysql selects the former, and oracle provides two methods for selection. We continue to look at the code below. If we have configured a thread cache and available cache, we will wake up this thread; otherwise, we will create a new thread ."


Static void create_new_thread (THD * thd)
{

If (cached_thread_count> wake_thread)
{
Start_cached_thread (thd );
}
Else
{
If (error = pthread_create (& thd-> real_id, & connection_attrib,
Handle_one_connection,
(Void *) thd )))
}
}


Bingxi: "Well, Lao Yang. Does it mean that the Bank has assigned a service person to the customer and has served the customer during this period. There is a code segment that is waiting for the user to run the command. However, it may be possible that the network may be killed, just as a person saves 100 yuan and keeps taking one dollar, it is taken away by the security guard ."


Pthread_handler_t handle_one_connection (void * arg)
{
While (! Net-> error & net-> vio! = 0 &&
! (Thd-> killed = THD: KILL_CONNECTION ))
{
Net-> no_send_error = 0;
If (do_command (thd ))
Break;
}
}


Alex: "Well, get the command and then execute the command. In the dispatch_command function, responses are processed based on different customer requests, such as opening an account or saving money"


Bool do_command (THD * thd)
{

If (packet_length = my_net_read (net) = packet_error) // obtain the command

DBUG_RETURN (dispatch_command (command, thd, packet + 1, (uint) packet_length ));
}

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.