MySQL服务器连接过程分析

mxf8 9年前

mysqld是MySQL服务器端主进程,可以说mysqld是MySQL的真正核心,一切工作都是围绕mysqld进程进行的。所以要解剖mysql这个庞然大物,mysqld的代码是最好的突破口。

一切都是从熟悉的main()函数开始的,其实是从mysqld_main()函数开始的。这些代码都在mysqld.cc。mysqld_main()随后调用了win_main)()。win_main()函数主要是做了一些初始化的工作。

初始化工作完成之后,MySQL已经做好准备接受连接了。然后我们的主角Handle_connections_methods()函数登场了。这个函数的主要工作是新建3个子进程,他们分别接受TCP/IP、命名管道以及共享内存这三种方式的连接。一般情况下客户都是用TCP /IP(socket)来连接MySQL服务器的,这是最有弹性的通信方式。但是在嵌入式软件的应用环境中,需要采用后两种通信方式。

  • 简化后的handle_connections_methods()函数:
static void handle_connections_methods()  {    mysql_mutex_lock(&LOCK_thread_count);    mysql_cond_init(key_COND_handler_count, &COND_handler_count, NULL);    handler_count=0;    handler_count++;    mysql_thread_create(key_thread_handle_con_namedpipes, &hThread, &connection_attrib, handle_connections_namedpipes, 0));    handler_count++;    mysql_thread_create(key_thread_handle_con_sockets, &hThread, &connection_attrib, handle_connections_sockets_thread, 0));    handler_count++;    mysql_thread_create(key_thread_handle_con_sharedmem, &hThread, &connection_attrib, handle_connections_shared_memory, 0))    while (handler_count > 0)      mysql_cond_wait(&COND_handler_count, &LOCK_thread_count);    mysql_mutex_unlock(&LOCK_thread_count);  }

新建了3个线程之后,handle_connectins_methods()函数进入一个长时间循环,直到3个连接线程全部退出后才退出。这里我主要看看socket的连接线程,我们的研究对象就是这个handle_connections_sockets_thread。这个线程把自己初始化之后,就直接调用了handle_connections_sockets();

handle_connections_sockets()函数使用select()调用监听mysqld的端口,然后等待客户端的连接。等到一个客户端连接后,这个函数中会新建一个THD类型的变量,这个变量是一个“交际花”,从连接建立开始,到SQL语法分析、查询执行、结果返回等等。这个变量一直都在,总之这是一个非常重要的变量。

还有struct st_vio这个结构体,这个结构体是一个命令的中转站。在“交际花”THD中也定义了一个vio类型的结构体。这个结构体的功能就是从储存从套接字中读取通信内容,然后又把自己的值赋给THD的vio变量。VIO类型中详细的描述了一次请求,包括请求的内容、时间、请求的套接字地址等等。之后发生的事情就是把这个“交际花”传递到服务线程,create_thread_to_handle_connection()实现这个功能。

  • 以下是删减后的代码
void create_thread_to_handle_connection(THD *thd)  {    if (cached_thread_count > wake_thread)    {      mysql_cond_signal(&COND_thread_cache);    }    else    {      mysql_thread_create(key_thread_one_connection, &thd->real_id, &connection_attrib, handle_one_connection, (void*) thd)));        }  }

这个函数会查看有没有闲置的缓存线程(MySQL不会把断开连接后的服务线程立即销毁掉,而是缓存了起来),如果有就是用缓存线程,如果没有就新建一个线程来服务连接。至此,一个连接就进入了服务线程,连接线程返回继续等待连接。

后边的内容就都是在服务线程中实现的,《深入理解MySQL》中有很详细的代码跟踪,感兴趣的同学可以看看。我把函数调用顺序附上,供参考。

handle_one_connection()  mysql_thread_create()  handle_one_connection()  do_handle_one_connection()  init_new_connection_thread()  init_new_connection_handler_thread()  do_command()  dispatch_command()  mysql_parse()  mysql_execuate_command()
来源:http://www.codeceo.com/article/mysql-connect-to-server.html