Công nghệ IT
Cấu hình Logical Replication cho Postgres RDS AWS


Thoại Kỳ
01-01-2025
85 lượt xem
Cấu hình Logical Replication cho Postgres RDS AWS
Cơ sở dữ liệu nhân bản chính
1.Tạo Nhóm Tham số Tùy chỉnh:
aws rds create-db-parameter-group \
--db-parameter-group-name my-custom-pg \
--db-parameter-group-family postgres16 \
--description "Custom parameter group for logical replication"
2.Sửa đổi Nhóm Tham số Tùy chỉnh:
aws rds modify-db-parameter-group \
--db-parameter-group-name my-custom-pg \
--parameters "ParameterName=rds.logical_replication,ParameterValue=1,ApplyMethod=pending-reboot" \
"ParameterName=max_replication_slots,ParameterValue=10,ApplyMethod=pending-reboot" \
"ParameterName=max_wal_senders,ParameterValue=11,ApplyMethod=pending-reboot" \
"ParameterName=max_logical_replication_workers,ParameterValue=10,ApplyMethod=pending-reboot" \
"ParameterName=max_worker_processes,ParameterValue=10,ApplyMethod=pending-reboot"
Lệnh này sửa đổi nhóm tham số tùy chỉnh để bật sao chép logic và cấu hình các tham số liên quan:
- rds.logical_replication: Đặt thành 1 để bật sao chép logic.
- max_replication_slots: Đặt thành 10 để cho phép tối đa 10 khe sao chép.
- max_wal_senders: Đặt thành 11 để cho phép tối đa 11 quy trình gửi WAL.
- max_logical_replication_workers: 10 worker sao chép logic.
- max_worker_processes: 10 quy trình worker tổng cộng (bao gồm cả các worker sao chép logic).
3.Tạo Instance Cơ sở dữ liệu Chính:
aws rds create-db-instance \
--db-instance-identifier primary-db \
--db-instance-class db.t3.micro \
--engine postgres \
--engine-version 16.3 \
--allocated-storage 20 \
--master-username thoaiky1992 \
--master-user-password thoaiky1992 \
--vpc-security-group-ids sg-xxxxxx \
--db-subnet-group-name post_16_sg \
--db-parameter-group-name my-custom-pg \
--publicly-accessible \
--backup-retention-period 7 \
--auto-minor-version-upgrade \
--storage-type gp2
Lệnh này tạo một instance PostgreSQL RDS mới với cấu hình được chỉ định:
- Identifier: primary-db
- Instance Class: db.t3.micro
- Engine Version: 16.3
- Allocated Storage: 20 GB
- Master Username/Password: thoaiky1992
- VPC Security Group: sg-xxxx
- DB Subnet Group: post_16_sg
- Publicly Accessible
- Backup Retention Period: 7 ngày
- Auto Minor Version Upgrade
- Storage Type: gp2
4. Khởi động lại Cơ sở dữ liệu Chính:
aws rds reboot-db-instance --db-instance-identifier primary-db
Lệnh này khởi động lại cơ sở dữ liệu chính để áp dụng các thay đổi tham số.
5.Cấu hình Sao chép Logic trong PostgreSQL:
psql -h <primary-db-endpoint> -U masteruser -d postgres -W
DROP PUBLICATION my_publication;
CREATE PUBLICATION my_publication FOR ALL TABLES;
SELECT * FROM pg_publication_tables WHERE pubname = 'my_publication';
Các lệnh này được thực thi trong shell PostgreSQL:
- Kết nối đến cơ sở dữ liệu chính.
- Xóa bất kỳ publication nào có tên my_publication.
- Tạo một publication mới tên my_publication cho tất cả các bảng.
- Xác minh publication bằng cách truy vấn pg_publication_tables.
Cơ sở dữ liệu Sao chép Logic
1.Tạo Instance Cơ sở dữ liệu Replica:
aws rds create-db-instance \
--db-instance-identifier replica-3-db \
--db-instance-class db.t3.micro \
--engine postgres \
--engine-version 16.3 \
--allocated-storage 20 \
--master-username thoaiky1992 \
--master-user-password thoaiky1992 \
--vpc-security-group-ids sg-0e490b23f460ddf59 \
--db-subnet-group-name post_16_sg \
--publicly-accessible \
--backup-retention-period 7 \
--auto-minor-version-upgrade \
--storage-type gp2 \
--db-parameter-group-name my-custom-pg
Lệnh này tạo một instance PostgreSQL RDS mới với cấu hình được chỉ định cho replica. Lưu ý rằng nó sử dụng cùng một nhóm tham số tùy chỉnh như cơ sở dữ liệu chính.
2.Cấu hình Sao chép Logic trong PostgreSQL:
psql -h <replica-db-endpoint> -U masteruser -d postgres -W
SHOW wal_level;
SHOW max_replication_slots;
SHOW max_wal_senders;
SHOW rds.logical_replication;
SELECT * FROM pg_stat_subscription;
- Lưu ý: Đảm bảo rằng tất cả các schema đều được tạo trên cơ sở dữ liệu replica như cần thiết.
DROP SUBSCRIPTION IF EXISTS my_subscription;
CREATE SUBSCRIPTION my_subscription
CONNECTION 'host=<primary-db-endpoint> dbname=postgres user=masteruser password=masterpassword'
PUBLICATION my_publication
WITH (copy_data = true);
Các lệnh này được thực thi trong shell PostgreSQL:
- Kết nối đến cơ sở dữ liệu replica.
- Tạo một subscription tên my_subscription đến publication my_publication trên cơ sở dữ liệu chính.
- Xóa bất kỳ subscription nào có tên my_subscription.
- Hiển thị các thiết lập liên quan đến sao chép để xác minh cấu hình của chúng.
- Truy vấn pg_stat_subscription để xác minh trạng thái của subscription.
Bài viết khác