카이도스의 Tech Blog

Mongodb Replica set 설치 - 7.0 본문

DB

Mongodb Replica set 설치 - 7.0

카이도스 2024. 2. 4. 14:18
728x90
반응형

Mongodb 설치 - 7.0

# 사전작업
## 호스트네임 변경
sudo hostnamectl set-hostname dev-mongo
sudo hostnamectl set-hostname dev-mongo2
sudo hostnamectl set-hostname dev-mongo3

## 디스크 작업
sudo su -
lsblk
fdisk /dev/sdb
mkfs.xfs /dev/sdb1
mkdir -p /data
mount /dev/sdb1 /data
df -Th
....
/dev/sdb1      xfs    500G  3.6G  497G   1% /data

## 아래 내용 추가 후 리부팅 및 확인
vi /etc/fstab
/dev/sdb1 /data xfs defaults 0 1
reboot

# 커널 설정 (/etc/sysctl.conf)
sudo su -
cat <<EOT>> /etc/sysctl.conf
vm.overcommit_memory=2
vm.swappiness=1
vm.overcommit_ratio=90

net.core.netdev_max_backlog=30000
net.ipv4.tcp_max_syn_backlog=30000

net.ipv4.tcp_syn_retries=2
net.ipv4.tcp_retries1=2

net.core.rmem_max=268435456
net.core.wmem_max=268435456
net.core.rmem_default=10485760
net.core.wmem_default=10485760
net.ipv4.tcp_rmem=4096 87380 134217728
net.ipv4.tcp_wmem=4096 87380 134217728

net.core.somaxconn=65535
net.ipv4.tcp_fin_timeout=12
vm.max_map_count=262144
net.ipv4.ip_local_port_range=1024 61000
net.ipv4.tcp_max_tw_buckets=540000
EOT

# 반영
sysctl -p

# Limits 설정 (/etc/security/limits.conf)
cat <<EOT>> /etc/security/limits.conf
*         hard    nofile      1000000
*         soft    nofile      1000000
root      hard    nofile      1000000
root      soft    nofile      1000000
EOT
------------------------------------------------------------------------------------------------------------------------------
# 설치
## 몽고디비 7.0 공용 키 다운
curl -fsSL https://pgp.mongodb.com/server-7.0.asc |    sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg    --dearmor

## 몽고디비 리스트 파일 생성
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

## 패키지 내려받기
sudo apt update && sudo apt install -y mongodb-org

## 디렉터리 변경
sudo cp -av /var/lib/mongodb /data/
sudo vi /etc/mongod.conf
storage:
  dbPath: /data/mongodb
....
  bindIp: 0.0.0.0

## 시스템 데몬을 다시 로딩 후 몽고디비 서비스 실행
sudo systemctl daemon-reload && sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod

Replica set 설정

## 마스터에서 진행
mongosh
test> use admin
switched to db admin
admin> db.createUser({user: 'admin', pwd: 'admin', roles: [{ role: 'root', db: 'admin' }]});
{ ok: 1 }
admin> show users
[
  {
    _id: 'admin.admin',
    userId: new UUID("47282e78-8595-48ab-8294-6fb7d409d27d"),
    user: 'admin',
    db: 'admin',
    roles: [ { role: 'root', db: 'admin' } ],
    mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
  }
]

# 프로세스 종료
sudo systemctl stop mongod.service
sudo systemctl status mongod.service

# Security Auth 설정(마스터에서)
sudo su -
openssl rand -base64 756 > /etc/mongodb.key
chmod 400 /etc/mongodb.key
chown mongodb:mongodb /etc/mongodb.key

# 레플리카에서 아래 진행
# Security Auth 설정 (Master Key 복사 후 동일 Key 값으로 설정)
sudo vi /etc/mongodb.key
sudo chmod 400 /etc/mongodb.key
sudo chown mongodb:mongodb /etc/mongodb.key

# config 설정
sudo vi /etc/mongod.conf
----------------------------------------
# Where and how to store data.
storage:
  dbPath: /data/mongodb
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  keyFile: "/etc/mongodb.key"
  clusterAuthMode: "keyFile"
  authorization : "enabled"

replication:
  replSetName: "repl0"
----------------------------------------
# 프로세스 시작
sudo systemctl start mongod.service
sudo systemctl status mongod.service

## 마스터에서 진행
mongosh -u admin -p 'admin'
> use admin
> rs.initiate()
> rs.add({ host: "IP2:27017" });
> rs.add({ host: "IP3:27017" });
> rs.status()
{
  set: 'repl0',
  date: ISODate("2023-10-19T10:10:35.278Z"),
  myState: 1,
  term: Long("1"),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 1,
  writeMajorityCount: 1,
  votingMembersCount: 1,
  writableVotingMembersCount: 1,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1697710232, i: 1 }), t: Long("1") },
    lastCommittedWallTime: ISODate("2023-10-19T10:10:32.899Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1697710232, i: 1 }), t: Long("1") },
    appliedOpTime: { ts: Timestamp({ t: 1697710232, i: 1 }), t: Long("1") },
    durableOpTime: { ts: Timestamp({ t: 1697710232, i: 1 }), t: Long("1") },
    lastAppliedWallTime: ISODate("2023-10-19T10:10:32.899Z"),
    lastDurableWallTime: ISODate("2023-10-19T10:10:32.899Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1697710169, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate("2023-10-19T10:08:42.877Z"),
    electionTerm: Long("1"),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1697710122, i: 1 }), t: Long("-1") },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1697710122, i: 1 }), t: Long("-1") },
    numVotesNeeded: 1,
    priorityAtElection: 1,
    electionTimeoutMillis: Long("10000"),
    newTermStartDate: ISODate("2023-10-19T10:08:42.890Z"),
    wMajorityWriteAvailabilityDate: ISODate("2023-10-19T10:08:42.898Z")
  },
  members: [
    {
      _id: 0,
      name: 'IP1:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 217,
      optime: { ts: Timestamp({ t: 1697710232, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-10-19T10:10:32.000Z"),
      lastAppliedWallTime: ISODate("2023-10-19T10:10:32.899Z"),
      lastDurableWallTime: ISODate("2023-10-19T10:10:32.899Z"),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: 'Could not find member to sync from',
      electionTime: Timestamp({ t: 1697710122, i: 2 }),
      electionDate: ISODate("2023-10-19T10:08:42.000Z"),
      configVersion: 3,
      configTerm: 1,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'IP2:27017',
      health: 1,
      state: 0,
      stateStr: 'STARTUP',
      uptime: 46,
      optime: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
      optimeDurable: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
      optimeDate: ISODate("1970-01-01T00:00:00.000Z"),
      optimeDurableDate: ISODate("1970-01-01T00:00:00.000Z"),
      lastAppliedWallTime: ISODate("1970-01-01T00:00:00.000Z"),
      lastDurableWallTime: ISODate("1970-01-01T00:00:00.000Z"),
      lastHeartbeat: ISODate("2023-10-19T10:10:35.218Z"),
      lastHeartbeatRecv: ISODate("1970-01-01T00:00:00.000Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      configVersion: -2,
      configTerm: -1
    },
    {
      _id: 2,
      name: 'IP3:27017',
      health: 1,
      state: 0,
      stateStr: 'STARTUP',
      uptime: 34,
      optime: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
      optimeDurable: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
      optimeDate: ISODate("1970-01-01T00:00:00.000Z"),
      optimeDurableDate: ISODate("1970-01-01T00:00:00.000Z"),
      lastAppliedWallTime: ISODate("1970-01-01T00:00:00.000Z"),
      lastDurableWallTime: ISODate("1970-01-01T00:00:00.000Z"),
      lastHeartbeat: ISODate("2023-10-19T10:10:35.218Z"),
      lastHeartbeatRecv: ISODate("1970-01-01T00:00:00.000Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      configVersion: -2,
      configTerm: -1
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1697710232, i: 1 }),
    signature: {
      hash: Binary.createFromBase64("jSh/v0wCRGn60w9UjCTyW/oRLQI=", 0),
      keyId: Long("7291609452078170118")
    }
  },
  operationTime: Timestamp({ t: 1697710232, i: 1 })
}

# Replicas Member Name 변경 (Hostname -> IP)
> cfg = rs.conf()
> cfg.members[0].host = "IP1:27017"
> rs.reconfig(cfg)
> rs.status()
{
  set: 'repl0',
  date: ISODate("2023-10-19T10:13:52.459Z"),
  myState: 1,
  term: Long("1"),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1697710427, i: 1 }), t: Long("1") },
    lastCommittedWallTime: ISODate("2023-10-19T10:13:47.521Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1697710427, i: 1 }), t: Long("1") },
    appliedOpTime: { ts: Timestamp({ t: 1697710427, i: 1 }), t: Long("1") },
    durableOpTime: { ts: Timestamp({ t: 1697710427, i: 1 }), t: Long("1") },
    lastAppliedWallTime: ISODate("2023-10-19T10:13:47.521Z"),
    lastDurableWallTime: ISODate("2023-10-19T10:13:47.521Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1697710412, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate("2023-10-19T10:08:42.877Z"),
    electionTerm: Long("1"),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1697710122, i: 1 }), t: Long("-1") },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1697710122, i: 1 }), t: Long("-1") },
    numVotesNeeded: 1,
    priorityAtElection: 1,
    electionTimeoutMillis: Long("10000"),
    newTermStartDate: ISODate("2023-10-19T10:08:42.890Z"),
    wMajorityWriteAvailabilityDate: ISODate("2023-10-19T10:08:42.898Z")
  },
  members: [
    {
      _id: 0,
      name: 'IP1:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 414,
      optime: { ts: Timestamp({ t: 1697710427, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-10-19T10:13:47.000Z"),
      lastAppliedWallTime: ISODate("2023-10-19T10:13:47.521Z"),
      lastDurableWallTime: ISODate("2023-10-19T10:13:47.521Z"),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1697710122, i: 2 }),
      electionDate: ISODate("2023-10-19T10:08:42.000Z"),
      configVersion: 6,
      configTerm: 1,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'IP2:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 243,
      optime: { ts: Timestamp({ t: 1697710427, i: 1 }), t: Long("1") },
      optimeDurable: { ts: Timestamp({ t: 1697710427, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-10-19T10:13:47.000Z"),
      optimeDurableDate: ISODate("2023-10-19T10:13:47.000Z"),
      lastAppliedWallTime: ISODate("2023-10-19T10:13:47.521Z"),
      lastDurableWallTime: ISODate("2023-10-19T10:13:47.521Z"),
      lastHeartbeat: ISODate("2023-10-19T10:13:51.526Z"),
      lastHeartbeatRecv: ISODate("2023-10-19T10:13:51.525Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '10.10.16.159:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 6,
      configTerm: 1
    },
    {
      _id: 2,
      name: 'IP3:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 231,
      optime: { ts: Timestamp({ t: 1697710427, i: 1 }), t: Long("1") },
      optimeDurable: { ts: Timestamp({ t: 1697710427, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-10-19T10:13:47.000Z"),
      optimeDurableDate: ISODate("2023-10-19T10:13:47.000Z"),
      lastAppliedWallTime: ISODate("2023-10-19T10:13:47.521Z"),
      lastDurableWallTime: ISODate("2023-10-19T10:13:47.521Z"),
      lastHeartbeat: ISODate("2023-10-19T10:13:51.524Z"),
      lastHeartbeatRecv: ISODate("2023-10-19T10:13:51.524Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '10.10.16.159:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 6,
      configTerm: 1
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1697710427, i: 1 }),
    signature: {
      hash: Binary.createFromBase64("ILyNB0MnY3xN7+5hipb8v7uDM30=", 0),
      keyId: Long("7291609452078170118")
    }
  },
  operationTime: Timestamp({ t: 1697710427, i: 1 })
}

 

2024.02.04 - [DB] - Mongodb Replica set 설치 - 7.0 - 복제 확인 간단 테스트

 

Mongodb Replica set 설치 - 7.0 - 복제 확인 간단 테스트

2024.02.04 - [분류 전체보기] - Mongodb Replica set 설치 - 7.0 - 복제 확인 간단 테스트 복제 확인 간단 테스트 IP1 # 접속 mongosh -u admin -p admin # db 확인 repl0 [direct: primary] test> show dbs admin 172.00 KiB config 212.00 K

djdakf1234.tistory.com

2024.02.04 - [DB] - Mongodb Replica set 설치 - 7.0 - Failover 테스트

 

Mongodb Replica set 설치 - 7.0 - Failover 테스트

2024.02.04 - [DB] - Mongodb Replica set 설치 - 7.0 Mongodb Replica set 설치 - 7.0 Mongodb 설치 - 7.0 # 사전작업 ## 호스트네임 변경 sudo hostnamectl set-hostname dev-mongo sudo hostnamectl set-hostname dev-mongo2 sudo hostnamectl set-hostna

djdakf1234.tistory.com

 

728x90
반응형

'DB' 카테고리의 다른 글

Mongodump, restore  (0) 2024.02.04
Mongodb Replica set 설치 - 7.0 - Failover 테스트  (0) 2024.02.04
Mongodb Replica set 설치 - 7.0 - 복제 확인 간단 테스트  (0) 2024.02.04
Mongodb 설치 - 6.0  (0) 2024.02.04
PostgreSQL M/R 구성 셋팅  (0) 2023.03.08
Comments