postgresql 11 主从配置(流复制)

服务器主从角色分配

ipdb 版本角色
192.168.2.13111
192.168.2.13211

安装postgresql11

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y
yum install postgresql11 -y
yum install postgresql11-server -y

主库配置

  • 主库初始化
/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl start postgresql-11
  • 登录数据库创建复制用户,进行主从同步使用
create role repl login replication encrypted password '123456';
  • 主库上配置从库采用repl账号
vim /var/lib/pgsql/11/data/pg_hba.conf

#只需要台添加下面两行,repl是用来做备份的用户,后面的192.168.2.0/24是该网段内的IP地址
host    replication     repl            192.168.2.0/24         md5
host    all             repl            192.168.2.0/24         trust

vim /var/lib/pgsql/11/data/postgresql.conf 

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 = 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
max_wal_senders = 10       # max number of walsender processes
wal_keep_segments = 10240    # in logfile segments, 16MB each; 0 disables
wal_sender_timeout = 60s    # in milliseconds; 0 disables
log_directory = 'log'    # directory where log files are written
  • 重启主库服务
systemctl restart postgresql-11

从库配置

  1. 从库安装完成后,不初始化,若已经初始化,删除其data目录
  2. 把主节点所有的数据文件都会拷贝过来
pg_basebackup -h 192.168.2.131 -U repl -D /var/lib/pgsql/11/data/ -X stream -P
  1. 从库配置文件,根据下面的配置进行修改。
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/12/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
  • 创建恢复文件recovery.conf
vim /var/lib/pgsql/11/data/recovery.conf

# 调整参数:
recovery_target_timeline = 'latest'   #同步到最新数据
standby_mode = on          #指明从库身份
trigger_file = 'failover.now'
primary_conninfo = 'host=192.168.2.131 port=5432 user=repl password=123456'   #连接到主库信息
  • 启动从库
systemctl restart postgresql-11
  • 验证主从配置
    • 在主库上运行以下命令
      postgres=# select client_addr,sync_state from pg_stat_replication;
      client_addr   | sync_state 
      --------------+------------
      192.168.2.132 | async
      
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值