Database/MongoDB

systemctl로 mongodb 서비스모드로 시작하기

BabyTT 2019. 6. 11. 13:50

MongoDB를 systemctl 서비스모드로 시작하기 

 

mongodb.service 파일을 생성

$sudo vim /lib/systemd/system/mongodb.service

[Unit]
Description=mongodb
After=network.target

[Service]
User=root
Group=root
LimitNOFILE=
LimitNPROC=
ExecStart=/home/mongodb/mongodb/bin/mongod --config /etc/mongodb.conf

[Install]
WantedBy=multi-user.target

 

생성된 파일을 symlink 등록

$sudo ln -s mongodb.service /etc/systemd/system/mongodb.service

 

persistent service 등록

$systemctl enable mongodb.service

위 명령어를 실행하면 /etc/systemd/system/multi-user.target.wants에 symbolic link가 생성된다.

 

 

서비스 등록 내역 확인

$systemctl show --property "Wants" multi-user.target | fmt -10 | sed 's/Wants=//g' | sort

 

mongodb.service daemon 리로드 후 시작

$sudo systemctl daemon-reload
$sudo systemctl start mongodb.service

 

mongodb 서비스를 fork 로 시작하기

  1. mongod.conf 에 등록
  2. mongod --fork 모드로 실행
  3. systemd 로 실행

 

Ansible로 등록하기

- name: Add systemd configuration if present
  template: src=mongodb.service.j2 dest=/lib/systemd/system/mongodb.service owner=mongodb group=mongodb mode=0644
  when: mongodb_is_systemd and is_mongos == 'false'

- name: Add symlink from systemd
  file: src=/lib/systemd/system/mongodb.service dest=/etc/systemd/system/multi-user.target.wants/mongodb.service state=link
  when: mongodb_is_systemd and is_mongos == 'false'
  notify: reload systemd
  
- name: reload systemd
  systemd:
    name: "{{ mongodb_daemon_name }}"
    daemon-reload: yes
    enabled: yes
  when: is_mongos == 'false'

- mongodb.service.j2 파일은 첫 줄 mongodb.service 파일과 동일하고 확장자만 jinja2로 설정

'Database > MongoDB' 카테고리의 다른 글

MongoDB 영속성 (Persistence)  (0) 2019.06.06
MongoDB Log rotation 설정  (0) 2019.06.03
MongoDB Max connection 변경 (with Ansible)  (0) 2019.05.31