PostgreSQL主備切換


備庫如何激活

在PostgreSQL(HOT-Standby)如主庫出現異常。備庫如何激活;來替換主庫工作。有下列2種方式

  1. 備庫在recovery.conf文件中有個配置項trigger_file。它是激活standby的觸發文件。當它存在;就會激活standby。
  2. 使用pg_ctl promote來激活。

演示場景

模擬在主庫異常掛掉。備庫來替換主庫工作一段時間。並原主庫切換成新備庫並恢復操作。
環境
主機名    IP地址    角色    數據目錄
postgres202    192.168.1.202    primary    /home/postgres/data
postgres203    192.168.1.203    standby    /home/postgres/data

postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid              | 7687
usesysid         | 24576
usename          | replica
application_name | walreceiver
client_addr      | 192.168.1.203
client_hostname  | 
client_port      | 60234
backend_start    | 2017-09-04 22:08:26.249452+08
backend_xmin     | 1680
state            | streaming
sent_location    | 0/B0000D0
write_location   | 0/B0000D0
flush_location   | 0/B0000D0
replay_location  | 0/B0000D0
sync_priority    | 0
sync_state       | async

2.1 模擬主庫關機
[postgres@postgres202 ~]$ pg_stop
waiting for server to shut down........ done
server stopped

2.2 激活備庫;
模擬新主庫工作一段時間。操作刪除T2表和新建T3表
[postgres@postgres203 data]$ pg_ctl promote
server promoting

[postgres@postgres203 ~]$ psql lottu lottu
psql (9.6.0)
Type "help" for help.

lottu=> \dt
       List of relations
 Schema | Name | Type  | Owner 
--------+------+-------+-------
 lottu  | t    | table | lottu
 lottu  | t2   | table | lottu
(2 rows)

lottu=> drop table t2;
DROP TABLE
                                        
lottu=> create table t3 as select * from t;
SELECT 12

#切換日志;進行歸檔。
[postgres@postgres203 ~]$ psql
psql (9.6.0)
Type "help" for help.

postgres=# select pg_switch_xlog();
 pg_switch_xlog 
----------------
 0/C018FC0
(1 row)

postgres=# select pg_switch_xlog();
 pg_switch_xlog 
----------------
 0/D000078
(1 row)

postgres=# select pg_switch_xlog();
 pg_switch_xlog 
----------------
 0/E000000
(1 row)

2.3 恢復主庫
1. 用pg_rewind命令同步新備庫。
[postgres@postgres203 ~]$ pg_rewind -?
pg_rewind resynchronizes a PostgreSQL cluster with another copy of the cluster.

Usage:
  pg_rewind [OPTION]...

Options:
  -D, --target-pgdata=DIRECTORY  existing data directory to modify
      --source-pgdata=DIRECTORY  source data directory to synchronize with
      --source-server=CONNSTR    source server to synchronize with
  -n, --dry-run                  stop before modifying anything
  -P, --progress                 write progress messages
      --debug                    write a lot of debug messages
  -V, --version                  output version information, then exit
  -?, --help                     show this help, then exit

Report bugs to <pgsql-bugs@postgresql.org>.

[postgres@postgres202 ~]$ pg_rewind --target-pgdata $PGDATA --source-server='host=192.168.1.203 port=5432 user=postgres dbname=postgres' -P
connected to server
servers diverged at WAL position 0/C000098 on timeline 1
rewinding from last common checkpoint at 0/C000028 on timeline 1
reading source file list
reading target file list
reading WAL in target
need to copy 203 MB (total source directory size is 221 MB)
208264/208264 kB (100%) copied
creating backup label and updating control file
syncing target data directory
Done!

[postgres@postgres202 data]$ mv recovery.done recovery.conf
[postgres@postgres202 data]$ vi recovery.conf
standby_mode = on  # 這個說明這台機器為從庫
primary_conninfo = 'host=192.168.1.203 port=5432 user=replica password=replica'  # 這個說明這台機器對應主庫的信息
recovery_target_timeline = 'latest' # 這個說明這個流復制同步到最新的數據

由於postgresql.conf是同步過來的;所有不做修改。也有幾個地方要進行修改
max_connections = 200              # 一般查多於寫的應用從庫的最大連接數要比較大
hot_standby = on                   # 說明這台機器不僅僅是用於數據歸檔,也用於數據查詢
max_standby_streaming_delay = 30s  # 數據流備份的最大延遲時間
wal_receiver_status_interval = 10s # 多久向主報告一次從的狀態,當然從每次數據復制都會向主報告狀態,這里只是設置最長的間隔時間
hot_standby_feedback = on          # 如果有錯誤的數據復制,是否向主進行反饋

【驗證】
[postgres@postgres202 data]$ pg_start
server starting
[postgres@postgres202 data]$ psql lottu lottu
psql (9.6.0)
Type "help" for help.

lottu=> \dt
       List of relations
 Schema | Name | Type  | Owner 
--------+------+-------+-------
 lottu  | t    | table | lottu
 lottu  | t3   | table | lottu
(2 rows)

[postgres@postgres203 data]$ psql
psql (9.6.0)
Type "help" for help.

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid              | 4193
usesysid         | 24576
usename          | replica
application_name | walreceiver
client_addr      | 192.168.1.202
client_hostname  | 
client_port      | 60078
backend_start    | 2017-09-02 07:56:08.390662+08
backend_xmin     | 1688
state            | streaming
sent_location    | 0/F01EEA8
write_location   | 0/F01EEA8
flush_location   | 0/F01EEA8
replay_location  | 0/F01EEA8
sync_priority    | 0
sync_state       | async

參考文獻:http://www.cnblogs.com/songyuejie/p/4976171.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM