设为首页 收藏本站
查看: 1030|回复: 0

[经验分享] postgresql streaming replication

[复制链接]

尚未签到

发表于 2016-11-19 09:11:49 | 显示全部楼层 |阅读模式
postgresql从9.0开始有流复制,这里记录一下流复制的安装记录。
<wbr style=""><div style="">环境:</div><div style="">redhat 5.5 64位</div><div style="">PG版本:9.1.2</div><div style="">master:192.168.1.168</div><div style="">slave:192.168.1.169</div><div style=""><br style="line-height:25px"></div><div style=""><strong style="line-height:25px"><span style="font-size:14px; line-height:28px">1.配置主机系统相关参数</span></strong></div><div style=""><strong style="line-height:25px"><span style="font-size:14px; line-height:28px"> 1.1修改/etc/sysctl.conf</span></strong></div><div style="">kernel.shmmni = 4096</div><div style="">kernel.sem = 50100 64128000 50100 1280</div><div style="">fs.file-max = 7672460</div><div style="">net.ipv4.ip_local_port_range = 9000 65000</div><div style="">net.core.rmem_default = 1048576</div><div style="">net.core.rmem_max = 4194304</div><div style="">net.core.wmem_default = 262144</div><div style="">net.core.wmem_max = 1048576</div><div style="">net.ipv4.tcp_tw_recycle = 1</div><div style="">net.ipv4.tcp_max_syn_backlog = 4096</div><div style="">net.core.netdev_max_backlog = 10000</div><div style="">vm.overcommit_memory = 0</div><div style="">net.ipv4.ip_conntrack_max = 655360</div><div style="">fs.aio-max-nr = 1048576</div><div style="">net.ipv4.tcp_timestamps = 0</div><div style="">1.2修改/etc/security/limits.conf</div><div style=""><div style="line-height:25px">* soft  nofile 131072</div><div style="line-height:25px">* hard  nofile 131072</div><div style="line-height:25px">* soft  nproc  131072</div><div style="line-height:25px">* hard  nproc  131072</div><div style="line-height:25px">* soft  core  unlimited</div><div style="line-height:25px">* hard  core  unlimited</div><div style="line-height:25px">* soft  memlock 50000000</div><div style="line-height:25px">* hard  memlock 50000000</div></div><div style=""><strong style="line-height:25px"><span style="font-size:14px; line-height:28px">1.3配置系统环境变量</span></strong></div><div style=""><div style="line-height:25px">export PS1="$USER@`/bin/hostname -s`-&gt; "</div><div style="line-height:25px">export PGPORT=1921</div><div style="line-height:25px">export PGDATA=/opt/pgdata/pg_root</div><div style="line-height:25px">export LANG=en_US.utf8</div><div style="line-height:25px">export PGHOME=/opt/pgsql9.1.2</div><div style="line-height:25px">export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib</div><div style="line-height:25px">export DATE=`date +"%Y%m%d%H%M"`</div><div style="line-height:25px">export PATH=$PGHOME/bin:$PATH:.</div><div style="line-height:25px">export MANPATH=$PGHOME/share/man:$MANPATH</div><div style="line-height:25px">alias rm='rm -i'</div><div style="line-height:25px">alias ll='ls -lh'</div></div><div style=""><strong style="line-height:25px"><span style="font-size:14px; line-height:28px">1.4配置其他</span></strong></div><div style="">防火墙,服务相关等等,这里根据需要就不再一一列举</div><div style=""><strong style="line-height:25px"><span style="font-size:14px; line-height:28px">2.安装PG(略)</span></strong></div><div style="">3.在master建立流复制用户</div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">create role repluser SUPERUSER LOGIN CONNECTION LIMIT 20 ENCRYPTED PASSWORD 'repluser';--注意这里满足login,replication权限即可,不是必须superuser。</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px"><strong style="line-height:25px"><span style="font-size:14px; line-height:28px">4.master配置pg_hba,conf,添加以下:</span></strong></span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">host replication    repluser    192.168.1.169/32    md5</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; color:#333333; line-height:28px"><strong style="line-height:28px">5.master配置postgresql.conf</strong></span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px"></span><div style="line-height:25px">listen_addresses = '*'     # what IP address(es) to listen on;</div><div style="line-height:25px">port = 1921               # (change requires restart)</div><div style="line-height:25px">max_connections = 2000         # (change requires restart)</div><div style="line-height:25px">unix_socket_directory = '/opt/pgdata/pg_root'     # (change requires restart)</div><div style="line-height:25px">unix_socket_permissions = 0700     # begin with 0 to use octal notation</div><div style="line-height:25px">password_encryption = on</div><div style="line-height:25px">shared_buffers = 2048MB         # min 128kB</div><div style="line-height:25px">maintenance_work_mem = 2048MB      # min 1MB</div><div style="line-height:25px">max_stack_depth = 8MB          # min 100kB</div><div style="line-height:25px">wal_level = hot_standby         # minimal, archive, or hot_standby</div><div style="line-height:25px">synchronous_commit = off        # immediate fsync at commit</div><div style="line-height:25px">wal_sync_method = fdatasync       # the default is the first option</div><div style="line-height:25px">wal_buffers = 128000kB         # min 32kB</div><div style="line-height:25px">wal_writer_delay = 20ms         # 1-10000 milliseconds</div><div style="line-height:25px">checkpoint_segments = 64        # in logfile segments, min 1, 16MB each</div><div style="line-height:25px">checkpoint_timeout = 30min       # range 30s-1h</div><div style="line-height:25px">archive_mode = on        # allows archiving to be done</div><div style="line-height:25px">archive_command = '/bin/date'     # command to use to archive a logfile segment</div><div style="line-height:25px">max_wal_senders = 30      # max number of walsender processes</div><div style="line-height:25px">wal_keep_segments = 1000    # in logfile segments, 16MB each; 0 disables;因为本例使用的logfile segment单个=64M,所以这里需要至少64*1000=64GB的pg_xlog目录</div><div style="line-height:25px">空间,否则可能空间溢出.</div><div style="line-height:25px">random_page_cost = 2.0         # same scale as above</div><div style="line-height:25px">effective_cache_size = 12800MB</div><div style="line-height:25px">constraint_exclusion = partition    # on, off, or partition</div><div style="line-height:25px">log_destination = 'csvlog'       # Valid values are combinations of</div><div style="line-height:25px">logging_collector = on     # Enable capturing of stderr and csvlog</div><div style="line-height:25px">log_connections = on      # 调试阶段建议主库和standby都打开log_connections,调试完后建议关闭.</div><div style="line-height:25px">log_directory = '/opt/pgdata/pg_log'      # directory where log files are written,</div><div style="line-height:25px">log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,</div><div style="line-height:25px">log_truncate_on_rotation = on      # If on, an existing log file of the</div><div style="line-height:25px">log_rotation_age = 1d          # Automatic rotation of logfiles will</div><div style="line-height:25px">log_rotation_size = 10MB        # Automatic rotation of logfiles will</div><div style="line-height:25px">log_min_duration_statement = 1000ms   # -1 is disabled, 0 logs all statements</div><div style="line-height:25px">log_checkpoints = on</div><div style="line-height:25px">log_lock_waits = on           # log lock waits &gt;= deadlock_timeout</div><div style="line-height:25px">log_statement = 'ddl'          # none, ddl, mod, all</div><div style="line-height:25px">track_activity_query_size = 2048    # (change requires restart)</div><div style="line-height:25px">autovacuum = on         # Enable autovacuum subprocess? 'on'</div><div style="line-height:25px">log_autovacuum_min_duration = 0 # -1 disables, 0 logs all actions and</div><div style="line-height:25px">check_function_bodies = on</div><div style="line-height:25px">bytea_output = 'escape'         # hex, escape</div><div style="line-height:25px">datestyle = 'iso, mdy'</div><div style="line-height:25px">lc_messages = 'C'            # locale for system error message</div><div style="line-height:25px">lc_monetary = 'C'            # locale for monetary formatting</div><div style="line-height:25px">lc_numeric = 'C'            # locale for number formatting</div><div style="line-height:25px">lc_time = 'C'              # locale for time formatting</div><div style="line-height:25px">default_text_search_config = 'pg_catalog.english'</div><div style="line-height:25px">deadlock_timeout = 1s</div><div style="line-height:25px">tcp_keepalives_idle = 60</div><div style="line-height:25px"><strong style="line-height:25px"><span style="font-size:14px; line-height:28px">6.启动master并对其进行全备一次</span></strong></div><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">select pg_start_backup('replication backup');</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px"><span style="line-height:25px">将$PGDATA压缩传送的slave</span></span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">select pg_stop_backup();</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px"><strong style="line-height:25px"><span style="font-size:14px; line-height:28px">7.slave配置</span></strong></span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">相应的也是配置pg_hba.conf,postgressql.conf</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px"></span><div style="line-height:25px">host   replication   repluser    191.168.169.85/32   md5</div><div style="line-height:25px">slave配置hot_standby = on其他基本不需要修改,可以和master一致</div><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px"></span><div style="line-height:25px">除了配置以上的,slave还需要配置recovery.conf</div><div style="line-height:25px"><div style="line-height:25px">#archive_cleanup_command = 'pg_archivecleanup $PGARCHIVE %r'</div><div style="line-height:25px">standby_mode = 'on'</div><div style="line-height:25px">trigger_file = '/opt/pgdata/pg_root/postgresql.trigger.1921'</div><div style="line-height:25px">primary_conninfo = 'host=192.168.1.168 port=1921 user=repluser password=repluser keepalives_idle=60'</div></div><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px"></span><div style="line-height:25px"><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">修改</span></div>more ~/.pgpass</div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px"></span><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">192.168.1.168:1921:replication:repluser:repluser:repluser</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">启动slave观察cvs日志</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">查看wal进程:</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">master:</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">wal sender process repluser 192.168.1.169(16494) streaming 0/70273E8</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">slave</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">postgres: wal receiver process  streaming 0/70273E8</span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; color:#333333; line-height:28px"><strong style="line-height:28px">8.测试</strong></span></div><div style=""><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#333333; line-height:25px">建立数据库,用户,测试数据,注意表空间的一定要建好相关目录,而且要一主从节点一致,否则slave会直接关掉。</span></div></wbr>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-302408-1-1.html 上篇帖子: postgresql pg_buffercache 下篇帖子: windows下postgresql的使用注意问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表