PG master-slave architecture deployment

One, the main database configuration

1. Install the stand-alone database through rpm or source code

2. Modify the listening port

The listening port of the default database is localhost. If we need to build a master-slave, we need to modify the listening port to ensure the connectivity between the master-slave.

1) psql modification

Through psql command line modification, a new postgresql.auto.conf file will be generated in the data directory, and the PG database will give priority to postgresql.auto.conf by default.

-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) Modify the configuration file directly

3. Create a copy user

-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. Role authorization, restart the database after authorization

## 配置授权
-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

Second, configure the slave server

1. Install and deploy a stand-alone database through rpm or source code

2. Stop the database server and empty the data directory of the slave server database

-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. Back up the main database through the pg_basebackup tool

-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

If the hot_standby parameter in postgresql.conf is set to on (the default value) and the Standby.signal file exists in the data directory, the replication slave will run in "hot standby" mode.

4. Log in to the main database to verify the login slot information

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. Start the slave database

-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

Three, test master-slave replication

1. View the relevant information of the WAL receiver from the server

1) At the operating system level, you can see the process of wal's streaming replication receiver

-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) At the database level, you can check the current status information of the wal process

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. View the related information of the main service wal sender

1) At the operating system level, you can see the process information related to the Wal sender

-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) At the database level, you can check the current status information of the wal sender process


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. Master-slave replication test

# 主数据库操作

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)


Fourth, modify the master-slave replication mode to synchronous mode

1. Check and modify the parameters before enabling synchronous replication

synchronous_commit

synchronous_standby_names

# 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. Reload server application parameters

-bash-4.2$ /usr/local/pgsql/bin/pg_ctl -D /data/pgsql12/data/ reload
server signaled

3. Log in to the main database to view the current replication mode


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

Reference document:
https://www.tecmint.com/configure-postgresql-streaming-replication-in-centos-8/

Guess you like

Origin blog.csdn.net/weixin_37692493/article/details/108500440