mysql的启动过程详解
有一天,两个不懂mysql内核的人想去了解mysql内核代码,两个人不是去调试代码、查找资料,而是在那边思考。因为不了解内核,所以边思考边去验证。 使用的mysql代码是5.1.7,调试环境是windows平台下的vs2003。 Bingxi:“alex,你觉得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));
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.
