MySQL 이중화 = Replication
Binary Log(binlog) 기반으로 데이터 변경사항 전달
핵심 포인트
- 고가용성(HA) ≠ 자동 장애조치
- 부하 분산, 데이터 보호, 읽기 확장

[ Client ]
|
v
[ Primary ]
- binlog 기록
|
v
[ Replica ]
- relay log
- read only
auto_increment_offset)| 서버 | 역할 | IP |
|---|---|---|
| ubuntu-svr1 | Primary | 192.168.80.110 |
| ubuntu-svr2 | Replica | 192.168.80.120 |
sudo apt update
sudo apt install mysql-server
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
# bind-address = 127.0.0.1 -> 0.0.0.0 로 변경(또는 주석)
sudo systemctl restart mysql
'바이너리 로그(Binary Log)'를 활성화하고, '복제(Replication)' 기능을 사용하기 위한 설정
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
server-id=1
log-bin=/var/log/mysql/mysql-bin.log
binlog_format=ROW
sudo systemctl restart mysql
CREATE USER 'repl'@'%' IDENTIFIED BY 'repl1234';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
FLUSH PRIVILEGES;
SHOW MASTER STATUS;
출력 예시:
File: mysql-bin.000001
Position: 1306
이 값이 Replica 서버 설정에 필요
caching_sha2_password 방식 -> mysql_native_password 방식
SELECT user, host, plugin
FROM mysql.user
WHERE user='repl';
-- Primary
ALTER USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY 'repl1234';
FLUSH PRIVILEGES;
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
server-id=2
relay-log=relay-bin
read_only=ON
sudo systemctl restart mysql
sudo mysql
CHANGE MASTER TO
MASTER_HOST='192.168.80.110',
MASTER_USER='repl',
MASTER_PASSWORD='repl1234',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=1306;
Primary server의binlog파일 위치와 일치 시켜야함!!
START REPLICA;
SHOW REPLICA STATUS\G
아래 두 값이 Yes여야 정상
Replica_IO_Running: Yes
Replica_SQL_Running: Yes
CREATE DATABASE repl_test;
USE repl_test;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50)
);
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
USE repl_test;
SELECT * FROM users;
STOP SLAVE;
SET GLOBAL read_only = OFF;
이게 MySQL Replication의 한계
REPLICA 동작 중지 및 REPLICA 설정 초기화
REPLICA STOP;
RESET REPLICA ALL;


Client
|
[ HAProxy ]
├── Write → Primary
└── Read → Replicas
PostgreSQL 이중화의 목적은 MySQL과 동일.
PostgreSQL의 핵심 특징은 WAL (Write-Ahead Log) 기반 복제.
PostgreSQL의
Standby 서버역시 Read Only이다.
| 구분 | 설명 |
|---|---|
| Streaming Replication | WAL을 실시간으로 전송 |
| Logical Replication | 테이블 단위 논리 복제 |
| File-based Replication | WAL 아카이빙 |
| 동기 / 비동기 | Commit 시점 제어, 비동기는 Primary가 Standby commit유무와 상관없이 commit |

[ Primary ]
│ WAL
▼
[ Standby ]
📌 MySQL Binary Log 복제와 개념적으로 동일
- 비동기 설정
# postgresql.conf (Primary)
# synchronous_commit = on
or
synchronous_commit = off
- 동기 설정
# postgresql.conf (Primary)
synchronous_commit = on
synchronous_standby_names = 'standby1'
-- (192.168.80.130) --
node1 : Primary
-- (192.168.80.110) --
node2 : Standby
sudo apt update
sudo apt install -y postgresql
# /etc/postgresql/14/main/postgresql.conf
listen_addresses = '*'
wal_level = replica
max_wal_senders = 10
wal_keep_size = 64MB
sudo -iu postgres psql
CREATE ROLE repl WITH REPLICATION LOGIN PASSWORD 'replpass';
# /etc/postgresql/14/main/pg_hba.conf
host replication repl 192.168.80.0/24 scram-sha-256
sudo systemctl restart postgresql
sudo systemctl stop postgresql
sudo rm -rf /var/lib/postgresql/14/main/*
sudo -i
pg_basebackup \
-h 192.168.80.130 \
-D /var/lib/postgresql/14/main \
-U repl \
-Fp -Xs -P -R
📌 -R 옵션 → 자동으로 standby.signal 생성
chown -R postgres:postgres /var/lib/postgresql/14/main/*
/var/lib/postgresql/14/main/디렉토리의 모든 파일/디렉토리의 소유자/소유그룹이 postgres로 되어있어야 한다.
sudo systemctl start postgresql
SELECT client_addr, state, sync_state
FROM pg_stat_replication;

SELECT pg_is_in_recovery();
true → Standby 정상
sudo pg_ctlcluster 14 main promote

Primary로 승격이 되면쓰기가 가능해진다.
| 항목 | MySQL | PostgreSQL |
|---|---|---|
| 로그 | Binary Log (bin) | WAL |
| 기본 복제 | 비동기 | 비동기 |
| 동기 복제 | 제한적 | 내장 지원 |
| Replica 읽기 | 가능 | 가능 |
| 승격 | RESET SLAVE | pg_ctl promote |