代码改变世界

Linux systemctl如何修改服务名称

2026-03-20 14:05  潇湘隐者  阅读(1)  评论(0)    收藏  举报

修改服务名称

关于Linux systemctl中如何修改服务名, 这里有个简单的案例,我们需要将服务名mysql.service修改为mysqld.service, 跟公司的安装规范一致.

收集相关服务信息

# systemctl list-unit-files --type=service | grep mysql
mysql.service                              disabled   

如上所示, 服务名为mysql.service

$ systemctl status mysql
● mysql.service - MySQL Server
   Loaded: loaded (/etc/systemd/system/mysql.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2026-02-27 22:13:27 CST; 1 weeks 4 days ago
     Docs: https://dev.mysql.com/doc/refman/5.7/en/
 Main PID: 1366406 (mysqld_safe)
    Tasks: 62 (limit: 48804)
   Memory: 4.1G
   CGroup: /system.slice/mysql.service
           ├─1366406 /bin/sh /opt/mysql/mysql8/bin/mysqld_safe --defaults-file=/etc/my.cnf
           └─3124328 /opt/mysql/mysql8/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql/mysql8 --datadir=/data/mysql --plugin-dir=/opt/mysql/mysql8/>

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

从上面可以看到服务文件为/etc/systemd/system/mysql.service

停止服务

# systemctl stop mysql.service
# systemctl disable mysql.service  此处不用执行,服务是disable

注意:systemctl disable xxx表示禁止开机启动服务,这个视情况而定.不是必须设置的选项.

# cd /etc/systemd/system/
# cp -rp mysql.service mysqld.service
# rm /etc/systemd/system/mysql.service
rm: remove regular file '/etc/systemd/system/mysql.service'? y

可以修改相关配置信息. 当然这里也可以删除mysql.service,创建一个新的服务文件.

刷新 systemd 配置

# systemctl daemon-reload

检查验证

# systemctl list-unit-files --type=service | grep mysql
mysql.service                              generated      
mysqld.service                             disabled       
mysqlrouter.service                        enabled    
    
# systemctl status mysql.service
● mysql.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysql; generated)
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)

Mar 11 15:24:35 mydev02 systemd[1]: Stopping MySQL Server...
Mar 11 15:24:56 mydev02 mysqld_safe[1366406]: 2026-03-11T07:24:56.599266Z mysqld_safe mysqld from pid file /data/mysql/mydev02.pid ended
Mar 11 15:24:56 mydev02 systemd[1]: mysql.service: Succeeded.
Mar 11 15:24:56 mydev02 systemd[1]: Stopped MySQL Server.
# ls /etc/rc.d/init.d/mysql
/etc/rc.d/init.d/mysql
# rm /etc/rc.d/init.d/mysql
rm: remove regular file '/etc/rc.d/init.d/mysql'? y
# systemctl daemon-reload
# systemctl list-unit-files --type=service | grep mysql
mysqld.service                             disabled       
mysqlrouter.service                        enabled    

启用服务mysqld.service

# systemctl enable mysqld.service  
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /etc/systemd/system/mysqld.service.
# systemctl daemon-reload
# systemctl start mysqld.service