문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판이전 판다음 판 | 이전 판 | ||
| postgresql:replication [2024/07/29 02:01] – [PostgreSQL Replication] taekgu | postgresql:replication [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1 | ||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| + | ======= PostgreSQL Replication ======= | ||
| + | |||
| + | 자! 서버복제를 구성하여 보자! 어떻게 되는지 하나 하나 알아 보자고요. | ||
| + | |||
| + | 여러가지 방법이 있습니다. 분석을 해야하나 장단점이 있는데 좀 알아보고 맞는 것 찾아봐야 겠습니다. | ||
| + | |||
| + | WAL을 넘겨서 Standby Server가 Active Server의 자료와 동기화하는방법이 있고, 로지컬로 변경분을 전송하는 방법이 있다. | ||
| + | ===== WAL ===== | ||
| + | |||
| + | PostgreSQL에서 제공하는 복제 서버 구축 방식을 요약하면 다음과 같다. | ||
| + | |||
| + | * 마스터 서버에서 발생하는 모든 작업을 로그로 만든다. | ||
| + | * 이 로그를 스탠바이 서버(들)로 전달한다. | ||
| + | * 스탠바이 서버(들)에서 받은 로그를 복원(재실행)한다. | ||
| + | * 이렇게 하면 마스터 서버와 같은 스키마/ | ||
| + | |||
| + | |||
| + | ===== Log Shipping ===== | ||
| + | WAL파일을 전송하면 Standby Server에서 복구방식으로 Database를 동기화 한다. | ||
| + | |||
| + | Stream 방식도 별도의 설명이 있는 것같은데 archive_timeout시간을 main서버에서 조절가능하다.(초단위) | ||
| + | Main Server의 부담이 있다고 한다. Standby Server가 다운되었을 경우 오버헤드가 있을 수 있다. | ||
| + | |||
| + | 본 설정은 Ubuntu에서 postgresql-16으로 하였다. | ||
| + | {다른 OS(Debian)과 묶어 보려하였으나 postgresql의 빌드버전차이로 Debian과는 연결을 못 하였다.} | ||
| + | |||
| + | PostgreSQL이 설치된 Platform이 다르다면 Replication 불가능합니다. 또한 Major Version은 동일해야 합니다. | ||
| + | |||
| + | 클러스터의 물리적인 복제이기 때문에 동일한 pg 버전 간의 복제만 가능합니다. | ||
| + | |||
| + | 아래와 같은 순서로 진행한다. | ||
| + | - scp를 위한 key준비 ( Main Server에서 Standby Server로 ) | ||
| + | - WAL파일을 위한 Archive파일 전송하기 위한 path 구성 | ||
| + | - Main Server 설정 | ||
| + | - Data Backup 및 Restore | ||
| + | - Standby Server 설정 | ||
| + | |||
| + | ==== 1.scp 준비 ==== | ||
| + | public key 생성 | ||
| + | |||
| + | <code bash> | ||
| + | # Main Server에서 | ||
| + | postgres@main-server: | ||
| + | |||
| + | # .ssh/ | ||
| + | postgres@main-server: | ||
| + | # public key를 복사 | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | # Standby Server에서 | ||
| + | postgres@standby-server: | ||
| + | postgres@standby-server: | ||
| + | # 복사한 main server의 public key를 authorized_keys에 붙여넣기를 한다. | ||
| + | </ | ||
| + | |||
| + | ==== 2.WAL파일을 위한 Archive파일 전송하기 위한 path 구성 ==== | ||
| + | Standby Server의 Archive파일를 받기위한 Directory생성 | ||
| + | |||
| + | <code bash> | ||
| + | # Standby Server | ||
| + | postgres@standby-server: | ||
| + | </ | ||
| + | |||
| + | ==== 3.Main Server 설정 ==== | ||
| + | |||
| + | postgresql.conf 파일수정 | ||
| + | postgresql 16버전기준 설정파일위치 | ||
| + | / | ||
| + | < | ||
| + | wal_level = replica | ||
| + | archive_mode = on | ||
| + | archive_command = 'scp %p postgres@standby-server:/ | ||
| + | archive_timeout = 30 # force a WAL file switch after this | ||
| + | max_wal_senders = 10 # max number of walsender process | ||
| + | </ | ||
| + | * archive_timeout : sec / WAL File(16MB)이 가득 차지 않아도 30초마다 WAL File을 Switching) | ||
| + | |||
| + | ==== 4.Data Backup 및 Restore ==== | ||
| + | |||
| + | Main Server를 stop & start | ||
| + | |||
| + | data backup | ||
| + | <code bash> | ||
| + | # dibian인 경우 / | ||
| + | cd / | ||
| + | tar -cvf Data.tar main | ||
| + | </ | ||
| + | |||
| + | Data.tar를 Standby Server로 복사하고 압축해제 | ||
| + | |||
| + | <code bash> | ||
| + | tar -xvf Data.tar | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | touch standby.signal | ||
| + | </ | ||
| + | |||
| + | Standby Server에는 standby.signal 파일이 존재해야한다. | ||
| + | |||
| + | ==== 3.Standby Server 설정 ==== | ||
| + | |||
| + | < | ||
| + | restore_command = 'test -f / | ||
| + | archive_cleanup_command = ' | ||
| + | hot_standby = on # " | ||
| + | </ | ||
| + | |||
| + | DB가 Standby 상태로 스트리밍 복제가 이루어지는 Slave노드에는 반드시 on으로 설정되어 있어야 한다. | ||
| + | ===== Stream Replication ===== | ||
| + | |||
| + | ====== Logical Replication ====== | ||
| + | |||
| + | Logical은 DDL문장이 아닌 것들에 대해서 지정이 가능하고, | ||
| + | |||
| + | <code > | ||
| + | CREATE ROLE repluser REPLICATION LOGIN PASSWORD ' | ||
| + | |||
| + | mama계정으로 접속해야죠 | ||
| + | |||
| + | CREATE PUBLICATION maro_publication FOR ALL TABLES; | ||
| + | |||
| + | GRANT USAGE ON SCHEMA maro_schema TO repluser; | ||
| + | |||
| + | REVOKE USAGE ON SCHEMA maro_schema FROM repluser; | ||
| + | |||
| + | GRANT SELECT ON ALL TABLES IN SCHEMA maro_schema TO repluser; | ||
| + | |||
| + | REVOKE SELECT ON ALL TABLES IN SCHEMA maro_schema FROM repluser; | ||
| + | |||
| + | CREATE SUBSCRIPTION maro_subscription CONNECTION ' | ||
| + | |||
| + | DROP SUBSCRIPTION maro_subscription; | ||
| + | |||
| + | \dRp, \dRs | ||
| + | |||
| + | alias padm=' | ||
| + | |||
| + | </ | ||