翼度科技»论坛 编程开发 mysql 查看内容

技术分享 | kill掉mysqld_safe进程会影响mysqld进程?

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
1、背景

公司内部看到一则问题
1、kill -9 mysqld_safe 进程
2、systemd 检测到 mysqld_safe 进程不存在后,重新拉起 mysqld_safe 进程
3、mysqld_safe 进程启动后,发现 mysqld 进程也被重启
期望:启、停 mysqld_safe 进程,不会影响 mysqld 进程
2、systemd 服务启动

2.1、复现问题

1)、查看数据库服务状态
  1. [greatsql@greatsql-1 ~]$ sudo systemctl status db-4306
  2. ● db-4306.service - db-4306 Server
  3.    Loaded: loaded (/usr/lib/systemd/system/db-4306.service; disabled; vendor preset: disabled)
  4.    Active: active (running) since Wed 2023-07-19 11:15:18 CST; 6h ago
  5. Main PID: 14917 (mysqld_safe)
  6.    CGroup: /system.slice/db-4306.service
  7.            ├─14917 /bin/sh /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
  8.            └─16340 /greatsql/svr/greatsql/bin/mysqld --defaults-file=/greatsql/conf/greatsql4306.cnf --basedir=/greatsql/svr/greatsql --datadir=/greatsql/dbdata/data4306/data -...
  9. Jul 19 11:15:18 greatsql-1 systemd[1]: Started db-4306 Server.
  10. Jul 19 11:15:19 greatsql-1 mysqld_safe[14917]: mysqld_safe Adding '/greatsql/svr/GreatSQL-8.0.32-24-Linux-glibc2.17-x86_64/lib/mysql/libjemalloc.so.1' t...or mysqldJul 19 11:15:19 greatsql-1 mysqld_safe[14917]: 2023-07-19T03:15:19.907338Z mysqld_safe Logging to '/greatsql/logs/error4306.log'.
  11. Jul 19 11:15:19 greatsql-1 mysqld_safe[14917]: 2023-07-19T03:15:19.953728Z mysqld_safe Starting mysqld daemon with databases from /greatsql/dbdata/data4306/data
  12. Hint: Some lines were ellipsized, use -l to show in full.
复制代码
2)、kill -9 mysqld_safe 进程,并再次查看数据库服务状态
  1. [greatsql@greatsql-1 ~]$ kill -9 14917
  2. [greatsql@greatsql-1 ~]$ sudo systemctl status db-4306
  3. ● db-4306.service - db-4306 Server
  4.    Loaded: loaded (/usr/lib/systemd/system/db-4306.service; disabled; vendor preset: disabled)
  5.    Active: active (running) since Wed 2023-07-19 18:00:33 CST; 43s ago
  6. Main PID: 15195 (mysqld_safe)
  7.     Tasks: 50
  8.    CGroup: /system.slice/db-4306.service
  9.            ├─15195 /bin/sh /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
  10.            └─16613 /greatsql/svr/greatsql/bin/mysqld --defaults-file=/greatsql/conf/greatsql4306.cnf --basedir=/greatsql/svr/greatsql --datadir=/greatsql/dbdata/data4306/data -...
  11. Jul 19 18:00:33 greatsql-1 systemd[1]: Started db-4306 Server.
  12. Jul 19 18:00:34 greatsql-1 mysqld_safe[15195]: mysqld_safe Adding '/greatsql/svr/GreatSQL-8.0.32-24-Linux-glibc2.17-x86_64/lib/mysql/libjemalloc.so.1' t...or mysqldJul 19 18:00:34 greatsql-1 mysqld_safe[15195]: 2023-07-19T10:00:34.640240Z mysqld_safe Logging to '/greatsql/logs/error4306.log'.
  13. Jul 19 18:00:34 greatsql-1 mysqld_safe[15195]: 2023-07-19T10:00:34.679333Z mysqld_safe Starting mysqld daemon with databases from /greatsql/dbdata/data4306/data
  14. Hint: Some lines were ellipsized, use -l to show in full.
复制代码
确实在 kill -9 mysqld_safe 后,重新拉起了 mysqld_safe 和 mysqld 进程(它们的 PID 和之前不一样)
3)、查看数据库错误日志
  1. 2023-07-19T18:00:31.933020+08:00 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.32-24).
复制代码
4)、查看 service 文件
  1. [greatsql@greatsql-1 ~]$ cat /usr/lib/systemd/system/db-4306.service
  2. [Unit]
  3. Description=db-4306 Server
  4. After=network.target
  5. [Install]
  6. WantedBy=multi-user.target
  7. [Service]
  8. User=greatsql
  9. Group=greatsql
  10. Type=simple
  11. ExecStart=/greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
  12. Restart=on-failure
  13. LimitNOFILE=1024000
  14. LimitNPROC=1024000
  15. TimeoutStopSec=15
  16. PrivateTmp=false
复制代码
2.2、分析原因

1)、查看进程信息
  1. [greatsql@greatsql-1 ~]$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
  2. PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
  3.     1 15195 15195 15195 ?           -1 Ss     986   0:00 /bin/sh /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
  4. 15195 16613 15195 15195 ?           -1 Sl     986   0:06 /greatsql/svr/greatsql/bin/mysqld --defaults-file=/greatsql/conf/greatsql4306.cnf --basedir=/greatsql/svr/greatsql --datadir=/greatsql/dbdata/data4306/data --plugin-dir=/greatsql/svr/greatsql/lib/plugin --log-error=/greatsql/logs/error4306.log --open-files-limit=65535 --pid-file=/greatsql/dbdata/data4306/data/mysql.pid --socket=/greatsql/dbdata/data4306/data/mysql.sock --port=4306
复制代码
mysqld_safe:PID(进程 ID)=PGID(进程组 ID)=SID(会话 ID),说明它是会话首进程,也是该进程组的组长
mysqld_safe 的 PID(进程 ID)=mysqld 的 PPID(父进程 ID),说明 mysqld_safe 是 mysqld 的父进程
kill -9 mysqld_safe (会话首进程),会向该进程组的每一个进程发送 SIGKILL,导致组中的进程被中止
2)、整体流程

  • mysqld_safe 是会话首进程,kill -9 mysqld_safe,导致组中所有进程被 kill
  • systemd 检测到 mysqld_safe 异常退出,Restart=on-failure 触发重新拉起 mysqld_safe
  • mysqld_safe 拉起子进程 mysqld
3、mysqld_safe 命令启动

1)、使用 mysqld_safe 启动数据库
  1. [greatsql@greatsql-1 ~]$ /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf &
  2. [1] 18229
  3. [greatsql@greatsql-1 ~]$  mysqld_safe Adding '/greatsql/svr/GreatSQL-8.0.32-24-Linux-glibc2.17-x86_64/lib/mysql/libjemalloc.so.1' to LD_PRELOAD for mysqld
  4. 2023-07-19T14:20:19.135297Z mysqld_safe Logging to '/greatsql/logs/error4306.log'.
  5. 2023-07-19T14:20:19.173594Z mysqld_safe Starting mysqld daemon with databases from /greatsql/dbdata/data4306/data
复制代码
2)、查看进程信息
  1. [greatsql@greatsql-1 ~]$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
  2. PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
  3. 17360 18229 18229 17206 pts/7    17360 S      986   0:00 /bin/sh /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
  4. 18229 19658 18229 17206 pts/7    17360 Sl     986   0:02 /greatsql/svr/greatsql/bin/mysqld --defaults-file=/greatsql/conf/greatsql4306.cnf --basedir=/greatsql/svr/greatsql --datadir=/greatsql/dbdata/data4306/data --plugin-dir=/greatsql/svr/greatsql/lib/plugin --log-error=/greatsql/logs/error4306.log --open-files-limit=65535 --pid-file=/greatsql/dbdata/data4306/data/mysql.pid --socket=/greatsql/dbdata/data4306/data/mysql.sock --port=4306
复制代码
PID≠SID,不是会话首进程
PGID≠TPGID,是后台进程组
mysqld_safe 的 PID=mysqld 的 PPID,说明 mysqld_safe 是 mysqld 的父进程
3)、kill -9 mysqld_safe 进程,并再次查看进程信息
  1. [greatsql@greatsql-1 ~]$ kill -9 18229
  2. [greatsql@greatsql-1 ~]$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
  3. PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
  4.     1 19658 18229 17206 pts/7    17360 Sl     986   0:07 /greatsql/svr/greatsql/bin/mysqld --defaults-file=/greatsql/conf/greatsql4306.cnf --basedir=/greatsql/svr/greatsql --datadir=/greatsql/dbdata/data4306/data --plugin-dir=/greatsql/svr/greatsql/lib/plugin --log-error=/greatsql/logs/error4306.log --open-files-limit=65535 --pid-file=/greatsql/dbdata/data4306/data/mysql.pid --socket=/greatsql/dbdata/data4306/data/mysql.sock --port=4306
复制代码
kill -9 mysqld_safe (非会话首进程),不影响同组的进程(mysqld),此时 init 进程会自动领养 mysqld 进程
4)、重新启动 mysqld_safe 进程,并再次查看进程信息
  1. [greatsql@greatsql-1 ~]$ /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf &
  2. [1] 31401
  3. [greatsql@greatsql-1 ~]$  mysqld_safe Adding '/greatsql/svr/GreatSQL-8.0.32-24-Linux-glibc2.17-x86_64/lib/mysql/libjemalloc.so.1' to LD_PRELOAD for mysqld
  4. 2023-07-19T14:38:42.429733Z mysqld_safe Logging to '/greatsql/logs/error4306.log'.
  5. 2023-07-19T14:38:42.493870Z mysqld_safe A mysqld process already exists
  6. [1]+  Exit 1                  /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
  7. [greatsql@greatsql-1 ~]$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
  8. PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
  9.     1 19658 18229 17206 pts/7    17360 Sl     986   0:09 /greatsql/svr/greatsql/bin/mysqld --defaults-file=/greatsql/conf/greatsql4306.cnf --basedir=/greatsql/svr/greatsql --datadir=/greatsql/dbdata/data4306/data --plugin-dir=/greatsql/svr/greatsql/lib/plugin --log-error=/greatsql/logs/error4306.log --open-files-limit=65535 --pid-file=/greatsql/dbdata/data4306/data/mysql.pid --socket=/greatsql/dbdata/data4306/data/mysql.sock --port=4306
复制代码
mysqld_safe 退出,原因是已存在 mysqld 进程
4、总结


  • mysqld_safe 进程和 mysqld 进程是父子进程关系
  • systemd 服务启动,kill 父进程(会话首进程),会导致子进程也退出
  • mysqld_safe 命令启动,kill 父进程(非会话首进程),不会导致子进程退出;重新启动父进程,报错子进程已存在
  • 可以使用mysqld_safe命令启动 + 改造mysqld_safe脚本,实现【启、停mysqld_safe进程,不会影响mysqld进程】。此时不要混合使用systemd启动数据库,需要维护这个特殊的mysqld_safe

Enjoy GreatSQL
来源:https://www.cnblogs.com/greatsql/p/17627824.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具