記事のディレクトリ
1つは、メインのデータベース構成です。
1.rpmまたはソースコードを使用してスタンドアロンデータベースをインストールします
2.リスニングポートを変更します
デフォルトデータベースのリスニングポートはlocalhostです。マスタースレーブを構築する必要がある場合は、リスニングポートを変更して、マスタースレーブ間の接続を確保する必要があります。
1)psqlの変更
psqlコマンドラインの変更により、新しいpostgresql.auto.confファイルがデータディレクトリに生成され、PGデータベースはデフォルトでpostgresql.auto.confを優先します。
-bash-4.2$ /usr/local/pgsql/bin/psql -c "alter system set listen_addresses to '*';"
ALTER SYSTEM
-bash-4.2$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
listen_addresses = '*'
-bash-4.2$ cat postgresql.conf | grep listen_addr
#listen_addresses = 'localhost' # what IP address(es) to listen on;
2)構成ファイルを直接変更する
3.コピーユーザーを作成します
-bash-4.2$ /usr/local/pgsql/bin/createuser --replication -P -e repl
Enter password for new role:
Enter it again:
SELECT pg_catalog.set_config('search_path', '', false);
CREATE ROLE repl PASSWORD 'md59d7520ffdd7a2d1aa29de3803946ef02' NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN REPLICATION;
4.ロールの承認、承認後にデータベースを再起動します
## 配置授权
-bash-4.2$ vi pg_hba.conf
host replication repl 192.168.1.171/23 trust
## 重启数据库
-bash-4.2$ /usr/local/pgsql/bin/pg_ctl -D /data/pgsql12/data/ stop
waiting for server to shut down.... done
server stopped
-bash-4.2$
-bash-4.2$
-bash-4.2$ /usr/local/pgsql/bin/pg_ctl -D /data/pgsql12/data/ start -l /data/pgsql12/logs/logfile
waiting for server to start.... done
server started
次に、スレーブサーバーを構成します
1.rpmまたはソースコードを使用してスタンドアロンデータベースをインストールおよびデプロイします
2.データベースサーバーを停止し、スレーブサーバーデータベースのデータディレクトリを空にします
-bash-4.2$ /usr/local/pgsql/bin/pg_ctl -D /data/pgsql12/data/ stop
waiting for server to shut down.... done
server stopped
-bash-4.2$
-bash-4.2$ rm -rf /data/pgsql12/data/*
-bash-4.2$ ll
total 0
3.pg_basebackupツールを使用してメインデータベースをバックアップします
-bash-4.2$ /usr/local/pgsql/bin/pg_basebackup -h 192.168.0.175 -D /data/pgsql12/data/ -U repl -P -v -R -X stream -C -S pgstandby1
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/2000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: created replication slot "pgstandby1"
24588/24588 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/2000100
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: base backup completed
-bash-4.2$ ll
total 8
drwx------ 19 postgres postgres 4096 Sep 5 13:05 data
drwxr-xr-x 2 postgres postgres 4096 Sep 5 00:12 logs
-bash-4.2$ cd data/
-bash-4.2$ ll
total 120
-rw------- 1 postgres postgres 224 Sep 5 13:05 backup_label
drwx------ 5 postgres postgres 4096 Sep 5 13:05 base
drwx------ 2 postgres postgres 4096 Sep 5 13:05 global
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_commit_ts
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_dynshmem
-rw------- 1 postgres postgres 4583 Sep 5 13:05 pg_hba.conf
-rw------- 1 postgres postgres 1636 Sep 5 13:05 pg_ident.conf
drwx------ 4 postgres postgres 4096 Sep 5 13:05 pg_logical
drwx------ 4 postgres postgres 4096 Sep 5 13:05 pg_multixact
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_notify
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_replslot
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_serial
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_snapshots
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_stat
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_stat_tmp
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_subtrans
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_tblspc
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_twophase
-rw------- 1 postgres postgres 3 Sep 5 13:05 PG_VERSION
drwx------ 3 postgres postgres 4096 Sep 5 13:05 pg_wal
drwx------ 2 postgres postgres 4096 Sep 5 13:05 pg_xact
-rw------- 1 postgres postgres 337 Sep 5 13:05 postgresql.auto.conf
-rw------- 1 postgres postgres 26615 Sep 5 13:05 postgresql.conf
-rw------- 1 postgres postgres 0 Sep 5 13:05 standby.signal
postgresql.confのhot_standbyパラメータがon(デフォルト値)に設定されていて、Standby.signalファイルがデータディレクトリに存在する場合、レプリケーションスレーブは「ホットスタンバイ」モードで実行されます。
4.メインデータベースにログインして、ログインスロット情報を確認します
postgres=# select * from pg_replication_slots ;
slot_name | plugin | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn
------------+--------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------
pgstandby1 | | physical | | | f | f | | | | 0/2000000 |
(1 row)
5.スレーブデータベースを起動します
-bash-4.2$ /usr/local/pgsql/bin/pg_ctl -D /data/pgsql12/data/ -l /data/pgsql12/logs/logfile start
waiting for server to start.... done
server started
3つ、マスタースレーブレプリケーションをテストします
1.サーバーからWALレシーバーの関連情報を表示します
1)オペレーティングシステムレベルで、walのストリーミングレプリケーションレシーバーのプロセスを確認できます
-bash-4.2$ ps -ef | grep post
root 999 1 0 Sep04 ? 00:00:00 /usr/libexec/postfix/master -w
postfix 1002 999 0 Sep04 ? 00:00:00 qmgr -l -t unix -u
postfix 13299 999 0 12:57 ? 00:00:00 pickup -l -t unix -u
root 13300 13164 0 12:57 pts/0 00:00:00 su - postgres
postgres 13301 13300 0 12:57 pts/0 00:00:00 -bash
postgres 13349 1 0 13:41 ? 00:00:00 /usr/local/pgsql/bin/postgres -D /data/pgsql12/data
postgres 13350 13349 0 13:41 ? 00:00:00 postgres: startup recovering 000000010000000000000003
postgres 13351 13349 0 13:41 ? 00:00:00 postgres: checkpointer
postgres 13352 13349 0 13:41 ? 00:00:00 postgres: background writer
postgres 13353 13349 0 13:41 ? 00:00:00 postgres: stats collector
postgres 13354 13349 0 13:41 ? 00:00:00 postgres: walreceiver streaming 0/3000148
postgres 13363 13301 0 13:46 pts/0 00:00:00 ps -ef
postgres 13364 13301 0 13:46 pts/0 00:00:00 grep --color=auto post
2)データベースレベルでは、walプロセスの現在のステータス情報を確認できます。
postgres=# \x 1
Expanded display is on.
postgres=# select * from pg_stat_wal_receiver ;
-[ RECORD 1 ]---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pid | 13354
status | streaming
receive_start_lsn | 0/3000000
receive_start_tli | 1
received_lsn | 0/3000148
received_tli | 1
last_msg_send_time | 2020-09-05 13:46:00.603516+08
last_msg_receipt_time | 2020-09-05 13:45:00.724649+08
latest_end_lsn | 0/3000148
latest_end_time | 2020-09-05 13:42:30.111672+08
slot_name | pgstandby1
sender_host | 192.168.0.175
sender_port | 5432
conninfo | user=repl passfile=/home/postgres/.pgpass dbname=replication host=192.168.0.175 port=5432 fallback_application_name=walreceiver sslmode=disable sslcompression=0 gssencmode=disable krbsrvname=postgres target_session_attrs=any
postgres=#
2.メインサービスの送信者の関連情報を表示します
1)オペレーティングシステムレベルで、wal送信者の関連するプロセス情報を確認できます。
-bash-4.2$ ps -ef | grep post
root 1011 1 0 Sep04 ? 00:00:00 /usr/libexec/postfix/master -w
postfix 1013 1011 0 Sep04 ? 00:00:00 qmgr -l -t unix -u
root 14764 14715 0 12:13 pts/0 00:00:00 su - postgres
postgres 14765 14764 0 12:13 pts/0 00:00:00 -bash
postfix 14906 1011 0 12:58 ? 00:00:00 pickup -l -t unix -u
postgres 14979 1 0 13:06 ? 00:00:00 /usr/local/pgsql/bin/postgres -D /data/pgsql12/data
postgres 14981 14979 0 13:06 ? 00:00:00 postgres: checkpointer
postgres 14982 14979 0 13:06 ? 00:00:00 postgres: background writer
postgres 14983 14979 0 13:06 ? 00:00:00 postgres: walwriter
postgres 14984 14979 0 13:06 ? 00:00:00 postgres: autovacuum launcher
postgres 14985 14979 0 13:06 ? 00:00:00 postgres: stats collector
postgres 14986 14979 0 13:06 ? 00:00:00 postgres: logical replication launcher
postgres 15040 14979 0 13:42 ? 00:00:00 postgres: walsender repl 192.168.1.171(58690) streaming 0/3000148
postgres 15055 14765 0 13:51 pts/0 00:00:00 ps -ef
postgres 15056 14765 0 13:51 pts/0 00:00:00 grep --color=auto post
-bash-4.2$
2)データベースレベルでは、walsenderプロセスの現在のステータス情報を確認できます。
postgres=# \x 1
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid | 15040
usesysid | 16384
usename | repl
application_name | walreceiver
client_addr | 192.168.1.171
client_hostname |
client_port | 58690
backend_start | 2020-09-05 13:42:30.108815+08
backend_xmin |
state | streaming
sent_lsn | 0/3000148
write_lsn | 0/3000148
flush_lsn | 0/3000148
replay_lsn | 0/3000148
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
reply_time | 2020-09-05 13:49:41.269624+08
3.マスタースレーブレプリケーションテスト
# 主数据库操作
postgres=# create database db1;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
db1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# \c db1
You are now connected to database "db1" as user "postgres".
db1=# \d
Did not find any relations.
db1=# create table t1 (id int,name varchar(20));
CREATE TABLE
db1=# insert into t1 values (1,'aa');
INSERT 0 1
db1=# insert into t1 values (2,'bb');
INSERT 0 1
db1=# insert into t1 values (3,'cc');
INSERT 0 1
db1=# select * from t1;
id | name
----+------
1 | aa
2 | bb
3 | cc
(3 rows)
# 从数据库验证
-bash-4.2$ /usr/local/pgsql/bin/psql
psql (12.2)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
db1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# \c db1
You are now connected to database "db1" as user "postgres".
db1=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | t1 | table | postgres
(1 row)
db1=# select * from t1;
id | name
----+------
1 | aa
2 | bb
3 | cc
(3 rows)
第4に、マスタースレーブレプリケーションモードを同期モードに変更します
1.同期レプリケーションを有効にする前に、パラメータを確認して変更します
同期コミット
同期スタンバイ名
# synchronous_commit
db1=# show synchronous_commit ; //默认开启
synchronous_commit
--------------------
on
(1 row)
# synchronous_standby_names
db1=# alter system set synchronous_standby_names to '*';
ALTER SYSTEM
-bash-4.2$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
listen_addresses = '*'
synchronous_standby_names = '*'
2.サーバーアプリケーションパラメータをリロードします
-bash-4.2$ /usr/local/pgsql/bin/pg_ctl -D /data/pgsql12/data/ reload
server signaled
3.メインデータベースにログインして、現在のレプリケーションモードを表示します
postgres=# \x 1
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid | 15040
usesysid | 16384
usename | repl
application_name | walreceiver
client_addr | 192.168.1.171
client_hostname |
client_port | 58690
backend_start | 2020-09-05 13:42:30.108815+08
backend_xmin |
state | streaming
sent_lsn | 0/3013CA8
write_lsn | 0/3013CA8
flush_lsn | 0/3013CA8
replay_lsn | 0/3013CA8
write_lag |
flush_lag |
replay_lag |
sync_priority | 1
sync_state | sync
reply_time | 2020-09-05 14:15:27.369742+08
参照ドキュメント:
https ://www.tecmint.com/configure-postgresql-streaming-replication-in-centos-8/