postgresql 11 主从配置(流复制)

文章目录

      • 服务器主从角色分配
      • 安装postgresql11
      • 主库配置
      • 从库配置

服务器主从角色分配

ip db 版本 角色
192.168.2.131 11
192.168.2.132 11

安装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
      

你可能感兴趣的:(大数据,postgresql)