一、主数据库配置
1、通过rpm或者源码安装单机数据库
2、修改监听端口
默认数据库的监听端口为 localhost,若需要搭建主从我们要修改监听端口保证主从之间的连通性。
1)psql修改
通过psql的命令行修改,在data目录下会新生成一个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文件,则replication slave将在“热备”模式下运行。
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
三、测试主从复制
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、查看主服务wal发送器相关信息
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)数据库层面,可以检查wal发送器进程当前状态信息
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)
四、修改主从复制模式为同步模式
1、开启同步复制前的参数检查并修改
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应用参数
-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/