美文网首页
08-MongoDB集群运维-服务开机自启

08-MongoDB集群运维-服务开机自启

作者: 过桥 | 来源:发表于2019-11-05 16:13 被阅读0次

环境

操作系统:CentOS 7
mongodb程序路径:/home/mongodb/software/mongodb-linux-x86_64-rhel70-4.2.1
mongodb日志文件路径:/opt/mongo/logs/

新建服务文件

/lib/systemd/system/下新建mongodb.service文件

[mongodb@mongodb01 ~]$ sudo vim /lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/home/mongodb/software/mongodb-linux-x86_64-rhel70-4.2.1/bin/mongod --config /opt/mongo/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/home/mongodb/software/mongodb-linux-x86_64-rhel70-4.2.1/bin/mongod --shutdown --config /opt/mongo/mongo.conf
PrivateTmp=true
[Install] 
WantedBy=multi-user.target

需注意修改代码中mongodmongo.conf路径

设置文件权限

sudo chmod 754 /lib/systemd/system/mongodb.service

读写运行权限赋予文件所有者,读运行的权限赋予群组用户,读的权限赋予其他用户

设置开机启动

如果本机MongoDB正在运行,先停止,再依次执行命令测试

# 设置启动
sudo systemctl start mongodb.service
# 设置关闭
sudo systemctl stop mongodb.service
# 设置开机自启
sudo systemctl enable mongodb.service

重启验证

[mongodb@mongodb01 ~]$ sudo reboot
# 登录验证
[C:\~]$ ssh mongodb@192.168.153.128


Connecting to 192.168.153.128:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Tue Nov  5 11:36:48 2019 from 192.168.153.1
[mongodb@mongodb01 ~]$ sudo netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      1456/mongod         
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1411/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1175/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1171/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1420/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      1973/sshd: mongodb@ 
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1175/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1171/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1420/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      1973/sshd: mongodb@ 
[mongodb@mongodb01 ~]$ 

可能遇到问题

[mongodb@mongodb03 ~]$ sudo systemctl start mongodb.service
Warning: mongodb.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[mongodb@mongodb01 ~]$ sudo systemctl start mongodb.service
Job for mongodb.service failed because the control process exited with error code. See "systemctl status mongodb.service" and "journalctl -xe" for details.
[mongodb@mongodb01 ~]$ 

问题原因零

代码拷贝不完全,可通过cat /lib/systemd/system/mongodb.service查看

问题原因一

服务正在运行,需先停止,注意随时查看正在运行服务,sudo netstat -ntlp,注意关闭或重启服务

问题原因二

文件中启动命令路径有误,重点查看ExecStart对应命令,单独执行看是否成功,ExecReloadExecStop非必须

相关文章

网友评论

      本文标题:08-MongoDB集群运维-服务开机自启

      本文链接:https://www.haomeiwen.com/subject/lqcebctx.html