PostgreSQL数据库-异步流复制Hot Standby环境搭建过程

来源:这里教程网 时间:2026-03-14 20:06:43 作者:

环境介绍

主库: 主机名   postgres10   IP   192.168.1.10备库: 主机名   postgres20   IP    192.168.1.20 基于PostgreSQL 10.5版本的hot standby的部署操作步骤。postgressql的hot standby 模式,即为主库可读可写,从库为只读模式。在配置方面非常简单,在此记录下来,方便大家学习。

Hot Standby环境搭建

1.在主库上创建数据复制用户repuser,并将此用户赋予superuser或replication权限

[postgres@postgres10 postgre]$ psqlpsql (10.5)Type "help" for help.postgres=# create user repuser with password 'postgres';CREATE ROLEpostgres=#postgres=# alter user repuser superuser;ALTER ROLEpostgres=#

2.编辑pg_hba.conf认证文件,将复制用户的认证添加上去,使用md5认证方式

[postgres@postgres10 pgdata]$ tail pg_hba.conflocal replication all trusthost replication all 127.0.0.1/32 trusthost replication all ::1/128 trusthost replication repuser 192.168.1.20/32 md5[postgres@postgres10 pgdata]$

3.在数库上添加如下参数,并重启数据库使之生效

listen_addresses = '192.168.1.10'min_wal_size = 800MBmax_wal_senders = 10wal_level = replica

4.在备数据库上使用pg_basebackup工具执行数据库基础备份

[postgres@postgresql20 postgre]$ pg_basebackup -h 192.168.1.10 -U repuser -F p -P -X stream -R -D $PGDATA -l pgbackup20201021Password:30488/30488 kB (100%), 1/1 tablespace[postgres@postgresql20 postgre]$

5.修改备库上的postgres.conf文件

添加参数hot_standby = on同时在$PGDATA目录下生成了recovery.conf文件,这个文件中记录了从库连接主库的连接信息

6.启动备库,进行验证

[postgres@postgresql20 pgdata]$[postgres@postgresql20 pgdata]$ pg_ctl startwaiting for server to start....2020-10-21 16:39:27.072 CST [3870] LOG: listening on IPv4 address "0.0.0.0", port 54322020-10-21 16:39:27.072 CST [3870] LOG: listening on IPv6 address "::", port 54322020-10-21 16:39:27.074 CST [3870] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2020-10-21 16:39:27.091 CST [3870] LOG: redirecting log output to logging collector process2020-10-21 16:39:27.091 CST [3870] HINT: Future log output will appear in directory "/postgre/pgdata/log".doneserver started[postgres@postgresql20 pgdata]$

7.查看主从复制状态

在主库上查看主从复制的状态postgres=# select client_addr,state,sync_state from pg_stat_replication; client_addr  |   state   | sync_state --------------+-----------+------------ 192.168.1.20 | streaming | async(1 row)如果看不到备库过来的连接,说明备库没有连过来,需要检查备库的日志文件查看原因。如果看到的流复制状态“state”的值不是“streaming”,也说明备库的流复制有问题。

8.在主库上创建测试表,在从库上查看同步情况

[postgres@postgres10 postgre]$ psqlpsql (10.5)Type "help" for help.postgres=# create table test01(id int,name varchar(10));CREATE TABLEpostgres=# insert into test01 values(1,'a');INSERT 0 1postgres=# insert into test01 values(2,'b');INSERT 0 1postgres=# insert into test01 values(3,'c');INSERT 0 1postgres=# select * from test01;id | name----+------1 | a2 | b3 | c(3 rows)在从库上查看数据同步情况[postgres@postgresql20 pgdata]$ psqlpsql (10.5)Type "help" for help.postgres=#postgres=# select * from test01;id | name----+------1 | a2 | b3 | c(3 rows)

交换主备库的角色

1.在原主库上创建recovery.conf文件,并编辑新的连接主库信息。

在此,将从库的recovery.conf文件拷贝至主库的$PGDATA目录下。并将更新主库的连接信息 [postgres@postgres10 pgdata]$ more recovery.conf  standby_mode = 'on' primary_conninfo = 'user=repuser password=postgres host=192.168.1.20 port=5432 sslmode=disable sslcompression=1 target_session_attrs=any'

2.在原主库的postgresql.conf配置文件中,添加hot_standby=on参数

hot_standby=on

3.将原备库的postgresql.conf配置文件中,删除hot_standby=on参数,同时将recovery.conf文件删除

4.启动主备两个数据库环境,并在新的主库上进行主从连接状态检测

[postgres@postgresql20 pgdata]$ psql psql (10.5) Type "help" for help. postgres=# select client_addr,state,sync_state from pg_stat_replication;  client_addr  |   state   | sync_state  --------------+-----------+------------  192.168.1.10 | streaming | async (1 row) 至此,可以看到原来的主库(192.168.1.10)已经成为了备库,而原来的备库(192.168.1.20)成为了主库。新的主从关系建立好了。

相关推荐

热文推荐