张月娟 发表于 2023-8-14 12:14:48

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

1、背景

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

2.1、复现问题

1)、查看数据库服务状态
$ sudo systemctl status db-4306
● db-4306.service - db-4306 Server
   Loaded: loaded (/usr/lib/systemd/system/db-4306.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-07-19 11:15:18 CST; 6h ago
Main PID: 14917 (mysqld_safe)
   CGroup: /system.slice/db-4306.service
         ├─14917 /bin/sh /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
         └─16340 /greatsql/svr/greatsql/bin/mysqld --defaults-file=/greatsql/conf/greatsql4306.cnf --basedir=/greatsql/svr/greatsql --datadir=/greatsql/dbdata/data4306/data -...
Jul 19 11:15:18 greatsql-1 systemd: Started db-4306 Server.
Jul 19 11:15:19 greatsql-1 mysqld_safe: 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: 2023-07-19T03:15:19.907338Z mysqld_safe Logging to '/greatsql/logs/error4306.log'.
Jul 19 11:15:19 greatsql-1 mysqld_safe: 2023-07-19T03:15:19.953728Z mysqld_safe Starting mysqld daemon with databases from /greatsql/dbdata/data4306/data
Hint: Some lines were ellipsized, use -l to show in full.2)、kill -9 mysqld_safe 进程,并再次查看数据库服务状态
$ kill -9 14917
$ sudo systemctl status db-4306
● db-4306.service - db-4306 Server
   Loaded: loaded (/usr/lib/systemd/system/db-4306.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-07-19 18:00:33 CST; 43s ago
Main PID: 15195 (mysqld_safe)
    Tasks: 50
   CGroup: /system.slice/db-4306.service
         ├─15195 /bin/sh /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
         └─16613 /greatsql/svr/greatsql/bin/mysqld --defaults-file=/greatsql/conf/greatsql4306.cnf --basedir=/greatsql/svr/greatsql --datadir=/greatsql/dbdata/data4306/data -...
Jul 19 18:00:33 greatsql-1 systemd: Started db-4306 Server.
Jul 19 18:00:34 greatsql-1 mysqld_safe: 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: 2023-07-19T10:00:34.640240Z mysqld_safe Logging to '/greatsql/logs/error4306.log'.
Jul 19 18:00:34 greatsql-1 mysqld_safe: 2023-07-19T10:00:34.679333Z mysqld_safe Starting mysqld daemon with databases from /greatsql/dbdata/data4306/data
Hint: Some lines were ellipsized, use -l to show in full.确实在 kill -9 mysqld_safe 后,重新拉起了 mysqld_safe 和 mysqld 进程(它们的 PID 和之前不一样)
3)、查看数据库错误日志
2023-07-19T18:00:31.933020+08:00 0 Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.32-24).4)、查看 service 文件
$ cat /usr/lib/systemd/system/db-4306.service

Description=db-4306 Server
After=network.target


WantedBy=multi-user.target


User=greatsql
Group=greatsql

Type=simple
ExecStart=/greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
Restart=on-failure
LimitNOFILE=1024000
LimitNPROC=1024000
TimeoutStopSec=15
PrivateTmp=false2.2、分析原因

1)、查看进程信息
$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
PPID   PIDPGID   SID TTY      TPGID STAT   UID   TIME COMMAND
    1 15195 15195 15195 ?         -1 Ss   986   0:00 /bin/sh /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
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=4306mysqld_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 启动数据库
$ /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf &
18229
$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
2023-07-19T14:20:19.135297Z mysqld_safe Logging to '/greatsql/logs/error4306.log'.
2023-07-19T14:20:19.173594Z mysqld_safe Starting mysqld daemon with databases from /greatsql/dbdata/data4306/data2)、查看进程信息
$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
PPID   PIDPGID   SID TTY      TPGID STAT   UID   TIME COMMAND
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
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=4306PID≠SID,不是会话首进程
PGID≠TPGID,是后台进程组
mysqld_safe 的 PID=mysqld 的 PPID,说明 mysqld_safe 是 mysqld 的父进程
3)、kill -9 mysqld_safe 进程,并再次查看进程信息
$ kill -9 18229
$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
PPID   PIDPGID   SID TTY      TPGID STAT   UID   TIME COMMAND
    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=4306kill -9 mysqld_safe (非会话首进程),不影响同组的进程(mysqld),此时 init 进程会自动领养 mysqld 进程
4)、重新启动 mysqld_safe 进程,并再次查看进程信息
$ /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf &
31401
$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
2023-07-19T14:38:42.429733Z mysqld_safe Logging to '/greatsql/logs/error4306.log'.
2023-07-19T14:38:42.493870Z mysqld_safe A mysqld process already exists

+Exit 1                  /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf

$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
PPID   PIDPGID   SID TTY      TPGID STAT   UID   TIME COMMAND
    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=4306mysqld_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】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 技术分享 | kill掉mysqld_safe进程会影响mysqld进程?