PostGreSql 12.6 的流复制
流复制允许一台后备服务器比使用基于文件的日志传送更能保持为最新的状态。后备服务器连接到主服务器,主服务器则在 WAL 记录产生时即将它们以流式传送给后备服务器而不必等到 WAL 文件被填充。
默认情况下流复制是异步的。
主库操作:
安装之后,初始化数据库:
[root@pg01 ~]# /usr/pgsql-12/bin/postgresql-12-setup initdb
Initializing database ... OK
[postgres@pg01 ~]$ createdb mydb
[postgres@pg01 ~]$ psql
psql (12.6)
输入 "help" 来获取帮助信息.
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+-------------+-------------+-----------------------
mydb | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行记录)
##登录系统,新建测试DB, mydb
postgres=# CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'secret';
CREATE ROLE
修改参数
[postgres@pg01 data]$ cat pg_hba.conf
host all all 0.0.0.0/0 ident
host replication replicator 192.168.56.11/32 md5
## Salve 服务器IP地址是 11.
[postgres@pg01 data]$ pg_ctl restart -D $PGDATA
##reboot DB
[postgres@pg01 data]$ psql -c "select pg_is_in_recovery()"
pg_is_in_recovery
-------------------
f
(1 行记录)
#false, 是Master 服务器
[postgres@pg01 data]$ psql -x -c "select * from pg_stat_replication"
-[ RECORD 1 ]----+------------------------------
pid | 4064
usesysid | 16385
usename | replicator
application_name | walreceiver
client_addr | 192.168.56.11
client_hostname |
client_port | 47944
backend_start | 2021-04-16 00:04:30.215703+08
backend_xmin |
state | streaming
sent_lsn | 0/3000060
write_lsn | 0/3000060
flush_lsn | 0/3000060
replay_lsn | 0/3000060
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
reply_time | 2021-04-16 00:04:50.920327+08
Salve 服务器操作
[postgres@pg02 12]$ pg_basebackup -h 192.168.56.10 -U replicator -p 5432 -D $PGDATA -Fp -Xs -P -R
口令:
33514/33514 kB (100%), 1/1 表空间
###会自动产生standby.signal,一个标识为slave的空文件,可以用touch 自建
[postgres@pg02 12]$ ls -al
总用量 8
drwx------. 5 postgres postgres 62 4月 16 00:02 .
drwx------. 3 postgres postgres 35 4月 14 15:50 ..
drwxrwxr-x. 2 postgres postgres 6 4月 13 10:54 archive
drwx------. 2 postgres postgres 6 2月 11 09:16 backups
drwx------. 20 postgres postgres 4096 4月 16 00:02 data
-rw-------. 1 postgres postgres 997 4月 14 15:50 initdb.log
[postgres@pg02 data]$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
listen_addresses = '*'
primary_conninfo = 'user=replicator password=secret host=192.168.56.10 port=5432 sslmode=prefer sslcompression=0 gssencmode=prefer krbsrvname=postgres target_session_attrs=any'
[postgres@pg02 data]$ psql -c "select pg_is_in_recovery()"
pg_is_in_recovery
-------------------
t
(1 行记录)
##Ture, 是Salve 服务器
测试结果(Master操作)
postgres=# \c mydb
您现在已经连接到数据库 "mydb",用户 "postgres".
mydb=# create table t01(name char(50));
CREATE TABLE
mydb=# insert into t01 values('qqqqq');
INSERT 0 1
mydb=# insert into t01 values('mmmmm');
INSERT 0 1
mydb=# \q
Salve 查看
[postgres@pg02 data]$ psql -x -c "select * from pg_stat_replication"
(0 行记录)
[postgres@pg02 data]$ psql
psql (12.6)
输入 "help" 来获取帮助信息.
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+-------------+-------------+------------------
-----
mydb | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/post
gres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/post
gres
(4 行记录)
postgres=# \c mydb
您现在已经连接到数据库 "mydb",用户 "postgres".
mydb=# \d
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+------+--------+----------
public | t01 | 数据表 | postgres
(1 行记录)
mydb=# select * from t01;
name
----------------------------------------------------
qqqqq
mmmmm
(2 行记录)
##复制过来了,功能可以了。 比Oracle 的DG 简单多了,和DB2的HADR 差不多有一拼。感觉不错!!!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/6126/viewspace-2768360/ ,如需转载,请注明出处,否则将追究法律责任。