[MySQL 8.0 매뉴얼 읽기] MySQL DB를 systemd 이용해서 시작하기

MySQL

MySQL DB를 바이너리 배포판으로 설치하게 되면, 직접 커멘드로 DB를 올리고 내리게 된다.
이를 systemctl을 이용해서 간단하게 DB를 startup/shutdown 할 수 있는 방법이 있다.

MySQL :: MySQL Secure Deployment Guide :: 5 Post Installation Setup
MySQL :: MySQL 8.0 Reference Manual :: 2.5.9 Managing MySQL Server with systemd

테스트 환경

  • Amazon Linux2
  • MySQL 8.0.26

systemctl로 mysql 서비스를 사용하는 모습

사전 작업

systemd service 유닛 컨피그 파일 생성하기

shell> cd /usr/lib/systemd/system
shell> touch mysqld.service
shell> chmod 644 mysqld.service

그리고 컨피그 정보를 mysqld.service 파일에 작성해준다.

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

# Have mysqld write its state to the systemd notify socket
Type=notify

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf $MYSQLD_OPTS 

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 10000

Restart=on-failure

RestartPreventExitStatus=1

# Set environment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1

PrivateTmp=false

mysqld 서비스 enable 처리하기

아래와 같이 서비스 enable 처리를 하면, 서버가 아예 재부팅 되더라도 서비스가 자동으로 시작된다.

systemctl enable mysqld.service

systemctl 이용하기

mysqld 서비스 시작하기

systemctl start mysqld

서비스 컨피그 파일을 수정하고 싶다면?

운영하다보면 당연히 서비스 컨피그 파일을 수정해야 할 일이 생길 수 있다.
이 때는 컨피그 파일을 수정해주고, 아래와 같이 데몬 리로드 및 재시작을 해주자.

systemctl daemon-reload
systemctl restart mysqld 

날짜 내용
2021.07.30 최초 글 작성