1、背景
公司内部看到一则问题
1、kill -9 mysqld_safe 进程
2、systemd 检测到 mysqld_safe 进程不存在后,重新拉起 mysqld_safe 进程
3、mysqld_safe 进程启动后,发现 mysqld 进程也被重启
期望:启、停 mysqld_safe 进程,不会影响 mysqld 进程
2、systemd 服务启动
2.1、复现问题
1)、查看数据库服务状态
[greatsql@greatsql-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[1]: Started db-4306 Server.
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'.
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
Hint: Some lines were ellipsized, use -l to show in full.
2)、kill -9 mysqld_safe
进程,并再次查看数据库服务状态
[greatsql@greatsql-1 ~]$ kill -9 14917
[greatsql@greatsql-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 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[1]: Started db-4306 Server.
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'.
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
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 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.32-24).
4)、查看 service 文件
[greatsql@greatsql-1 ~]$ cat /usr/lib/systemd/system/db-4306.service
[Unit]
Description=db-4306 Server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
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=false
2.2、分析原因
1)、查看进程信息
[greatsql@greatsql-1 ~]$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
PPID PID PGID 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=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 启动数据库
[greatsql@greatsql-1 ~]$ /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf &
[1] 18229
[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
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/data
2)、查看进程信息
[greatsql@greatsql-1 ~]$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
PPID PID PGID 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=4306
PID≠SID,不是会话首进程
PGID≠TPGID,是后台进程组
mysqld_safe 的 PID=mysqld 的 PPID,说明 mysqld_safe 是 mysqld 的父进程
3)、kill -9 mysqld_safe 进程,并再次查看进程信息
[greatsql@greatsql-1 ~]$ kill -9 18229
[greatsql@greatsql-1 ~]$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
PPID PID PGID 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=4306
kill -9 mysqld_safe (非会话首进程),不影响同组的进程(mysqld),此时 init 进程会自动领养 mysqld 进程
4)、重新启动 mysqld_safe 进程,并再次查看进程信息
[greatsql@greatsql-1 ~]$ /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf &
[1] 31401
[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
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
[1]+ Exit 1 /greatsql/svr/greatsql/bin/mysqld_safe --defaults-file=/greatsql/conf/greatsql4306.cnf
[greatsql@greatsql-1 ~]$ ps axj |head -1;ps axj |grep 4306 |grep -v grep
PPID PID PGID 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=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
标签:--,safe,greatsql,mysqld,进程,svr From: https://www.cnblogs.com/greatsql/p/17627824.html