您好,登錄后才能下訂單哦!
這篇文章主要介紹“PostgreSQL主從切換實例測試分析”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“PostgreSQL主從切換實例測試分析”文章能幫助大家解決問題。
在PostgreSQL(HOT-Standby)如主庫出現異常。備庫如何激活;來替換主庫工作。有下列2種方式
備庫在recovery.conf文件中有個配置項trigger_file。它是激活standby的觸發文件。當它存在;就會激活standby。
使用pg_ctl promote來激活。
模擬演示主庫異常關機,將備庫切換為主庫,然后原主庫修復后切換為新的備庫繼續工作。
主機名 | IP地址 | 角色 | 數據目錄 |
master | 192.168.20.133 | 主庫 | /var/lib/pgsql/11/data |
slave | 192.168.20.134 | 備庫 | /var/lib/pgsql/11/data |
主庫
lei=# select * from pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 3274 usesysid | 16774 usename | repuser application_name | walreceiver client_addr | 192.168.20.134 client_hostname | slave client_port | 49896 backend_start | 2019-05-30 02:40:58.253032-04 backend_xmin | state | streaming sent_lsn | 0/180003C8 write_lsn | 0/180003C8 flush_lsn | 0/180003C8 replay_lsn | 0/180003C8 write_lag | flush_lag | replay_lag | sync_priority | 0 sync_state | async
[root@master data]# systemctl stop postgresql-11
作為新主庫運行,刪除數據庫lei中表test并創建表tt
[postgres@slave ~]$ pg_ctl -D /var/lib/pgsql/11/data/ promote waiting for server to promote.... done server promoted
刪除表test,創建表tt
[postgres@slave ~]$ psql lei; psql (11.3) Type "help" for help. lei=# \dt List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | lei | table | postgres public | t | table | postgres public | test | table | postgres (3 rows) lei=# drop table test; DROP TABLE lei=# create table tt(id int); CREATE TABLE
手動切換幾次WAL日志
lei=# select pg_switch_wal(); pg_switch_wal --------------- 0/19019058 (1 row) lei=# select pg_switch_wal(); pg_switch_wal --------------- 0/1A000078 (1 row) lei=# select pg_switch_wal(); pg_switch_wal --------------- 0/1B000000 (1 row)
用pg_rewind命令同步新備庫
[postgres@master ~]$ pg_rewind --target-pgdata /var/lib/pgsql/11/data/ --source-server='host=slave port=5432 user=postgres dbname=postgres' -P connected to server servers diverged at WAL location 0/19000098 on timeline 3 rewinding from last common checkpoint at 0/19000028 on timeline 3 reading source file list reading target file list reading WAL in target need to copy 133 MB (total source directory size is 165 MB) 136230/136230 kB (100%) copied creating backup label and updating control file syncing target data directory Done!
修改recovery.conf文件
由于配置是同步過來的,所以需要修改一下配置primary_conninfo
[postgres@master ~]$ mv /var/lib/pgsql/11/data/recovery.done /var/lib/pgsql/11/data/recovery.conf [postgres@master ~]$ vi /var/lib/pgsql/11/data/recovery.conf primary_conninfo = 'host=slave port=5432 user=replica password=replica'
啟動新備庫
[root@master data]# systemctl start postgresql-11
查看數據是否同步過來
可以看到表test沒有了,多了tt表
postgres=# \c lei; You are now connected to database "lei" as user "postgres". lei=# \dt List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | lei | table | postgres public | t | table | postgres public | tt | table | postgres (3 rows)
主庫查看進程狀態
lei=# \x Expanded display is on. lei=# select * from pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 8625 usesysid | 16774 usename | repuser application_name | walreceiver client_addr | 192.168.20.133 client_hostname | master client_port | 55306 backend_start | 2019-05-30 03:26:14.645623-04 backend_xmin | state | streaming sent_lsn | 0/1E0000D0 write_lsn | 0/1E0000D0 flush_lsn | 0/1E0000D0 replay_lsn | 0/1E0000D0 write_lag | 00:00:00.001552 flush_lag | 00:00:00.002167 replay_lag | 00:00:00.002169 sync_priority | 0 sync_state | async
如果有異常信息,請查看數據庫日志來定位問題,通常問題都是出現在幾個配置文件中。
pg_hba.conf
postgresql.conf
recovery.conf
至此PG主備就切換完成了!
關于“PostgreSQL主從切換實例測試分析”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。