Home > Database > Mysql Tutorial > Parse the startup process of mysql

Parse the startup process of mysql

怪我咯
Release: 2017-04-01 10:46:42
Original
1148 people have browsed it

One day, two people who didn’t understand the mysql kernel wanted to understand the mysql kernel code. Instead of debugging the code and looking for information, the two people That’s where you think. Because I don’t understand the kernel, I verify it while thinking about it.
The mysql code used is 5.1.7, and the debugging environment is vs2003 under the windows platform.
Bingxi: "Alex, what do you think the startup process of mysql will be like? Let's take bank as an example." Alex: "Well, bingxi. The bank is open this morning. The environment will be prepared first, and then the door will be opened to welcome guests. The same is true for mysql. There will be a han
dle_connections_socketsfunction in Mysql. This function is like a dialing machine. Each user comes to A number will be taken, and then the business will be processed. "

pthread_handler_t handle_connections_sockets(void *arg attribute((unused)))

{

  ……

  while (!abort_loop)

  {

    select((int) max_used_connection,&readFDs,0,0,0) < 0) //有连接了则往下来执行,否则一直等待

    ……

    accept(sock, my_reinterpret_cast(struct sockaddr *) (&cAddr),&length)  //接受请求

    ……

    create_new_thread(thd);

  }

  //abort_loop=1,则执行到这里进行推出。今天业务不处理了

}
Copy after login

Bingxi: "Ah, there are two possibilities here, 1) a staff member will be assigned to handle it when the user comes. ) Throw the people who are scheduled into the work

queue, and get the service from the designated window according to the number calling machine. The former scenario is suitable for situations where the request volume is large and the response speed is particularly fast, but there will also be distribution problems. There is a limit, the so-called maximum number of connections. This situation is common in the Internet industry. Correspondingly, we can see that the load variation range of the machine is particularly large. Similarly, this is also one of its drawbacks, assuming that each business is complex (consumes resources). SQL statements), the machine will not be able to support it if processed at the same time. In this case, the second method is better. This situation belongs to a transactional scenario. "Alex: "Well, yes, Mysql chooses the former. , Oracle provides two methods to choose from. Let’s continue to look at the following code. If we configure the thread
cache, and there is an available cache, wake up the thread, otherwise 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)))

  }

}
Copy after login

Bingxi: "Well, Lao Yang. Do you understand that the bank assigns a service staff to the customer and has been serving the customer during this period. There is a code segment in it, which has been waiting for the user to download Order. But it is possible that the network was killed, just like a person who deposited 100 and kept withdrawing 1 yuan, and was taken away by the security guard. 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, depositing money, etc.

The above is the detailed content of Parse the startup process of mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template