Home Database Mysql Tutorial mysql的启动过程详解_MySQL

mysql的启动过程详解_MySQL

Jun 01, 2016 pm 01:05 PM
mysql

有一天,两个不懂mysql内核的人想去了解mysql内核代码,两个人不是去调试代码、查找资料,而是在那边思考。因为不了解内核,所以边思考边去验证。
 
使用的mysql代码是5.1.7,调试环境是windows平台下的vs2003。
 
Bingxi:“alex,你觉得mysql的启动过程会是什么样的呢?我们以银行为例吧。”
Alex:“嗯,bingxi。早上银行开门了,会先准备好环境,然后开门迎客,mysql也是这样。Mysql里面会有一个handle_connections_sockets函数,这个函数就好比是个叫号机,每个用户来了都会取个号,然后就会进行业务处理。”

代码如下:

pthread_handler_t handle_connections_sockets(void *arg __attribute__((unused)))
{
  ……
  while (!abort_loop)
  {
    select((int) max_used_connection,&readFDs,0,0,0)     ……
    accept(sock, my_reinterpret_cast(struct sockaddr *) (&cAddr),&length)  //接受请求
    ……
    create_new_thread(thd);
  }
  //abort_loop=1,则执行到这里进行推出。今天业务不处理了
}


Bingxi:“啊,这里面存在两种可能的,1)用户来一个就分配一个工作人员处理,2)将排号的人丢进工作队列,根据叫号机到指定窗口获取服务。前者的场景适合于请求量大,并且需要响应速度特别快的情况,但是分配也会有个限制,所谓的最大连接数,这样的情况常见于互联网行业,相应地我们可以看到机器的负载变化范围特别大。同样的,这也是它的一个弊端,假设每个业务都复杂(消耗资源型sql语句),同时处理的话,机器会支撑不住,这时候第二种方法就比较好,这种情况属于事务性场景。”

Alex:“嗯,是的。Mysql选择的是前者,oracle提供两种方法供选择。我们继续往下面的代码看,如果我们配置了线程缓存,且有可用的缓存,则唤醒该线程,否则创建新的线程。”

代码如下:


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:“嗯,老杨。是不是理解银行为客户分配了一个服务人员,在这段期间一直为该客户服务。里面有个代码段,是一直在等用户下命令。但是有可能网络,或者被kill掉了,就像一个人存了100,不断取1块钱一样,被保安带走了。”

代码如下:


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:“嗯,获取命令,然后执行命令。在dispatch_command函数中,根据不同的客户请求进行响应的处理,比如开账户、存钱等”

代码如下:


bool do_command(THD *thd)
{
 
  if ((packet_length=my_net_read(net)) == packet_error) //获取命令
 
  DBUG_RETURN(dispatch_command(command,thd, packet+1, (uint) packet_length));
}

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles