下面信息抄自爱可生开源社区
当MySQL服务收到关闭服务的SIGINT 信号后,进行如下处理:
可以有多种方式来启动关机进程,如:
根据触发关机的方式不同来决定是否需要创建一个shutdown线程来执行关闭操作。
为了预防在关闭系统过程再接收新任务,MySQL 会关闭 TCP/IP 端口、socket 等通道来阻止接受新的客户端连接。
对于已建立连接,则会:
MySQL Server层会刷新所有表缓存并关闭所有已打开的表。
每个存储引擎执行对其管理的表和对象执行必要的操作,如InnoDB存储引擎会刷新缓冲池中脏页数据和更新表空间的LSN。
根据服务关闭情况向管理进程返回"退出代码"
MySQL正常关闭过程中Error日志记录信息为:
2022-03-25T17:17:58.165016+08:00 0 [Note] Giving 1 client threads a chance to die gracefully 2022-03-25T17:17:58.165059+08:00 0 [Note] Shutting down slave threads 2022-03-25T17:17:58.165070+08:00 3 [Warning] SEMISYNC: Forced shutdown. Some updates might not be replicated. 2022-03-25T17:17:58.165092+08:00 3 [Note] Semi-sync replication switched OFF. 2022-03-25T17:17:58.165654+08:00 0 [Note] Forcefully disconnecting 0 remaining clients 2022-03-25T17:17:58.165680+08:00 0 [Note] Event Scheduler: Purging the queue. 0 events 2022-03-25T17:17:58.165919+08:00 0 [Note] Binlog end 2022-03-25T17:17:58.167688+08:00 0 [Note] Shutting down plugin 'rpl_semi_sync_slave' 2022-03-25T17:17:58.167719+08:00 0 [Note] Shutting down plugin 'rpl_semi_sync_master' 2022-03-25T17:17:58.167745+08:00 0 [Note] Stopping ack receiver thread 2022-03-25T17:17:58.167831+08:00 0 [Note] unregister_replicator OK 2022-03-25T17:17:58.167843+08:00 0 [Note] Shutting down plugin 'ngram' 省略shutting down plugin 2022-03-25T17:17:58.167985+08:00 0 [Note] Shutting down plugin 'InnoDB' 2022-03-25T17:17:58.168053+08:00 0 [Note] InnoDB: FTS optimize thread exiting. 2022-03-25T17:17:58.168140+08:00 0 [Note] InnoDB: Starting shutdown... 2022-03-25T17:18:00.704250+08:00 0 [Note] InnoDB: Shutdown completed; log sequence number 63089498676 2022-03-25T17:18:00.705160+08:00 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1" 2022-03-25T17:18:00.705183+08:00 0 [Note] Shutting down plugin 'MEMORY' 省略shutting down plugin 2022-03-25T17:18:00.705547+08:00 0 [Note] Shutting down plugin 'binlog' 2022-03-25T17:18:00.706904+08:00 0 [Note] /opt/soft/mysql57/bin/mysqld: Shutdown complete
从错误日志中可以发现:
使用正常非暴力方式关闭MySQL服务时,如果该MySQL服务是半同步复制的主节点,可能会导致部分事务在事务提交前未收到从节点返回的确认信息。
聊聊 MySQL 关机的故事
MySQL-The Server Shutdown Process