본문 바로가기

리눅스

CentOS 7에서 MongoDB Community Server를 설치하는 방법

반응형

CentOS 7에서 MongoDB Community Server를 설치하는 방법

MongoDB 설치

MongoDB의 공식 저장소를 추가하고 패키지 관리자 yum을 사용하여 설치합니다.

 

패키지 다운로드 URL : https://www.mongodb.com/try/download/community

m1

MongoDB(mongod) 서버 설치

MongoDB Community Server를 설치합니다.

yum install https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.6-1.el7.x86_64.rpm
$ yum install https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.6-1.el7.x86_64.rpm
Installing:
 mongodb-org-server

설정 파일에서 `bindIp` 편집

vim /etc/mongod.conf
$ vim /etc/mongod.conf
 26 # network interfaces
 27 net:
 28   port: 27017
 29   bindIp: 0.0.0.0

MongoDB 서비스 시작 및 활성화

MongoDB 서비스를 시작하고 부팅 시 자동으로 시작하도록 활성화합니다.

systemctl enable --now mongod

MongoDB 상태 확인

MongoDB 서비스가 정상적으로 실행 중인지 확인합니다.

systemctl status mongod
$ systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since 금 2021-05-28 09:09:49 KST; 20min ago
     Docs: https://docs.mongodb.org/manual
  Process: 12463 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 12460 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 12457 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 12454 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 12465 (mongod)
    Tasks: 32
   Memory: 173.1M
   CGroup: /system.slice/mongod.service
           └─12465 /usr/bin/mongod -f /etc/mongod.conf

 5월 28 09:09:47 bvm-111 systemd[1]: Starting MongoDB Database Server...
 5월 28 09:09:47 bvm-111 mongod[12463]: about to fork child process, waiting until server is ready for connections.
 5월 28 09:09:47 bvm-111 mongod[12463]: forked process: 12465
 5월 28 09:09:49 bvm-111 mongod[12463]: child process started successfully, parent exiting
 5월 28 09:09:49 bvm-111 systemd[1]: Started MongoDB Database Server.

MongoDB LISTEN 포트 확인

netstat -nlp | grep mongod
$ netstat -nlp | grep mongod
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      12465/mongod
unix  2      [ ACC ]     STREAM     LISTENING     9041377  12465/mongod         /tmp/mongodb-27017.sock
728x90

MongoDB(mongo) 클라이언트 설치

필요한 경우 MongoDB 클라이언트를 설치하여 MongoDB 서버에 연결하고 명령을 실행할 수 있습니다.

yum install -y https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-shell-4.4.6-1.el7.x86_64.rpm
$ yum install -y https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-shell-4.4.6-1.el7.x86_64.rpm
Installing:
 mongodb-org-shell

 

mongo
$ mongo
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("829c87f0-e897-459e-8dce-c761d65c5514") }
MongoDB server version: 4.4.6
---
The server generated these startup warnings when booting:
        2021-05-28T09:09:49.026+09:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-05-28T09:09:49.027+09:00: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl --interleave=all mongod [other options]
        2021-05-28T09:09:49.027+09:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-05-28T09:09:49.027+09:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
> db.version()
4.4.6
> quit()
$

커널 파라미터 설정

The server generated these startup warnings when booting:

/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'

/sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'

 

  • 임시 적용
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
  • 영구 적용
vim /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

 

systemctl restart mongod

 

$ mongo
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("16601d3c-f55d-4f9b-a04f-b59d7a10768c") }
MongoDB server version: 4.4.6
---
The server generated these startup warnings when booting:
        2021-05-28T10:11:24.557+09:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-05-28T10:11:24.559+09:00: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl --interleave=all mongod [other options]
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> quit()

MongoDB Rotate Log Files

/etc/mongod.conf 설정 파일 편집

  • logRotate: reopen 추가
vim /etc/mongod.conf
$ vim /etc/mongod.conf
...
  6 # where to write logging data.
  7 systemLog:
  8   destination: file
  9   logAppend: true
 10   path: /var/log/mongodb/mongod.log
 11   logRotate: reopen
 ...
systemctl status mongod

logrotation 등록

  • mongod 파일 생성
vim /etc/logrotate.d/mongod
$ vim /etc/logrotate.d/mongod
/var/log/mongodb/mongod.log {
	daily
	size 100M
	rotate 10
	missingok
	compress
	delaycompress
	notifempty
	create 640 mongod mongod
	sharedscripts
	postrotate
		/bin/kill -SIGUSR1 `cat /var/run/mongod.pid 2>/dev/null` >/dev/null 2>&1
	endscript
}
  • logrotate 테스트
logrotate -d /etc/logrotate.d/mongod
$ logrotate -d /etc/logrotate.d/mongod
reading config file /etc/logrotate.d/mongod
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/mongodb/mongod.log  104857600 bytes (10 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/mongodb/mongod.log
  log does not need rotating (log size is below the 'size' threshold)
not running postrotate script, since no logs were rotated

 

MongoDB Community Server가 CentOS 7 시스템에 설치되었습니다. 필요한 경우 구성 파일을 수정하고 보안 설정 및 클러스터 구성을 수행할 수 있습니다.

 

참고URL

- https://docs.mongodb.com/v4.4/tutorial/install-mongodb-on-red-hat/

- https://docs.mongodb.com/v4.4/tutorial/rotate-log-files/

- https://www.percona.com/blog/2018/09/27/automating-mongodb-log-rotation/

 

728x90
반응형