การจำลองข้อมูลทางกายภาพ (แบบสตรีม)
เรียนรู้การตั้งค่าและกำหนดค่าการจำลองข้อมูลทางกายภาพแบบสตรีม เพื่อสร้างเซิร์ฟเวอร์สำรองแบบอ่านอย่างเดียว
การจำลองข้อมูลทางกายภาพ (แบบสตรีม) เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Physical Replication: A Byte-for-Byte Copy
Physical replication in PostgreSQL creates an exact, byte-for-byte copy of your primary database server. Think of it as mirroring the entire data directory and all changes that happen.
This method is highly robust for disaster recovery and provides a consistent read-only copy of your data, known as a standby server. If your primary server fails, you can promote the standby to become the new primary.
How Streaming Replication Works
Streaming replication is a form of physical replication where the primary server continuously sends its Write-Ahead Log (WAL) records to the standby server. The standby then applies these WAL records in real-time.
This ensures the standby is always up-to-date with the primary, minimizing data loss in case of a failure. It's like a constant stream of updates flowing from one database to another.
Primary Config: `wal_level`
To enable streaming replication, the primary server must generate enough WAL information. The wal_level parameter controls this.
It must be set to replica (or logical for more advanced features) in your postgresql.conf. The default minimal level is not sufficient for replication.
Let's check the current wal_level:
SHOW wal_level;Primary Config: `max_wal_senders`
The primary server needs processes to send WAL data to standby servers. These are called WAL sender processes.
The max_wal_senders parameter determines the maximum number of concurrent connections from standby servers. Set this to at least the number of standbys you plan to have, plus some for potential cascading standbys.
Check your current setting:
SHOW max_wal_senders;Creating a Replication User
It's best practice to create a dedicated PostgreSQL user for replication. This user needs the REPLICATION privilege, which allows it to connect and stream WAL data.
This separation enhances security and makes it clear which connections are for replication purposes.
Here's how you might create one (replace with a strong password):
CREATE USER repl_user WITH REPLICATION ENCRYPTED PASSWORD 'your_secret_password';Allowing Standby Connections (`pg_hba.conf`)
The primary server needs to allow connections from the standby server. This is configured in the pg_hba.conf file, which controls client authentication.
You'll need to add an entry for your replication user, specifying the IP address or range of your standby server(s). This tells PostgreSQL to trust connections from those standbys.
# Example pg_hba.conf entry:
host replication repl_user 192.168.1.100/32 md5Base Backup for Standby
Before a standby can start streaming WAL, it needs an initial copy of the primary's data directory. This is called a base backup.
PostgreSQL provides the pg_basebackup utility to create this snapshot. It copies all data files from the primary to the standby, ensuring they start from a consistent point.
This command is typically run from the standby server:
# Example pg_basebackup command (run on standby):
pg_basebackup -h primary_ip -p 5432 -U repl_user -D /var/lib/postgresql/16/main -F p -Xs -P -RStandby Configuration: `standby.signal`
Once the base backup is in place, the standby server needs to know it's a standby. For PostgreSQL 12 and later, this is signaled by the presence of an empty file named standby.signal in the data directory.
This file tells PostgreSQL to start in recovery mode and look for replication configuration.
Standby Configuration: `primary_conninfo`
The standby server also needs to know how to connect to the primary. This is specified using the primary_conninfo parameter in its postgresql.conf file.
It's a connection string similar to what you'd use with psql, including the primary's host, port, replication user, and password.
You can set this using ALTER SYSTEM on the standby:
ALTER SYSTEM SET primary_conninfo = 'host=primary_ip port=5432 user=repl_user password=your_secret_password application_name=my_standby';Replication Config Check
You've learned about the core parameters for setting up streaming replication. Let's test your knowledge!
Recap: Physical Streaming Replication
In this lesson, we explored physical streaming replication. You learned that it creates a byte-for-byte copy of the primary database by continuously sending WAL records.
- We covered primary server configurations like
wal_levelandmax_wal_senders. - You also learned about creating a dedicated replication user and configuring
pg_hba.conf. - Finally, we touched on preparing the standby with a base backup using
pg_basebackupand configuring its connection usingprimary_conninfo.
Next, we'll put these concepts into practice by setting up a standby server!
คำถามที่พบบ่อย
บทเรียน “การจำลองข้อมูลทางกายภาพ (แบบสตรีม)” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจำลองข้อมูลทางกายภาพ (แบบสตรีม)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจำลองข้อมูลทางกายภาพ (แบบสตรีม)”
เรียนรู้การตั้งค่าและกำหนดค่าการจำลองข้อมูลทางกายภาพแบบสตรีม เพื่อสร้างเซิร์ฟเวอร์สำรองแบบอ่านอย่างเดียว คุณปฏิบัติ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced PostgreSQL: Indexing, Partitioning, Replication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การจำลองข้อมูลทางกายภาพ (แบบสตรีม)” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม
ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจแนวคิดการจำลองข้อมูล
- การจำลองข้อมูลทางกายภาพ (แบบสตรีม)
- การตั้งค่าเซิร์ฟเวอร์สำรอง
- การจำลองแบบซิงโครนัสเทียบกับอะซิงโครนัส