1.修改连接权限
# vim /var/lib/pgsql/11/data/pg_hba.conf# 客户端访问
host all all all md5
# replica是用来做备份的用户,172.51.216.82/32是备的IP地址
host replication replica 172.51.216.82/32 md5
# 完整配置# TYPE DATABASE USER ADDRESS METHOD# "local" is for Unix domain socket connections onlylocal all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the# replication privilege.local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
host all all all md5
host replication replica 172.51.216.82/32 md5
2.修改数据库配置:
# vim /var/lib/pgsql/11/data/postgresql.conf
同步增加配置:
synchronous_commit = on # synchronization level;
synchronous_standby_names ='msa'
listen_addresses ='*'# what IP address(es) to listen on;
port = 5432 # (change requires restart)
max_connections = 512 # (change requires restart)
shared_buffers = 128MB # min 128kB
dynamic_shared_memory_type = posix # the default is the first option
wal_level = hot_standby # minimal, replica, or logical
archive_mode = on # enables archiving; off, on, or always
archive_command ='cp %p /var/lib/pgsql/11/data/pg_archive/%f'# command to use to archive a logfile segment
max_wal_senders = 6 # max number of walsender processes
wal_keep_segments = 256 # in logfile segments, 16MB each; 0 disables
wal_sender_timeout = 60s # in milliseconds; 0 disables
log_directory ='log'# directory where log files are written
修改完,要创建刚刚配置的一些目录结构:
# mkdir /var/lib/pgsql/11/data/pg_archive/# chown -R postgres.postgres /var/lib/pgsql/11/data
重启主库服务
1
2
# systemctl restart postgresql-11# systemctl status postgresql-11
1、修改从库配置文件
-bash-4.2$ vim /var/lib/pgsql/11/data/postgresql.conf
listen_addresses ='*'# what IP address(es) to listen on;
port = 5432 # (change requires restart)
max_connections = 1000 # (change requires restart)
shared_buffers = 128MB # min 128kB
dynamic_shared_memory_type = posix # the default is the first option
wal_level = replica # minimal, replica, or logical
archive_mode = on # enables archiving; off, on, or always
archive_command ='cp %p /var/lib/pgsql/11/data/pg_archive/%f'# command to use to archive a logfile segment
wal_sender_timeout = 60s # in milliseconds; 0 disables
hot_standby = on # "on" allows queries during recovery
max_standby_streaming_delay = 30s # max delay before canceling queries
wal_receiver_status_interval = 10s # send replies at least this often
hot_standby_feedback = on # send info from standby to prevent
log_directory ='log'# directory where log files are written,
2.创建恢复文件recovery.conf
-bash-4.2$ cp /usr/pgsql-11/share/recovery.conf.sample /var/lib/pgsql/11/data/recovery.conf
-bash-4.2$ vim /var/lib/pgsql/11/data/recovery.conf
# 修改参数:
recovery_target_timeline ='latest'#同步到最新数据
standby_mode = on #指明从库身份
trigger_file ='failover.now'
primary_conninfo ='host=172.51.216.81 port=5432 user=replica password=replica'#连接到主库信息
切换到root用户
$ exit
1.修改连接权限
# vim /var/lib/pgsql/11/data/pg_hba.conf# 客户端访问
host all all all md5
# replica是用来做备份的用户,172.51.216.82/32是备的IP地址
host replication replica 172.51.216.82/32 md5
#完成配置# TYPE DATABASE USER ADDRESS METHOD# "local" is for Unix domain socket connections onlylocal all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the# replication privilege.local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
host all all all md5
host replication replica 172.51.216.82/32 md5
2.修改数据库配置:
# vim /var/lib/pgsql/11/data/postgresql.conf
同步增加配置:
synchronous_commit = on # synchronization level;
synchronous_standby_names ='msa'
listen_addresses ='*'# what IP address(es) to listen on;
port = 5432 # (change requires restart)
max_connections = 512 # (change requires restart)
shared_buffers = 128MB # min 128kB
dynamic_shared_memory_type = posix # the default is the first option
wal_level = hot_standby # minimal, replica, or logical
archive_mode = on # enables archiving; off, on, or always
archive_command ='cp %p /var/lib/pgsql/11/data/pg_archive/%f'# command to use to archive a logfile segment
max_wal_senders = 6 # max number of walsender processes
wal_keep_segments = 256 # in logfile segments, 16MB each; 0 disables
wal_sender_timeout = 60s # in milliseconds; 0 disables
log_directory ='log'# directory where log files are written
修改完,要创建刚刚配置的一些目录结构:
# mkdir /var/lib/pgsql/11/data/pg_archive/# chown -R postgres.postgres /var/lib/pgsql/11/data
重启主库服务
1
2
# systemctl restart postgresql-11# systemctl status postgresql-11
1、修改从库配置文件
-bash-4.2$ vim /var/lib/pgsql/11/data/postgresql.conf
listen_addresses ='*'# what IP address(es) to listen on;
port = 5432 # (change requires restart)
max_connections = 1000 # (change requires restart)
shared_buffers = 128MB # min 128kB
dynamic_shared_memory_type = posix # the default is the first option
wal_level = replica # minimal, replica, or logical
archive_mode = on # enables archiving; off, on, or always
archive_command ='cp %p /var/lib/pgsql/11/data/pg_archive/%f'# command to use to archive a logfile segment
wal_sender_timeout = 60s # in milliseconds; 0 disables
hot_standby = on # "on" allows queries during recovery
max_standby_streaming_delay = 30s # max delay before canceling queries
wal_receiver_status_interval = 10s # send replies at least this often
hot_standby_feedback = on # send info from standby to prevent
log_directory ='log'# directory where log files are written,
2.创建恢复文件recovery.conf
-bash-4.2$ cp /usr/pgsql-11/share/recovery.conf.sample /var/lib/pgsql/11/data/recovery.conf
-bash-4.2$ vim /var/lib/pgsql/11/data/recovery.conf
# 修改参数:
recovery_target_timeline ='latest'#同步到最新数据
standby_mode = on #指明从库身份
trigger_file ='failover.now'
primary_conninfo ='host=172.51.216.81 port=5432 user=replica password=replica application_name=msa'#连接到主库信息
同步primary_conninfo增加:
application_name=msa
切换到root用户
$ exit
在主库执行 pg_ctl stop 模拟主库宕机。
pg_ctl stop
-bash-4.2$ pg_ctl stop
waiting for server to shut down.... done
server stopped
这时备库日志会报错,提示 primary 主库连接不上
2021-03-15 13:22:57.311 CST [66145] FATAL: could not connect to the primary server: could not connect to server: Connection refused
Is the server running on host "172.51.216.81" and accepting
TCP/IP connections on port 5432?
激活备库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
在备库执行 pg_ctl promote 激活备库
-bash-4.2$ pg_ctl promote
waiting for server to promote.... done
server promoted
备库激活后可以插入数据,变为可读写。这时配置文件 recovery.conf 变为 recovery.done。
postgres=# SELECT pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
We use cookies to improve user experience. By continuing to browse this site, you agree the use of cookies, in accordance with our cookie policy.