您的位置
主页 > 网站技术 > 数据库 > » 正文

MySQL服务器连接过程浅析

来源: 锦尚中国 点击:

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();




首页  - 关于站长圈  - 广告服务  - 联系我们  - 关于站长圈  - 网站地图  - 版权声明