Linux服务注册,其实我知道是什么意思的,看到这个名字。
不过我想记录一下。以及时间操作。
很多同学第一次接触数据库(MySQL、PostgreSQL、DM 达梦数据库)时,都会遇到一个概念:
注册服务(Register Service)
例如达梦数据库安装完成后,官方文档通常会让我们执行:
./dm_service_installer.sh \
-t dmserver \
-dm_ini /home/dmdba/data/DAMENG/dm.ini \
-p DMSERVER
很多人都会疑惑:
dmserver?本文将通过一个最简单的 HelloWorld 服务,一步一步理解 Linux 服务(systemd)的工作原理。
先来看两个程序。
例如:
python3 hello.py
或者
./dmserver dm.ini
它们都是普通程序。
特点:
例如:
Terminal
│
▼
dmserver
如果服务器重启:
服务器重启
│
▼
dmserver 消失
这显然不适合数据库。
数据库要求:
于是 Linux 提供了:
systemd
它是 Linux 的服务管理器。
所谓注册服务,其实就是:
告诉 systemd:如何启动这个程序。
例如告诉 Linux:
如果有人执行
systemctl start hello
实际上请执行:
/home/user/hello
并且:
这些信息都会写进一个配置文件。
通常位于:
/etc/systemd/system/
例如:
hello.service
这就是 Linux 服务。
例如:
mkdir hello
cd hello
创建:
hello.sh
内容:
#!/bin/bash
while true
do
echo "$(date) Hello Linux Service"
sleep 5
done
赋予权限:
chmod +x hello.sh
测试运行:
./hello.sh
输出:
Thu Jun 19 21:00:01
Hello Linux Service
Thu Jun 19 21:00:06
Hello Linux Service
Ctrl+C 即可退出。
它只是一个普通程序。
现在创建:
/etc/systemd/system/hello.service
内容:
[Unit]
Description=Hello World Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/home/user/hello
ExecStart=/home/user/hello/hello.sh
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
解释一下。
Description=Hello World Service
服务说明。
执行:
systemctl status hello
就会显示:
Hello World Service
After=network.target
表示:
网络启动以后,再启动本服务。
数据库通常都会写:
After=network.target
因为数据库需要监听端口。
这是最重要的部分。
说明:
程序启动后一直运行。
例如:
nginx
redis
dmserver
都属于这种。
指定:
使用哪个用户运行。
也可以:
User=dmdba
数据库一般不会使用 root。
例如 DM:
User=dmdba
工作目录。
例如:
WorkingDirectory=/home/user/hello
程序内部如果读取相对路径:
./config.ini
就是从这里开始找。
真正执行的命令。
例如:
ExecStart=/home/user/hello/hello.sh
执行:
systemctl start hello
实际上就是:
/home/user/hello/hello.sh
数据库也是一样。
例如 DM:
ExecStart=/home/dmdba/dmdbms/bin/dmserver \
/home/dmdba/data/DAMENG/dm.ini
所以:
systemctl start DmServiceDMSERVER
实际上就是:
dmserver dm.ini
如果程序崩溃:
kill -9
systemd 会自动重新启动。
数据库一般都会配置。
新建 service 文件以后:
执行:
sudo systemctl daemon-reload
意思:
重新扫描:
/etc/systemd/system/
否则 Linux 不知道新增了服务。
启动:
sudo systemctl start hello
查看:
systemctl status hello
例如:
hello.service
Active: active (running)
说明:
已经运行。
停止:
systemctl stop hello
再次查看:
inactive(dead)
说明已经退出。
systemctl restart hello
等价于:
stop
↓
start
systemd 最大的优点就是:
统一管理日志。
查看:
journalctl -u hello
实时查看:
journalctl -u hello -f
效果:
Thu Jun 19
Hello Linux Service
Thu Jun 19
Hello Linux Service
数据库同样适用。
例如:
journalctl -u DmServiceDMSERVER
启用:
systemctl enable hello
查看:
systemctl is-enabled hello
输出:
enabled
说明:
服务器重启以后:
自动启动。
取消:
systemctl disable hello
查看:
systemctl list-units --type=service
查看所有:
systemctl list-unit-files
例如:
sshd.service
docker.service
hello.service
DmServiceDMSERVER.service
第一步:
停止:
systemctl stop hello
第二步:
禁止开机启动:
systemctl disable hello
第三步:
删除:
/etc/systemd/system/hello.service
第四步:
刷新:
systemctl daemon-reload
完成。
现在再来看官方命令:
./dm_service_installer.sh \
-t dmserver \
-dm_ini /home/dmdba/data/DAMENG/dm.ini \
-p DMSERVER
实际上它做了下面几件事情。
① 找到数据库:
/home/dmdba/data/DAMENG/dm.ini
↓
② 自动生成:
DmServiceDMSERVER.service
↓
③ 写入:
ExecStart=
/home/dmdba/dmdbms/bin/dmserver \
/home/dmdba/data/DAMENG/dm.ini
↓
④ 注册到:
systemd
↓
以后就可以:
systemctl start DmServiceDMSERVER
而不用:
./dmserver dm.ini
所以:
DM 的"注册服务",本质就是自动帮我们生成一个 systemd 服务。
启动:
systemctl start DmServiceDMSERVER
停止:
systemctl stop DmServiceDMSERVER
重启:
systemctl restart DmServiceDMSERVER
查看状态:
systemctl status DmServiceDMSERVER
查看日志:
journalctl -u DmServiceDMSERVER
实时日志:
journalctl -u DmServiceDMSERVER -f
设置开机启动:
systemctl enable DmServiceDMSERVER
取消开机启动:
systemctl disable DmServiceDMSERVER
可以把 Linux 服务理解成:
systemd 帮你管理程序生命周期。
普通程序:
程序
↓
自己运行
↓
自己停止
Linux 服务:
程序
↓
注册到 systemd
↓
systemctl 管理
↓
支持:
启动
停止
重启
日志
开机启动
自动重启
统一管理
因此,不论是 Nginx、Redis、MySQL,还是 DM 达梦数据库,它们最终都是作为 Linux 服务运行的。理解了 systemd,也就理解了"注册服务"的真正含义。




