Mysql startup Process Detailed _mysql

Source: Internet
Author: User
Tags mysql code

One day, two people do not know the MySQL kernel to understand the MySQL kernel code, two people are not to debug the code, find information, but over there to think. Because you do not understand the kernel, so think side to verify.

The MySQL code used is 5.1.7, and the debugging environment is vs2003 under the Windows platform.

Bingxi: "Alex, what do you think of the MySQL startup process?" Let's take the bank as an example. ”
Alex: "Well, Bingxi." The bank opens the door in the morning, prepares the environment first, then opens the door to greet the guest, MySQL is also like this. MySQL inside will have a handle_connections_sockets function, this function is like a call machine, each user will take a number, and then will conduct business processing. ”

Copy Code code as follows:
pthread_handler_t handle_connections_sockets (void *arg __attribute__ ((unused))
{
......
while (!abort_loop)
{
Select ((int) max_used_connection,&readfds,0,0,0) < 0)//have a connection then go down and execute, or wait
......
Accept (sock, my_reinterpret_cast (struct sockaddr *) (&CADDR), &length)//Accept Request
......
Create_new_thread (THD);
}
Abort_loop=1, then execute here to launch. Today's business is off the handle.
}

Bingxi: "Ah, there are two possible, 1 users to one to assign a worker processing, 2 will automatic arranging people into the work queue, according to the designated window to obtain services." The former scenario is suitable for large requests, and needs to respond particularly quickly, but the allocation will have a limit, the so-called maximum number of connections, such a situation is common in the Internet industry, accordingly we can see the machine load changes in the range of particularly large. Again, this is one of its drawbacks, assuming that each business is complex (consuming resource-intensive SQL statements), and that the machine will not support it at the same time, the second method is better, which is a transactional scenario. ”

Alex: "Well, yes." MySQL chooses the former, and Oracle offers two ways to choose. We continue to look at the code down, and if we configure the thread cache and have the available cache, wake the thread or create a new thread. ”

Copy Code code as follows:

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." Is it understood that the bank has assigned a service person to the customer and has been serving the customer during this period. There is a code snippet, is always waiting for the user to order. But it is possible that the network, or be killed off, like a person saved 100, and constantly take 1 dollars, the security was taken away. ”
Copy Code code as follows:

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, respond to different customer requests, such as opening an account, saving money, etc.
Copy Code code as follows:

BOOL Do_command (THD *THD)
{

if (packet_length=my_net_read (net) = = Packet_error)//Get command

Dbug_return (Dispatch_command (COMMAND,THD, packet+1, (UINT) packet_length));
}

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.