-
Notifications
You must be signed in to change notification settings - Fork 0
Postgresql 11 Replication
ต้องการทำข้อมูลบน Postgresql DB ซ้ำไปยัง server อีกตัว โดยให้ตัวหลักทำหน้าที่ update ข้อมูล และตัวสำรองทำหน้าที่ query ได้เพียงอย่างเดียว
- Master Server 192.168.56.100
- Slave Server 192.168.56.101
- Postgresql 11
- Debian 10
-
สร้าง db user สำหรับใช้ในการทำ replication ในการสร้าง user โดยใช้ psql นั้นต้องอยู่ภายใต้
postgresuser profile เพราะฉนั้นใช้คำสั่งsudo su postgres$ psql -c "CREATE USER replica REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'YOUR_PASSWORD';" -
แก้ไขไฟล์
/etc/postgresql/11/main/pg_hba.confกำหนดให้บัญชี replica สามารถเข้าถึง master server ได้จาก slave serverhost replication replica 192.168.56.101/24 md5 -
แก้ไขไฟล์
/etc/postgresql/11/main/postgresql.confเพื่อกำหนดให้สามารถเข้าถึงได้จาก network และเปิดการใช้งาน WAL replicalisten_addresses = 'localhost,192.168.56.100' wal_level = replica max_wal_senders = 10 wal_keep_segments = 64 -
หลังจากแก้ไข config files ต่างๆ ให้ออกจาก
postgresprofile ด้วยคำสั่งexitแล้วทำการ restart postgresql servicesudo systemctl restart postgresqlหรือ
sudo service postgresql restart
-
ทำการหยุด postgresql service
sudo systemctl stop postgresql -
ดำเนินการแก้ไขไฟล์
/etc/postgresql/11/main/pg_hba.confเปิดให้ master server สามารถเข้าถึงได้host replication replica 192.168.56.100/24 md5 -
แก้ไขไฟล์
/etc/postgresql/11/main/postgresql.confเพื่อตั้งค่าการเข้าถึงจาก network และการทำ replicalisten_addresses = 'localhost,192.168.56.101' wal_level = replica max_wal_senders = 10 wal_keep_segments = 64 hot_standby = on -
จากนั้นเตรียมการคัดลอกข้อมูลจาก master server โดยต้องลบข้อมูลที่มีอยู่บน /var/lib/postgresql/11/main ออกก่อน
rm -dr /var/lib/postgresql/11/main -
ทำการคัดลอกข้อมูล
pg_basebackup -h 192.168.56.100 -D /var/lib/postgresql/11/main/ -P -U replica --wal-method=fetch -
สร้างไฟล์ /var/lib/postgresql/11/main/recovery.conf โดยมีการตั้งค่า ดังนี้
standby_mode = 'on' primary_conninfo = 'host=192.168.56.100 port=5432 user=replica password=123' trigger_file = '/tmp/MasterNow' -
สำหรับ Postgresql 12 นั้น ไม่ได้ใช้ recovery.conf แล้ว เปลี่ยนเป็นไฟล์เปล่า /var/lib/pgsql/12/data/standby.signal และตั้งค่าการเชื่อมต่อไว้ใน postgresql.conf
primary_conninfo = 'host=[REPLICA_IP] port=5432 user=replication password=[REPLICATION PASSWORD]' -
ทำการ start postgresql service
systemctl start postgresql
- ลองสร้าง database หรือ table บน Master แล้วตรวจสอบบน slave ว่ามีสิ่งที่สร้างหรือไม่
Part III. Server Administration Chapter 26. High Availability, Load Balancing, and Replication
ความคิดเห็นคำแนะนำของท่านต่อบทความนี้ จะถูกนำมาใช้ในการพัฒนาปรับปรุงงานเขียนของผม อย่างไงก็รบกวนแสดงความเห็นคิดด้วยนะครับ
สนับสนุนการเงินได้ที่
- บัญชีกสิกรไทย 003–3–29344–5

