目标
使用PG的物理流复制技术搭建2节点的主从环境,进行主从切换测试。
实验环境:Oracle Linux 7.6 on virtualbox Primary: 10.0.2.110 pghost00 Slave:10.0.2.111 pghost01 Pgversion: 10.9 |
步骤
1. 主库修改参数文件postgresql.conf,开启归档,设置wal日志级别
wal_level = replicaarchive_mode = onarchive_command = '/bin/date'max_wal_senders = 10 -->设置发送wal日志进程的个数,值至少要大于备库的个数wal_keep_segments = 100 -->设置wal日志保留的个数hot_standby = on -->开启hot_standby,则可以在备库上进行查询操作
2. 主库创建复制帐号
postgres=# create user repuser replication login connection limit 5 encrypted password 'rep12345';CREATE ROLEpostgres=# \du+List of rolesRole name | Attributes | Member of | Description-----------+------------------------------------------------------------+-----------+-------------postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} |repuser | Replication +| {} || 5 connections
3. 主库配置pg_hba.conf, 使复制用户repuser可以连接主从数据库
# replication privilege.host replication repuser 10.0.2.110/24 md5host replication repuser 10.0.2.111/24 md5
4. 从库使用pg_basebackup远程备份主库
--从库首先清空$PGDATA[postgres@pghost01 data]$ cd $PGDATA[postgres@pghost01 data]$ rm -rf *[postgres@pghost01 data]$ pg_basebackup -D $PGDATA -Fp -Xs -v -P -h 10.0.2.110 -p 5432 -U repuserpg_basebackup: initiating base backup, waiting for checkpoint to completepg_basebackup: checkpoint completedpg_basebackup: write-ahead log start point: 2/1000028 on timeline 13pg_basebackup: starting background WAL receiver288141/288141 kB (100%), 1/1 tablespacepg_basebackup: write-ahead log end point: 2/10000F8pg_basebackup: waiting for background process to finish streaming ...pg_basebackup: base backup completed
5. 从库配置recovery.conf
[postgres@pghost01 data]$ cat recovery.confrecovery_target_timeline = 'latest'standby_mode = onprimary_conninfo = 'host=10.0.2.110 port=5432 user=repuser password=rep12345'
6. 启动从库, 并验证复制状态
[postgres@pghost01 data]$ pg_ctl startwaiting for server to start....2019-12-20 11:03:26.502 CST [3902] LOG: listening on IPv4 address "0.0.0.0", port 54322019-12-20 11:03:26.502 CST [3902] LOG: listening on IPv6 address "::", port 54322019-12-20 11:03:26.506 CST [3902] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2019-12-20 11:03:26.523 CST [3902] LOG: redirecting log output to logging collector process2019-12-20 11:03:26.523 CST [3902] HINT: Future log output will appear in directory "log".. done--主库查看wal日志发送状态select * from pg_stat_replication;--从库查看wal日志接收状态select * from pg_stat_wal_receiver;
手动切换测试
--关闭主库-bash-4.2$ pg_ctl stopwaiting for server to shut down.... doneserver stopped--在备库上执行pg_ctl promote命令激活备库,如果recovery.conf变成recovery.done, 表示备库已切换成为主库。-bash-4.2$ pg_ctl promotewaiting for server to promote.... doneserver promoted-bash-4.2$ pg_controldata | grep cluster ---->>通过控制文件查询实例状态Database cluster state: in production-bash-4.2$ ls -ltr recovery*-rw-r--r-- 1 postgres postgres 5904 Jun 4 10:04 recovery.done--将老的主库切换成备库,在老的主库的$PGDATA目录下创建recovery.conf文件。配置和老的从库pghost01一样,只是primary_conninfo参数中的IP换成对端IP。-bash-4.2$ cp /usr/pgsql-11/share/recovery.conf.sample $PGDATA/-bash-4.2$ mv recovery.conf.sample recovery.conf-bash-4.2$ vi recovery.conf-bash-4.2$-bash-4.2$ tail recovery.confrecovery_target_timeline = 'latest'standby_mode = onprimary_conninfo = 'host=10.0.2.111 port=5432 user=repuser password=rep12345'--启动老的主库,这时观察主、备进程是否正常,如果正常表示主备切换成功。-bash-4.2$ pg_ctl startwaiting for server to start....2020-06-04 10:24:08.409 CST [95234] LOG: listening on IPv4 address "0.0.0.0", port 54302020-06-04 10:24:08.411 CST [95234] LOG: could not create IPv6 socket for address "::": Address family not supported by protocol2020-06-04 10:24:08.413 CST [95234] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5430"2020-06-04 10:24:08.415 CST [95234] LOG: listening on Unix socket "/tmp/.s.PGSQL.5430"2020-06-04 10:24:08.479 CST [95234] LOG: redirecting log output to logging collector process2020-06-04 10:24:08.479 CST [95234] HINT: Future log output will appear in directory "log".doneserver started-bash-4.2$-bash-4.2$-bash-4.2$ psqlpsql (11.8)Type "help" for help.postgres=# select pg_is_in_recovery();pg_is_in_recovery-------------------t(1 row)
pg_rewind工具
Pg_rewind是流复制维护时一个非常好的数据同步工具,如果上面实验中没有先关闭主库,待备库提升为主库后,pghost0原主库就没办法成功转为备库了,只能再进行全量数据同步初始化,如果数据库很大需要很长时间。这种情况下就可以使用pg_rewind工具只同步差异数据。
使用pg_rewind的前提条件为以下之一:
Postgresql.conf中wal_log_hints参数设置成on ------->>>>>需要重启数据库生效
初始化数据库时initdb使用了--data-checksums选项,这个选项开启后会在数据块上进行检测以发现I/O错误,此选项只能在initdb时设置,开启后性能有损失。
--主库不关的情况下,直接备库激活-bash-4.2$ pg_ctl promotewaiting for server to promote.... doneserver promoted-bash-4.2$ pg_controldata | grep clusterDatabase cluster state: in production--原主库使用pg_rewind工具增量同步原备库上的数据,先关库,pg_rewind中需要指定super user用户-bash-4.2$ pg_ctl stop -m fastwaiting for server to shut down.... doneserver stopped-bash-4.2$-bash-4.2$-bash-4.2$ pg_rewind --target-pgdata $PGDATA --source-server='host=10.0.2.111 port=5432 user=postgres password=postgres' -Pconnected to serverservers diverged at WAL location 1/A9000248 on timeline 6rewinding from last common checkpoint at 1/A9000028 on timeline 6reading source file listreading target file listreading WAL in targetneed to copy 1296 MB (total source directory size is 2521 MB)1327859/1327859 kB (100%) copiedcreating backup label and updating control filesyncing target data directoryDone!-bash-4.2$--修改原主库的recovery.conf文件并启库-bash-4.2$ tail recovery.confrecovery_target_timeline = 'latest'standby_mode = onprimary_conninfo = 'host=10.0.2.111 port=5432 user=repuser password=rep12345'-bash-4.2$ pg_ctl startwaiting for server to start....2019-08-14 16:16:16.089 CST [30332] LOG: listening on IPv4 address "0.0.0.0", port 54322019-08-14 16:16:16.090 CST [30332] LOG: listening on IPv6 address "::", port 54322019-08-14 16:16:16.094 CST [30332] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2019-08-14 16:16:16.111 CST [30332] LOG: redirecting log output to logging collector process2019-08-14 16:16:16.111 CST [30332] HINT: Future log output will appear in directory "log".doneserver started-bash-4.2$
未完待续...
文章转载自长河的笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




