1. 程式人生 > >Postgresql 非同步流複製 詳解及配置切換

Postgresql 非同步流複製 詳解及配置切換

####### 1, postgresql移步同步 and 主備切換 2, postgresql 同步 and 主備切換 3, archive 資料同步 參考: 參考: <<從小工到專家>> postgres hot_standby 主從配置部署: (hot_standy) 版本: postgresql 9.6.2 Centos 6.8 x86 64bit 目錄: /usr/local/pgsql* --安裝目錄 /data/postgres/posdb --資料目錄 配置檔案: pg_hba.conf 網管控制(配置引數) postgres.conf 資料庫引數配置檔案 recovery.conf 備庫需手動建立的 配置同步引數檔案 主庫: 10.0.1.82 埠: 5438 備庫: 192.168.41.212 埠: 5438 流複製: (原理詳解)
9.0開始提供的一種新的傳遞WAL 日誌的方式, 只要primary 資料庫 一產生日誌, 就會傳遞到standby 資料庫. (1,非同步,2,同步) 兩種方式. (9.2 增加了級聯複製功能), 9.0之前, 主從需第三方同步拷貝 資料同步方式時: 在primary 提交事務時, 一定會等到WAL 日誌傳遞到standby 後才返回, 這樣可以得到standby 資料完全和 primary 資料庫同步. 沒有一點落後.(自動切換,可以達到零丟失) 資料非同步同步方式: Primary 提交事務後,不必等日誌傳遞到standby 就即可返回. 所以standby 資料庫通常 比primary 落後很少.
standby 執行原理: postgresql 資料庫異常中止後, 資料庫重啟,,會重放停機前最後一個checkpoint 點之後的 WAL 日誌. 再把資料庫恢復到停機時的狀態. 建立standby 的過程分為倆大步驟: a, 生成一個基礎備份 , 通過pg_start_backup('db_name'), pg_stop_backup(); select pg_start_backup('target') --Ps: 可以使用pg_basebackup 來 完成基礎備份的步驟. b, 拷貝備份過程中的所有WAL 日誌檔案. pg_basebackup 命令簡介: 這個工具會把整個資料庫例項都拷貝出來, 而
不只是把例項中的部分(某個資料庫或某張表) 單獨備份出來. 該工具需要配置 pg_hba.conf 引數檔案
引數: -D directory 或 --pgdata=directory 指定把備份寫到那個目錄.(如果目錄不存在,會自動建立) -F format 或 --format=format : 指定輸出的格式 目前支援兩種格式: a, 原樣輸出, 這種情況下 format 指定為" p" 或"plain"; b, tar格式, 這種相當於把輸出的備份檔案打包一個tar檔案中, 指定為"t" 或"tar" -x 或 --xlog : 備份時會把備份中產生的xlog 檔案也自動備份出來. 這樣在資料庫恢復時,應用這些xlog 檔案,把資料庫推到一個一致點. 設定這個選項需要設定 wal_keep_segments 引數(以抱枕在備份過程中,需要的WAL日誌檔案不會被覆蓋). 與選項 -X fetch 完全一樣. -X method 或 xlog-method=method: 可以取的值為"f" , "fetch", "s" ,"stream" , 當為f時,與fetch意義相同, 與-x 引數一樣. 當為"s", stream 表示意思也相同,表示備份開始後,啟動另一個流複製從主庫接收WAL日誌.(這種方式避免了,使用-X f時, 主庫上的WAL日誌有可能被覆蓋從而導致失敗的問題.), 使用這種連線,主庫需要配置 max_wal_senders 至少需要大於2 -z 或 --gzip, 僅能與tar 輸出模式配合使用. 表示tar 備份是經過gzip 壓縮的. 生成tar.gz 備份包. -Z level 或 --compress=level, 指定gzip 壓縮級別(1-9) , 級別越高,耗CPU 越厲害. -c fast | spread 或 --checkpoint=fast | spread : 設定checkpoint 的模式是fast還是spread . -l label 或 --label=label , 指定備份的一個標示. 備份的標識是一個任意字串,便於今後維護識別. -P 或 --progress , 允許在備份過程中實時列印備份進度. -v 或--verbose , 詳細模式,在使用-P 引數後,會打印出正在備份的具體的資訊. -V 或 --version, 列印pg_basebackup 的版本後,退出. ---------------------------- -h host 或 --host =host , 執行連線資料庫的IP地址或主機名 -p port 或 --port=port 指定連線埠 -s interval 或 --status-interval = interval: 指定向伺服器週期反饋狀態的秒數, 如果伺服器配置了 流複製的超時,在使用--xlog=stream 選項時, 這需要設定這個引數. 預設為10s, 如果設定為0 , 表示不向伺服器反饋狀態. eg: pg_basebackup -F p --progress -D /data/postgresql/postdb -h 10.0.1.82 -p 5438 -U replica --password (基礎備份) ############# 我是華麗分界線 ################ 安裝postgresql : groupadd -g 1200 postgres useradd -m -u 1100 -g postgres postgres mkdir -pv /data/PostgreSQL/{postgresdb,postgreslog} gunzip postgresql-9.6.2-3-linux-x64-binaries.tar.gz tar -xvf postgresql-9.6.2-3-linux-x64-binaries.tar -C /usr/local/ 初始化postgressql 資料庫: cd /usr/local/pgsql-9.6.2/bin ./initdb -E utf8 -D /data/PostgreSQL/postgresdb 主庫: postgres.conf 配置 (4GB-Memory) ## authentic setting ## port = 5438 max_connections = 500 unix_socket_directories = '/tmp' listen_addresses = '*' superuser_reserved_connections = 5 #uthentication_timeout = 60 deadlock_timeout =2000 max_locks_per_transaction = 64 ## base setting ## datestyle = 'iso, mdy' timezone = 'PRC' lc_messages = 'en_US.UTF-8' lc_monetary = 'en_US.UTF-8' lc_numeric = 'en_US.UTF-8' lc_time = 'en_US.UTF-8' default_text_search_config = 'pg_catalog.english' ## log setting ## logging_collector = on log_destination = 'csvlog' log_directory = '/data/postgres/poslog' # -- every day on logs -- # log_filename = 'pglog5438-%Y-%m-%d_%H%M%S.log' log_file_mode = 0600 log_connections = on log_disconnections = off log_checkpoints = on log_rotation_age = 1d log_rotation_size = 200MB log_truncate_on_rotation = off log_timezone = 'PRC' log_lock_waits = on log_min_duration_statement = 3s log_lock_waits = on log_min_messages = info log_min_error_statement = info ## memory setting ## shared_buffers = 512MB temp_buffers = 16MB work_mem = 32MB effective_cache_size = 2GB maintenance_work_mem = 128MB #max_stack_depth = 2MB dynamic_shared_memory_type = posix ## WAL setting ## fsync = on synchronous_commit = on wal_sync_method = fdatasync # -- yixia is defaults -- # full_page_writes = on wal_buffers = 16MB wal_writer_delay = 200ms commit_delay = 0 commit_siblings = 5 #wal_level = archive #wal_level = hot_standby archive_mode = on archive_command = 'cp %p /data/postgres/pos_archive/%f' ## statistic setting ## track_activities = on log_statement_stats = on # log_parser_stats = # log_planner_stats = # log_executor_stats = autovacuum = on track_counts = on ## async standby setting ## max_wal_senders = 5 wal_level = hot_standby #listen_addresses = '*' wal_keep_segments = 10240 wal_sender_timeout = 120s ## 初次需要修改引數 ## listener_addresses = '*' --可以是IP, 也可以是 ' * ' 替代. wal_level = hot_standby --熱備模式開啟 max_wal_sender = 5 --可以並行設定幾個流複製連線程序.( 幾個從,設定幾個) wal_keep_segments = 10240 #重要配置 流複製 wal_send_timeout = 60s --可防止邏輯錯誤,延緩同步時間. max_connections = 512 --standby此引數設定最好比primary 設定的要大. archive_mode = on --允許歸檔 archive_command = 'cp % /data/postgreslog/archivelog/%f' --歸檔路徑 關於wal 日誌歸檔描述: --以上是主庫設定. 編輯環境變數: [[email protected] ~]$ cat .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH export DATADIR=/data/postgres/posdb export BASEDIR=/usr/local/pgsql-9.6.2 export PATH=$BASEDIR/bin:$PATH:$HOME/bin:$PATH 編輯 pg_hba.conf 配置引數檔案: # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust host all all 0.0.0.0/0 trust #因本地測試環境,我全放開 # Allow replication connections from localhost, by a user with the # replication privilege. #local replication postgres trust #host replication postgres 127.0.0.1/32 trust host replication postgres ::1/128 trust host replication replica 192.168.41.0/24 trust #流複製IP target 端 (網段) host replication replica 10.0.1.0/24 trust #流複製IP target 端 (網段) --replication 表示支援流複製. 使用的是replica 使用者 啟動主庫, postgresql 資料庫 a, 通過 /usr/local/pgsql-9.6.2/bin/postgres -D /data/postgres/posdb b, pg_ctl start -D /data/postgres/posdb 檢測 啟動程序 [[email protected] posdb]# lsof -i:5438 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME postgres 11734 postgres 3u IPv4 19736071 0t0 TCP *:5438 (LISTEN) postgres 11734 postgres 4u IPv6 19736072 0t0 TCP *:5438 (LISTEN) postgres 11960 postgres 10u IPv4 19738713 0t0 TCP limin-test.novalocal:5438->192.168.41.212:56398 (ESTABLISHED) # 備庫設定: a, 如果為開始的初始話資料庫, 不需要刪除 /data/目錄下的檔案. 如果不是,最好刪除. b, 備份複製: pg_basebackup -F p --progress -D /data/postgresql/postdb -h 10.0.1.82 -p 5438 -U replica --password c, 手動建立recovery.conf 配置檔案: [[email protected] postdb]$ cat recovery.conf standby_mode = on trigger_file = '/data/postgresql/postlog/trigger.kenyou' recovery_target_timeline = 'latest' restore_command = 'cp %p /data/postgresql/postdb/%f' primary_conninfo = 'host=10.0.1.82 port=5438 user=replica password=replica' d, 根據主庫的postgresql.conf 配置檔案 修改引數配置: 1, listen_addresses = '* ' 2, wal_level = hot_standby 3, max_connections = xxx, # 一般比主庫大一些. 4,hot_standby = on 5, max_standby_streaming_delay = 30s 6. wal_receiver_status_interval = 10s 7, hot_standby_feedback = on # 如果有錯誤的資料複製,是否向 主進行範例. ### 備庫 postgresql.conf 配置檔案 #### [[email protected] postdb]$ cat recovery.conf standby_mode = on trigger_file = '/data/postgresql/postlog/trigger.kenyou' recovery_target_timeline = 'latest' restore_command = 'cp %p /data/postgresql/postdb/%f' primary_conninfo = 'host=10.0.1.82 port=5438 user=replica password=replica' [[email protected] postdb]$ cat postgresql.conf ## authentic setting ## port = 5438 max_connections = 550 unix_socket_directories = '/tmp' listen_addresses = '*' superuser_reserved_connections = 5 #uthentication_timeout = 60 deadlock_timeout =2000 max_locks_per_transaction = 64 ## base setting ## datestyle = 'iso, mdy' timezone = 'PRC' lc_messages = 'en_US.UTF-8' lc_monetary = 'en_US.UTF-8' lc_numeric = 'en_US.UTF-8' lc_time = 'en_US.UTF-8' default_text_search_config = 'pg_catalog.english' ## log setting ## logging_collector = on log_destination = 'csvlog' log_directory = '/data/postgresql/postlog' # -- every day on logs -- # log_filename = 'pglog5438-%Y-%m-%d_%H%M%S.log' log_file_mode = 0600 log_connections = on log_disconnections = off log_checkpoints = on log_rotation_age = 1d log_rotation_size = 200MB log_truncate_on_rotation = off log_timezone = 'PRC' log_lock_waits = on log_min_duration_statement = 3s log_lock_waits = on log_min_messages = info log_min_error_statement = info ## memory setting ## shared_buffers = 512MB temp_buffers = 16MB work_mem = 32MB effective_cache_size = 2GB maintenance_work_mem = 128MB #max_stack_depth = 2MB dynamic_shared_memory_type = posix ## WAL setting ## fsync = on synchronous_commit = on wal_sync_method = fdatasync # -- yixia is defaults -- # full_page_writes = on wal_buffers = 16MB wal_writer_delay = 200ms commit_delay = 0 commit_siblings = 5 #wal_level = archive #wal_level = hot_standby archive_mode = on archive_command = 'cp %p /data/postgresql/postdb/%f' #restore_command = 'cp %p /data/postgresql/postdb/%f' ## statistic setting ## track_activities = on log_statement_stats = on autovacuum = on track_counts = on ## async standby setting ## max_wal_senders = 5 wal_level = hot_standby #listen_addresses = '*' wal_keep_segments = 10240 wal_sender_timeout = 120s hot_standby = on hot_standby_feedback = on max_standby_streaming_delay = 20s wal_receiver_status_interval = 1s ## 啟動備庫: pg_ctl start -D /data..... ##檢測主從 複製狀態: (主庫上檢視) select client_addr, sync_state from pg_stat_replication; postgres=# select client_addr, sync_state from pg_stat_replication; client_addr | sync_state ----------------+------------ 192.168.41.212 | async (1 row) postgres=# postgres=# select * from pg_stat_replication; pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | ba ckend_start | backend_xmin | state | sent_location | write_location | flush_location | replay_lo cation | sync_priority | sync_state -------+----------+---------+------------------+----------------+-----------------+-------------+----------- --------------------+--------------+-----------+---------------+----------------+----------------+---------- -------+---------------+------------ 11960 | 16568 | replica | walreceiver | 192.168.41.212 | | 56398 | 2017-08-16 17:37:45.876011+08 | 1761 | streaming | 0/801DB70 | 0/801DB70 | 0/801DB70 | 0/801DB70 | 0 | async (1 row) ## 資料 ddl, dml 操作測試: ##standby 只讀, 所以任何操作, 都會告警失敗: postgres=# drop database martinli; ERROR: cannot execute DROP DATABASE in a read-only transaction [[email protected] ~]$ netstat -lntup|grep 5438 && ps -ef|grep postmaster (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:5438 0.0.0.0:* LISTEN 11734/postgres tcp 0 0 :::5438 :::* LISTEN 11734/postgres 主庫 sender : [[email protected] ~]$ ps -ef |grep postgres |grep sender postgres 11960 11734 0 Aug16 ? 00:00:19 postgres: wal sender process replica 192.168.41.212(56398) streaming 0/801F168 postgres 24621 20934 0 16:56 pts/0 00:00:00 grep sender 備庫 recover : [[email protected] postdb]$ ps -ef |grep postgres |grep recover postgres 27798 27796 0 Aug16 ? 00:00:00 postgres: startup process recovering 000000010000000000000008 postgres 28948 28759 0 08:53 pts/4 00:00:00 grep recover ----------------- 非同步 同步部署完成 ------------ 非同步postgresql 主備切換: 1, 停止主庫: [[email protected] ~]$ pg_ctl stop -D /data/postgres/posdb/ waiting for server to shut down....... done server stopped ------ 主庫 log 日誌: 2017-08-17 17:00:46.522 CST,,,11734,,59940c60.2dd6,5,,2017-08-16 17:12:00 CST,,0,LOG,00000,"database system is shut down",,,,,,,,,"" -------- 此時備庫的日誌: ",,,,,,,,,"" 2017-08-17 08:58:19.789 CST,,,28962,,5994ea2b.7122,1,,2017-08-17 08:58:19 CST,,0,FATAL,XX000,"could not connect to the primary server: could not connect to server: Connection refused Is the server running on host ""10.0.1.82"" and accepting TCP/IP connections on port 5438? ",,,,,,,,,"" ---------- 2, 檢視備庫的 recovery.conf 引數檔案, 在對應的目錄下建立 trigger 檔案. [[email protected] postdb]$ cat recovery.conf standby_mode = on trigger_file = '/data/postgresql/postlog/trigger.kenyou' recovery_target_timeline = 'latest' restore_command = 'cp %p /data/postgresql/postdb/%f' primary_conninfo = 'host=10.0.1.82 port=5438 user=replica password=replica' cd /data/postgresql/postlog/ touch trigger.kenyou # 這一步很重要. 建立後, ll 檢視,並沒有什麼, 但是, 日誌及recovery.conf 檔案變了: --------------- 資料狀態檢視------ 通過pg_controldata : 未建立 (touch trigger.kenyou) 觸發檔案之前的 狀態資訊: 備庫: [[email protected] postlog]$ pg_controldata -D /data/postgresql/postdb/ pg_control version number: 960 Catalog version number: 201608131 Database system identifier: 6410192200887248642 Database cluster state: in archive recovery # 提示恢復狀態. pg_control last modified: Thu 17 Aug 2017 08:59:15 AM CST Latest checkpoint location: 0/9000028 Prior checkpoint location: 0/801F1A0 Latest checkpoint's REDO location: 0/9000028 Latest checkpoint's REDO WAL file: 000000010000000000000009 Latest checkpoint's TimeLineID: 1 Latest checkpoint's PrevTimeLineID: 1 Latest checkpoint's full_page_writes: on Latest checkpoint's NextXID: 0:1761 Latest checkpoint's NextOID: 16571 Latest checkpoint's NextMultiXactId: 1 Latest checkpoint's NextMultiOffset: 0 Latest checkpoint's oldestXID: 1668 Latest checkpoint's oldestXID's DB: 1 Latest checkpoint's oldestActiveXID: 0 Latest checkpoint's oldestMultiXid: 1 Latest checkpoint's oldestMulti's DB: 1 Latest checkpoint's oldestCommitTsXid:0 Latest checkpoint's newestCommitTsXid:0 Time of latest checkpoint: Thu 17 Aug 2017 05:00:44 PM CST Fake LSN counter for unlogged rels: 0/1 Minimum recovery ending location: 0/9000098 Min recovery ending loc's timeline: 1 Backup start location: 0/0 Backup end location: 0/0 End-of-backup record required: no wal_level setting: replica wal_log_hints setting: off max_connections setting: 500 max_worker_processes setting: 8 max_prepared_xacts setting: 0 max_locks_per_xact setting: 64 track_commit_timestamp setting: off Maximum data alignment: 8 Database block size: 8192 Blocks per segment of large relation: 131072 WAL block size: 8192 Bytes per WAL segment: 16777216 建立 (touch trigger.kenyou) 觸發檔案之後的 狀態資訊: 備庫: [[email protected] postlog]$ pg_controldata -D /data/postgresql/postdb/ pg_control version number: 960 Catalog version number: 201608131 Database system identifier: 6410192200887248642 Database cluster state: in production # 變法了. pg_control last modified: Thu 17 Aug 2017 09:10:30 AM CST Latest checkpoint location: 0/9000138 Prior checkpoint location: 0/9000028 Latest checkpoint's REDO location: 0/9000100 Latest checkpoint's REDO WAL file: 000000020000000000000009 Latest checkpoint's TimeLineID: 2 Latest checkpoint's PrevTimeLineID: 2 Latest checkpoint's full_page_writes: on Latest checkpoint's NextXID: 0:1761 Latest checkpoint's NextOID: 16571 Latest checkpoint's NextMultiXactId: 1 Latest checkpoint's NextMultiOffset: 0 Latest checkpoint's oldestXID: 1668 Latest checkpoint's oldestXID's DB: 1 Latest checkpoint's oldestActiveXID: 1761 Latest checkpoint's oldestMultiXid: 1 Latest checkpoint's oldestMulti's DB: 1 Latest checkpoint's oldestCommitTsXid:0 Latest checkpoint's newestCommitTsXid:0 Time of latest checkpoint: Thu 17 Aug 2017 09:10:30 AM CST Fake LSN counter for unlogged rels: 0/1 Minimum recovery ending location: 0/0 Min recovery ending loc's timeline: 0 Backup start location: 0/0 Backup end location: 0/0 End-of-backup record required: no wal_level setting: replica wal_log_hints setting: off max_connections setting: 550 max_worker_processes setting: 8 max_prepared_xacts setting: 0 max_locks_per_xact setting: 64 track_commit_timestamp setting: off Maximum data alignment: 8 Database block size: 8192 Blocks per segment of large relation: 131072 WAL block size: 8192 Bytes per WAL segment: 16777216 Maximum length of identifiers: 64 Maximum columns in an index: 32 Maximum size of a TOAST chunk: 1996 Size of a large-object chunk: 2048 Date/time type storage: 64-bit integers Float4 argument passing: by value Float8 argument passing: by value Data page checksum version: 0 同時,恢復配置檔案由 recovery.conf 變為了 recovery.done [[email protected] postdb]$ cat recovery.done standby_mode = on trigger_file = '/data/postgresql/postlog/trigger.kenyou' recovery_target_timeline = 'latest' restore_command = 'cp %p /data/postgresql/postdb/%f' primary_conninfo = 'host=10.0.1.82 port=5438 user=replica password=replica' ------log 日誌狀態 ------ ",,,,,,,,,"" 2017-08-17 09:10:25.520 CST,,,29138,,5994ed01.71d2,1,,2017-08-17 09:10:25 CST,,0,FATAL,XX000,"could not connect to the primary server: could not connect to server: Connection refused Is the server running on host ""10.0.1.82"" and accepting TCP/IP connections on port 5438? ",,,,,,,,,"" ### 這裡是未建立 trigger 之前的提示. 2017-08-17 09:10:30.522 CST,,,27798,,5993a108.6c96,6,,2017-08-16 09:34:00 CST,1/0,0,LOG,00000,"trigger file found: /data/postgresql/postlog/trigger.kenyou",,,,,,,,,"" ## 這裡找到了一個trigger 檔案, (touch trigger.kenyou 後) 2017-08-17 09:10:30.522 CST,,,27798,,5993a108.6c96,7,,2017-08-16 09:34:00 CST,1/0,0,LOG,00000,"redo done at 0/9000028",,,,,,,,,"" 2017-08-17 09:10:30.522 CST,,,27798,,5993a108.6c96,8,,2017-08-16 09:34:00 CST,1/0,0,LOG,00000,"last completed transaction was at log time 2017-08-16 17:57:07.604087+08",,,,,,,,,"" 2017-08-17 09:10:30.587 CST,,,27798,,5993a108.6c96,9,,2017-08-16 09:34:00 CST,1/0,0,LOG,00000,"selected new timeline ID: 2",,,,,,,,,"" 2017-08-17 09:10:30.788 CST,,,27798,,5993a108.6c96,10,,2017-08-16 09:34:00 CST,1/0,0,LOG,00000,"archive recovery complete",,,,,,,,,"" 2017-08-17 09:10:30.844 CST,,,27798,,5993a108.6c96,11,,2017-08-16 09:34:00 CST,1/0,0,LOG,00000,"MultiXact member wraparound protections are now enabled",,,,,,,,,"" 2017-08-17 09:10:30.868 CST,,,27800,,5993a109.6c98,844,,2017-08-16 09:34:01 CST,,0,LOG,00000,"checkpoint starting: force",,,,,,,,,"" 2017-08-17 09:10:30.870 CST,,,27796,,5993a108.6c94,3,,2017-08-16 09:34:00 CST,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,"" 2017-08-17 09:10:30.870 CST,,,29142,,5994ed06.71d6,1,,2017-08-17 09:10:30 CST,,0,LOG,00000,"autovacuum launcher started",,,,,,,,,"" 2017-08-17 09:10:30.940 CST,,,27800,,5993a109.6c98,845,,2017-08-16 09:34:01 CST,,0,LOG,00000,"checkpoint complete: wrote 0 buffers (0.0%); 0 transaction log file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.000 s, total=0.072 s; sync files=0, longest=0.000 s, average=0.000 s; distance=0 kB, estimate=14633 kB",,,,,,,,,"" 2017-08-17 09:15:30.946 CST,,,27800,,5993a109.6c98,846,,2017-08-16 09:34:01 CST,,0,LOG,00000,"checkpoint starting: time",,,,,,,,,"" 2017-08-17 09:15:31.091 CST,,,27800,,5993a109.6c98,847,,2017-08-16 09:34:01 CST,,0,LOG,00000,"checkpoint complete: wrote 1 buffers (0.0%); 0 transaction log file(s) added, 0 removed, 0 recycled; write=0.102 s, sync=0.018 s, total=0.144 s; sync files=1, longest=0.018 s, average=0.018 s; distance=0 kB, estimate=13170 kB",,,,,,,,,"" -------------- 主庫: [[email protected] posdb]$ pg_controldata -D /data/postgres/posdb/ pg_control version number: 960 Catalog version number: 201608131 Database system identifier: 6410192200887248642 Database cluster state: shut down ## 因關閉了, 所以主庫的狀態不會改變. pg_control last modified: Thu 17 Aug 2017 05:00:46 PM CST Latest checkpoint location: 0/9000028 Prior checkpoint location: 0/801F1A0 Latest checkpoint's REDO location: 0/9000028 Latest checkpoint's REDO WAL file: 000000010000000000000009 Latest checkpoint's TimeLineID: 1 Latest checkpoint's PrevTimeLineID: 1 Latest checkpoint's full_page_writes: on Latest checkpoint's NextXID: 0:1761 Latest checkpoint's NextOID: 16571 Latest checkpoint's NextMultiXactId: 1 Latest checkpoint's NextMultiOffset: 0 Latest checkpoint's oldestXID: 1668 Latest checkpoint's oldestXID's DB: 1 Latest checkpoint's oldestActiveXID: 0 Latest checkpoint's oldestMultiXid: 1 Latest checkpoint's oldestMulti's DB: 1 Latest checkpoint's oldestCommitTsXid:0 Latest checkpoint's newestCommitTsXid:0 Time of latest checkpoint: Thu 17 Aug 2017 05:00:44 PM CST Fake LSN counter for unlogged rels: 0/1 Minimum recovery ending location: 0/0 Min recovery ending loc's timeline: 0 Backup start location: 0/0 Backup end location: 0/0 End-of-backup record required: no wal_level setting: replica wal_log_hints setting: off max_connections setting: 500 max_worker_processes setting: 8 max_prepared_xacts setting: 0 max_locks_per_xact setting: 64 track_commit_timestamp setting: off Maximum data alignment: 8 Database block size: 8192 Blocks per segment of large relation: 131072 WAL block size: 8192 Bytes per WAL segment: 16777216 Maximum length of identifiers: 64 Maximum columns in an index: 32 Maximum size of a TOAST chunk: 1996 Size of a large-object chunk: 2048 Date/time type storage: 64-bit integers Float4 argument passing: by value Float8 argument passing: by value Data page checksum version: 0 新主庫監聽檢測: [[email protected] postlog]$ ps -ef |grep pos |grep sender postgres 29186 27796 0 09:27 ? 00:00:00 postgres: wal sender process replica 10.0.1.82(16143) streaming 0/9000480 postgres 29200 28408 0 09:30 pts/3 00:00:00 grep sender 3, 把 原備庫的 recover.conf 檔案 拷貝至原主庫,修改對應資訊: [[email protected] posdb]$ cat recovery.conf standby_mode = on trigger_file = '/data/postgres/posdb/trigger.kenyou' ## 這裡建立需要注意,與原主對應. recovery_target_timeline = 'latest' restore_command = 'cp %p /data/postgres/poslog/%f' #primary_conninfo = 'host=10.0.1.82 port=5438 user=replica password=replica' primary_conninfo = 'host=192.168.41.212 port=5438 user=replica password=replica' 4, 啟動原主庫 (現在的備庫) pg_ctl start -D /data/postgres/posdb/ --------------- [[email protected] posdb]$ ps -ef |grep pos |grep recover postgres 24823 24821 0 17:31 ? 00:00:00 postgres: startup process recovering 000000020000000000000009 postgres 24834 21610 0 17:31 pts/2 00:00:00 grep recover --狀態檢測(新備庫) [[email protected] posdb]$ pg_controldata -D /data/postgres/posdb/ pg_control version number: 960 Catalog version number: 201608131 Database system identifier: 6410192200887248642 Database cluster state: in archive recovery pg_control last modified: Thu 17 Aug 2017 05:29:05 PM CST Latest checkpoint location: 0/9000028 Prior checkpoint location: 0/9000028 Latest checkpoint's REDO location: 0/9000028 Latest checkpoint's REDO WAL file: 000000010000000000000009 Latest checkpoint's TimeLineID: 1 Latest checkpoint's PrevTimeLineID: 1 Latest checkpoint's full_page_writes: on Latest checkpoint's NextXID: 0:1761 Latest checkpoint's NextOID: 16571 驗證: 在新主庫(原主庫上) martinli=# select * from pg_stat_replication martinli-# ;