久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

怎么使用pg

148次閱讀
沒有評論

共計 6287 個字符,預計需要花費 16 分鐘才能閱讀完成。

本篇內容主要講解“怎么使用 pg_rewind”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“怎么使用 pg_rewind”吧!

pg_rewind

  是 postgresql 主叢數據庫之同步數據目錄的工具。需要目標服務器在 postgresql.conf 中允許 wal_log_hints,或者在 initdb 初始化集群時允許 checksums,full_page_writes 也必須為 on

  pg_rewind 只復制表數據文件中更改的塊; 所有其他文件都被完整復制,包括配置文件。pg_rewind 相對于使用 pg_basebackup 備份或 rsync 等工具的優勢在于,pg_rewind 不需要讀取數據庫中未更改的塊。這使得在數據庫很大且之間只有一小部分塊不同的情況下,速度會快得多。

  pg_rewind [option…] {-D | –target-pgdata} directory {–source-pgdata=directory | –source-server=connstr

參數:

-D directory –target-pgdata=directory

  此選項指定與源同步的目標數據目錄。在運行 pg_rewind 之前,必須干凈關閉目標服務器

–source-pgdata=directory

  指定要與之同步的源服務器的數據目錄的文件系統路徑。此選項要求干凈關閉源服務器

–source-server=connstr

  指定要連接到源 PostgreSQL 服務器的 libpq 連接字符串。連接必須是具有超級用戶訪問權限的正常 (非復制) 連接。此選項要求源服務器正在運行,而不是處于恢復模式。

-n –dry-run

  除了實際修改目標目錄之外,執行所有操作。

-P –progress

  使進展報告。

實驗使用兩臺主機,都安裝 postgresql-10.7,已配置流復制

主:192.168.56.5 m1

叢:192.168.56.25 m7

m1(主):創建測試表和數據

postgres=# create table test (id int,e_name varchar(100),e_mail varchar(100),d_id int);
CREATE TABLE
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
postgres=# insert into test values(1, zbs , 123@126.com ,10);
INSERT 0 1
postgres=# select * from test;
id | e_name | e_mail | d_id
----+--------+-------------+------
1 | zbs | 123@126.com | 10
(1 row)

m7 (叢):查詢數據復制成功

[postgres@z_leader ~]$ psql postgres
psql (10.7)
Type  help  for help.
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
postgres=# select * from test;
id | e_name | e_mail | d_id
----+--------+-------------+------
1 | zbs | 123@126.com | 10
(1 row)

提升叢庫為新主庫

[postgres@z_leader data]$ pg_ctl promote -D /usr/local/pg/data
waiting for server to promote.... done
server promoted
[postgres@z_leader data]$ psql postgres
psql (10.7)
Type  help  for help.
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
(1 row)

m1(原主庫)插入一條記錄,模擬原主庫上的數據沒有復制到原叢庫上

postgres=# insert into test values(2, zbs1 , 124@126.com ,10);
INSERT 0 1
postgres=# select * from test;
id | e_name | e_mail | d_id
----+--------+-------------+------
1 | zbs | 123@126.com | 10
2 | zbs1 | 124@126.com | 10
(2 rows)

m7:在原叢庫上 (已提升為主庫) 插入一條記錄并查看結果

postgres=# insert into test values(3, zbs2 , 124@126.com ,10);
INSERT 0 1
postgres=# select * from test;
id | e_name | e_mail | d_id
----+--------+-------------+------
1 | zbs | 123@126.com | 10
3 | zbs2 | 124@126.com | 10
(2 rows)

m1 將原主庫變為新主庫的叢庫

[postgres@localhost ~]$ kill -INT `head -1 /usr/local/pg/data/postmaster.pid`

– 配置流復制文件和參數

[postgres@localhost data]$ mv recovery.done recovery.conf
[postgres@localhost data]$ cat recovery.conf
standby_mode =  on 
restore_command =  cp /usr/local/pg/arch/%f 
primary_conninfo =  host=192.168.56.25 port=5432 user=rep 
recovery_target_timeline =  latest
[postgres@localhost data]$

– 啟動數據庫

[postgres@localhost ~]$ /usr/local/pg/bin/pg_ctl -D /usr/local/pg/data -l logfile start
waiting for server to start.... done
server started
[postgres@localhost data]$ psql postgres
psql (10.7)
Type  help  for help.
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
(1 row)
postgres=# select * from test;
id | e_name | e_mail | d_id
----+--------+-------------+------
1 | zbs | 123@126.com | 10
2 | zbs1 | 124@126.com | 10
(2 rows)

– 在 m7 上插入的記錄未能復制過來

— 日志信息

2019-03-02 09:15:17.415 CST [2492] LOG: consistent recovery state reached at 0/D000098
2019-03-02 09:15:17.415 CST [2492] LOG: invalid record length at 0/D000098: wanted 24, got 0
2019-03-02 09:15:17.415 CST [2490] LOG: database system is ready to accept read only connections
2019-03-02 09:15:17.429 CST [2500] LOG: fetching timeline history file for timeline 6 from primary server
2019-03-02 09:15:17.460 CST [2500] FATAL: could not start WAL streaming: ERROR: requested starting point 0/D000000 on timeline 5 is not in this
server s history
DETAIL: This server s history forked from timeline 5 at 0/C003168.
cp: missing destination file operand after `/usr/local/pg/arch/00000006.history 
Try `cp --help  for more information.
cp: missing destination file operand after `/usr/local/pg/arch/00000007.history 
Try `cp --help  for more information.
cp: missing destination file operand after `/usr/local/pg/arch/00000006.history 
Try `cp --help  for more information.
2019-03-02 09:15:17.469 CST [2492] LOG: new timeline 6 forked off current database system timeline 5 before current recovery point 0/D000098
cp: missing destination file operand after `/usr/local/pg/arch/00000005000000000000000D
[postgres@localhost ~]$ kill -INT `head -1 /usr/local/pg/data/postmaster.pid`

— 使得 pg_rewind 同步數據庫時間線

[[postgres@localhost ~]$ pg_rewind --target-pgdata /usr/local/pg/data --source-server= host=192.168.56.25 port=5432 user=postgres dbname=postgres  -P
connected to server
servers diverged at WAL location 0/C003168 on timeline 5
rewinding from last common checkpoint at 0/C003010 on timeline 5
reading source file list
reading target file list
reading WAL in target
need to copy 100 MB (total source directory size is 118 MB)
102599/102599 kB (100%) copied
creating backup label and updating control file
syncing target data directory
Done!

–pg_rewind 后此文件需要重新配置

[postgres@localhost data]$ cat recovery.conf
standby_mode =  on 
restore_command =  cp /usr/local/pg/arch/%f 
primary_conninfo =  host=192.168.56.25 port=5432 user=rep 
recovery_target_timeline =  latest
[postgres@localhost ~]$ /usr/local/pg/bin/pg_ctl -D /usr/local/pg/data -l logfile start
waiting for server to start.... done
server started
[postgres@localhost ~]$ psql postgres
psql (10.7)
Type  help  for help.
postgres=# select * from test;
id | e_name | e_mail | d_id
----+--------+-------------+------
1 | zbs | 123@126.com | 10
3 | zbs2 | 124@126.com | 10
(2 rows)
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
(1 row)

– 原主庫沒有復制到叢庫的記錄消失,在新主庫上插入的記錄已同步

m7(新主庫)
[postgres@z_leader ~]$ psql postgres
psql (10.7)
Type  help  for help.
postgres=# insert into test values(4, zbs2 , 124@126.com ,10);
INSERT 0 1
postgres=# select * from test;
id | e_name | e_mail | d_id
----+--------+-------------+------
1 | zbs | 123@126.com | 10
3 | zbs2 | 124@126.com | 10
4 | zbs2 | 124@126.com | 10
(3 rows)

m1(新叢庫)

postgres=# select * from test;
id | e_name | e_mail | d_id
----+--------+-------------+------
1 | zbs | 123@126.com | 10
3 | zbs2 | 124@126.com | 10
4 | zbs2 | 124@126.com | 10
(3 rows)

到此,相信大家對“怎么使用 pg_rewind”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-24發表,共計6287字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 许昌市| 莱西市| 静乐县| 来凤县| 伊宁县| 定远县| 绩溪县| 阳原县| 微博| 雷波县| 江华| 英山县| 资兴市| 嘉定区| 北流市| 阳谷县| 奎屯市| 沙湾县| 翼城县| 洮南市| 东兰县| 黄冈市| 大方县| 红桥区| 会昌县| 高州市| 华宁县| 界首市| 武川县| 涿州市| 灌云县| 张家口市| 正镶白旗| 永清县| 平罗县| 榕江县| 泌阳县| 东海县| 灵川县| 海门市| 邵阳市|