1. 程式人生 > >postgresql 主從復制並切換

postgresql 主從復制並切換

ans 延遲 stopped commit x86_64 x86 mes info lib

1 環境

192.168.19.145 Kylin 3.3 mysqlhq 9.5.2 psql_master
192.168.19.227 Kylin 3.3 mysql3 9.5.2 psql_standby
postgres=# select version();
version
-------------------------------------------------------------------------------------------------------------
PostgreSQL 9.5.2 on x86_64-kylin-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11), 64-bit
prot=5432

2 yum 安裝pgsql

[root@mysqlhq ~]# yum list | grep postgresql
[root@mysqlhq ~]# yum install postgresql-server postgresql-contrib
[root@mysqlhq ~]# service postgresql initdb
[root@mysqlhq ~]# systemctl start postgresql
[root@mysqlhq ~]# systemctl status postgresql
[root@mysqlhq ~]# netstat -lnt|grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
[root@mysqlhq ~]# ps -ef|grep postgres
[root@mysqlhq ~]# su - postgres
-bash-4.2$ psql
psql (9.5.2)
Type "help" for help.

3 修改主庫的配置文件

#default 路徑///var/lib/pgsql/data
[root@mysqlhq ~]# cd /var/lib/pgsql/data/
[root@mysqlhq data]# cp postgresql.conf postgresql.conf.bk20181213
[root@mysqlhq data]# vim postgresql.conf
listen_addresses = ‘*‘
max_connections = 1000
wal_level = hot_standby
#synchronous_commit = on(同步復制,-- 實時,如果需要異步改為off)
checkpoint_timeout = 5min
archive_mode = on
archive_command = ‘/bin/date‘
max_wal_senders = 2
wal_keep_segments = 16
hot_standby = on
max_standby_archive_delay = 300s
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 1s
hot_standby_feedback = on
wal_receiver_timeout = 60s

-bash-4.2$ cat /var/lib/pgsql/data/pg_hba.conf | grep -v ‘^#‘ | grep -v ‘^$‘
local all all peer
host all all 127.0.0.1/32 trust
host all all 10.15.7.115/32 trust
host all all 192.168.19.145/32 md5
host all all ::1/128 ident
host replication repuser 192.168.19.227/32 md5

[root@mysqlhq data]# su - postgres
Last login: Tue Dec 11 16:34:47 CST 2018 on pts/3
-bash-4.2$ psql
psql (9.5.2)
Type "help" for help.

postgres=# create role repuser login replication encrypted password ‘pgreplication‘;
CREATE ROLE

-bash-4.2$ /usr/bin/pg_ctl stop
waiting for server to shut down.... done
server stopped
-bash-4.2$ /usr/bin/pg_ctl start
server starting
-bash-4.2$ netstat -lnt|grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
tcp6 0 0 :::5432 :::* LISTEN
查看日誌
tail -f -n 100 /var/lib/pgsql/data/pg_log/postgresql-Thu.log

從庫配置
1 基礎備份
從庫安裝完成後,不初始化,若已經初始化,刪除其data目錄
[root@mysql3 pgsql]# rm -rf data/

[root@mysql3 pgsql]# /usr/bin/pg_basebackup -D /var/lib/pgsql/data -F p -X stream -R -v -P -h 192.168.19.145 -p 5432 -U repuser
口令:
事務日誌起始於時間點: 0/7000028, 基於時間表1
pg_basebackup: 啟動後臺 WAL 接收進程
73823/73876 kB (100%), 2/2 表空間
pg_basebackup: 無法得到來自服務器的事務日誌終止位置: ERROR: could not open file "./postgresql.conf.bk20181213": Permission denied
刪除文件./postgresql.conf.bk20181213,這些配置文件的備份需要放在非data目錄

-R 備份後對文件recovery.conf進行寫操作
刪除數據目錄,重新pg_basebackup
[root@mysql3 pgdata]# /usr/bin/pg_basebackup -D /var/lib/pgsql/data -F p -X stream -R -v -P -h 192.168.19.145 -p 5432 -U repuser
口令:
事務日誌起始於時間點: 0/9000028, 基於時間表1
pg_basebackup: 啟動後臺 WAL 接收進程
73859/73859 kB (100%), 2/2 表空間
transaction log end point: 0/90000F8
pg_basebackup: 等待後臺進程結束流操作...
pg_basebackup: base backup completed
2 配置備庫參數postgresql.conf
[root@mysql3 data]# vim postgresql.conf
#在基礎備份時,初始化文件是從主庫復制來的,所以配置文件一致,註釋掉
wal_level,
max_wal_senders
wal_keep_segments等參數
打開如下參數:

hot_standby = on #在備份的同時允許查詢
max_standby_streaming_delay = 30s #可選,流復制最大延遲
wal_receiver_status_interval = 10s #可選,從向主報告狀態的最大間隔時間
hot_standby_feedback = on #可選,查詢沖突時向主反饋
max_connections = 1000 #默認參數,非主從配置相關參數,表示到數據庫的連接數,一般從庫做主要的讀服務時,設置值需要高於主庫
3 創建恢復文件recovery.conf
yum安裝的pg,所以在pg_basebackup的時候加上-R參數,生成recovery.conf
[root@mysql3 data]# cat recovery.conf
standby_mode = ‘on‘ #指明從庫身份
primary_conninfo = ‘user=repuser password=pgreplication host=192.168.19.145 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres‘ #連接到主庫信息
[root@mysql3 data]# vim recovery.conf
recovery_target_timeline = ‘latest‘ #同步到最新數據

#trigger_file = ‘/postgres/data/trigger_activestandby‘
指定觸發文件,文件存在時,將觸發從庫提升為主庫,前提是必須設置”standby_mode = on”;如果不設置此參數,也可采用”pg_ctl promote“觸發從庫切換成主庫

chown -R postgres:postgres /var/lib/pgsql/data
chown -R postgres:postgres /usr/local/pgdata
[root@mysql3 data]# /usr/bin/pg_ctl start
pg_ctl: 無法以 root 用戶運行
請以服務器進程所屬用戶 (非特權用戶) 登錄 (或使用 "su")

[root@mysql3 data]# su - postgres
上一次登錄:四 12月 13 18:01:23 CST 2018pts/1 上
-bash-4.2$ /usr/bin/pg_ctl start
正在啟動服務器進程
-bash-4.2$ LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".

4 驗證主從
1 主庫進程 sender
-bash-4.2$ ps -ef|grep postgres
postgres 4087 26671 0 09:44 ? 00:00:00 postgres: wal sender process repuser 192.168.19.227(55336) streaming 0/D000140
postgres 4093 24928 31 09:45 pts/3 00:00:00 ps -ef
postgres 4094 24928 7 09:45 pts/3 00:00:00 grep --color=auto postgres
root 24927 30012 0 Dec13 pts/3 00:00:00 su - postgres
postgres 24928 24927 0 Dec13 pts/3 00:00:00 -bash
postgres 26671 1 0 Dec13 pts/3 00:00:04 /usr/bin/postgres
postgres 26672 26671 0 Dec13 ? 00:00:00 postgres: logger process
postgres 26674 26671 0 Dec13 ? 00:00:02 postgres: checkpointer process
postgres 26675 26671 0 Dec13 ? 00:00:00 postgres: writer process
postgres 26676 26671 0 Dec13 ? 00:00:01 postgres: wal writer process
postgres 26677 26671 0 Dec13 ? 00:00:04 postgres: autovacuum launcher process
postgres 26678 26671 0 Dec13 ? 00:00:00 postgres: archiver process last was 00000001000000000000000C
postgres 26679 26671 0 Dec13 ? 00:00:05 postgres: stats collector process

2 驗證從庫 recovering
-bash-4.2$ ps -ef|grep postgres
root 32525 9758 0 09:44 pts/1 00:00:00 su - postgres
postgres 32526 32525 0 09:44 pts/1 00:00:00 -bash
postgres 32563 1 0 09:44 pts/1 00:00:00 /usr/bin/postgres
postgres 32564 32563 0 09:44 ? 00:00:00 postgres: logger process
postgres 32565 32563 0 09:44 ? 00:00:00 postgres: startup process recovering 00000001000000000000000D
postgres 32566 32563 0 09:44 ? 00:00:00 postgres: checkpointer process
postgres 32567 32563 0 09:44 ? 00:00:00 postgres: writer process
postgres 32568 32563 0 09:44 ? 00:00:00 postgres: stats collector process
postgres 32569 32563 0 09:44 ? 00:00:00 postgres: wal receiver process streaming 0/D000140
postgres 32586 32526 0 09:46 pts/1 00:00:00 ps -ef
postgres 32587 32526 0 09:46 pts/1 00:00:00 grep --color=auto postgres

3 sql查詢主從狀態
-bash-4.2$ psql
psql (9.5.2)
Type "help" for help.

postgres=# select * from pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_location | write_location | flush_location | replay
_location | sync_priority | sync_state
------+----------+---------+------------------+----------------+-----------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+----------------+-------
----------+---------------+------------
4087 | 25404 | repuser | walreceiver | 192.168.19.227 | | 55336 | 2018-12-14 09:44:45.165618+08 | 2103 | streaming | 0/D000140 | 0/D000140 | 0/D000140 | 0/D000
140 | 0 | async
(1 row)
select pg_is_in_recovery();

4 主從數據測試
主庫 192.168.19.145
postgres-# \c yhq1
yhq1=# CREATE TABLE yhq22(id integer not null,date TIMESTAMP NOT NULL DEFAULT LOCALTIMESTAMP(0));
CREATE TABLE
yhq1=# INSERT INTO yhq22 (id) VALUES (1);
INSERT 0 1
yhq1=# INSERT INTO yhq22 (id) VALUES (1);
INSERT 0 1
yhq1=# INSERT INTO yhq22 (id) VALUES (1);
INSERT 0 1
yhq1=# commit;
WARNING: there is no transaction in progress
COMMIT
yhq1=# select * from yhq22;
id | date
----+---------------------
1 | 2018-12-14 09:52:51
1 | 2018-12-14 09:52:53
1 | 2018-12-14 09:52:53
(3 rows)
從庫
postgres=# \c yhq1
您現在已經連接到數據庫 "yhq1",用戶 "postgres".
yhq1=# \d
關聯列表
架構模式 | 名稱 | 類型 | 擁有者
----------+-------+--------+----------
public | yhq | 數據表 | postgres
public | yhq22 | 數據表 | postgres
(2 行記錄)

yhq1=# select * from yhq22;
id | date
----+---------------------
1 | 2018-12-14 09:52:51
1 | 2018-12-14 09:52:53
1 | 2018-12-14 09:52:53
(3 行記錄)
yhq1=# INSERT INTO yhq22 (id) VALUES (2);
ERROR: cannot execute INSERT in a read-only transaction

5命令查看主從狀態
192.168.19.145
-bash-4.2$ pg_controldata /var/lib/pgsql/data/
pg_control version number: 942
Catalog version number: 201510051
Database system identifier: 6606088538881141528
Database cluster state: in production
pg_control last modified: Fri 14 Dec 2018 10:33:14 AM CST
Latest checkpoint location: 0/D017540
Prior checkpoint location: 0/D017460
Latest checkpoint‘s REDO location: 0/D017508
Latest checkpoint‘s REDO WAL file: 00000001000000000000000D
Latest checkpoint‘s TimeLineID: 1
Latest checkpoint‘s PrevTimeLineID: 1
Latest checkpoint‘s full_page_writes: on
Latest checkpoint‘s NextXID: 0/2107
Latest checkpoint‘s NextOID: 33597
Latest checkpoint‘s NextMultiXactId: 1
Latest checkpoint‘s NextMultiOffset: 0
Latest checkpoint‘s oldestXID: 1823
Latest checkpoint‘s oldestXID‘s DB: 1
Latest checkpoint‘s oldestActiveXID: 2107
Latest checkpoint‘s oldestMultiXid: 1
Latest checkpoint‘s oldestMulti‘s DB: 24598
Latest checkpoint‘s oldestCommitTsXid:0
Latest checkpoint‘s newestCommitTsXid:0
Time of latest checkpoint: Fri 14 Dec 2018 10:33:14 AM CST
192.168.19.227
-bash-4.2$ pg_controldata /var/lib/pgsql/data/
pg_control 版本: 942
Catalog 版本: 201510051
數據庫系統標識符: 6606088538881141528
數據庫簇狀態: 正在歸檔恢復
pg_control 最後修改: 2018年12月14日 星期五 10時34分46秒
最新檢查點位置: 0/D017540
優先檢查點位置: 0/D017460
最新檢查點的 REDO 位置: 0/D017508
最新檢查點的重做日誌文件: 00000001000000000000000D
最新檢查點的 TimeLineID: 1
最新檢查點的PrevTimeLineID: 1
最新檢查點的full_page_writes: 開啟
最新檢查點的 NextXID: 0/2107
最新檢查點的 NextOID: 33597
最新檢查點的NextMultiXactId: 1
最新檢查點的NextMultiOffsetD: 0
最新檢查點的oldestXID: 1823
最新檢查點的oldestXID所在的數據庫:1
最新檢查點檢查oldestActiveXID: 2107
最新檢查點檢查oldestMultiXid: 1
最新檢查點的oldestMulti所在的數據庫:24598
Latest checkpoint‘s oldestCommitTsXid:0
Latest checkpoint‘s newestCommitTsXid:0
最新檢查點的時間: 2018年12月14日 星期五 10時33分14秒
不帶日誌的關系: 0/1使用虛假的LSN計數器

6 主從切換
192.168.19.145
-bash-4.2$ pg_ctl stop
waiting for server to shut down...... done
server stopped
-bash-4.2$ pg_controldata /var/lib/pgsql/data/
pg_control version number: 942
Catalog version number: 201510051
Database system identifier: 6606088538881141528
Database cluster state: shut down
pg_control last modified: Fri 14 Dec 2018 10:38:54 AM CST
Latest checkpoint location: 0/E000028
Prior checkpoint location: 0/D017620
Latest checkpoint‘s REDO location: 0/E000028
從 192.168.19.227
從庫日誌
-bash-4.2$ tail -n 100 /var/lib/pgsql/data/pg_log/postgresql-Fri.log
LOG: database system is ready to accept read only connections
LOG: started streaming WAL from primary at 0/D000000 on timeline 1
ERROR: syntax error at or near "select" at character 35
STATEMENT: INSERT INTO yhq22 (id) VALUES (2)
select * from yhq22;
LOG: replication terminated by primary server
DETAIL: End of WAL reached on timeline 1 at 0/E000098.
FATAL: could not send end-of-streaming message to primary: no COPY in progress

LOG: invalid record length at 0/E000098
FATAL: could not connect to the primary server: could not connect to server: Connection refused
Is the server running on host "192.168.19.145" and accepting
TCP/IP connections on port 5432?
.....
LOG: received promote request
LOG: redo done at 0/E000028
LOG: last completed transaction was at log time 2018-12-14 09:52:53.356998+08
LOG: selected new timeline ID: 2
LOG: archive recovery complete
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started

-bash-4.2$ pg_ctl promote
服務器重新加載中
-bash-4.2$ pg_controldata /var/lib/pgsql/data/
pg_control 版本: 942
Catalog 版本: 201510051
數據庫系統標識符: 6606088538881141528
數據庫簇狀態: 在運行中
pg_control 最後修改: 2018年12月14日 星期五 10時40分21秒
最新檢查點位置: 0/E000100
優先檢查點位置: 0/E000028
最新檢查點的 REDO 位置: 0/E0000C8
最新檢查點的重做日誌文件: 00000002000000000000000E
從庫正常讀寫
yhq1=# INSERT INTO yhq22 (id) VALUES (2);
INSERT 0 1
yhq1=# select * from yhq22;
id | date
----+---------------------
1 | 2018-12-14 09:52:51
1 | 2018-12-14 09:52:53
1 | 2018-12-14 09:52:53
2 | 2018-12-14 10:41:04
從庫的文件 變成recovery.conf
-rw-r--r-- 1 postgres postgres 195 12月 14 09:41 recovery.done

原主庫操作(恢復原主庫為從庫)
1 重新搭建新主從 ?
2 增量搭建
這時候原從庫已經寫入了很多其他數據
yhq1=# select count(*) from yhq22;
count
-------
8
測試1
從 192.168.19.145
新建文件recovery.conf 並修改文件postgresql.conf
-bash-4.2$ vim recovery.conf
standby_mode = ‘on‘
primary_conninfo = ‘user=repuser password=pgreplication host=192.168.19.227 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres‘
recovery_target_timeline = ‘latest‘
-bash-4.2$ vim postgresql.conf
#max_wal_senders
#wal_keep_segments
-bash-4.2$ /usr/bin/pg_ctl start
server starting
-bash-4.2$ LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".

從 192.168.19.145
-bash-4.2$ pg_controldata /var/lib/pgsql/data/
pg_control version number: 942
Catalog version number: 201510051
Database system identifier: 6606088538881141528
Database cluster state: in archive recovery
pg_control last modified: Fri 14 Dec 2018 11:31:48 AM CST
Latest checkpoint location: 0/E000028
Prior checkpoint location: 0/E000028
Latest checkpoint‘s REDO location: 0/E000028
Latest checkpoint‘s REDO WAL file: 00000001000000000000000E
Latest checkpoint‘s TimeLineID: 1
Latest checkpoint‘s PrevTimeLineID: 1
Latest checkpoint‘s full_page_writes: on
Latest checkpoint‘s NextXID: 0/2107
主 192.168.19.227
-bash-4.2$ pg_controldata /var/lib/pgsql/data/
pg_control 版本: 942
Catalog 版本: 201510051
數據庫系統標識符: 6606088538881141528
數據庫簇狀態: 在運行中
pg_control 最後修改: 2018年12月14日 星期五 11時30分22秒
最新檢查點位置: 0/E000E00
優先檢查點位置: 0/E000D20
最新檢查點的 REDO 位置: 0/E000DC8
最新檢查點的重做日誌文件: 00000002000000000000000E
最新檢查點的 TimeLineID: 2
最新檢查點的PrevTimeLineID: 2
最新檢查點的full_page_writes: 開啟
最新檢查點的 NextXID: 0/2112
yhq1=# select * from pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_location | write_location | flush_location | replay_location | sync_priorit
y | sync_state
-----+----------+---------+------------------+-------------+-----------------+-------------+---------------+--------------+-------+---------------+----------------+----------------+-----------------+-------------
--+------------
(0 行記錄)

新從--是沒有新增加的數據的
postgres=# \c yhq1
You are now connected to database "yhq1" as user "postgres".
yhq1=# select *from yhq22;
id | date
----+---------------------
1 | 2018-12-14 09:52:51
1 | 2018-12-14 09:52:53
1 | 2018-12-14 09:52:53
yhq1=# select * from pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_location | write_location | flush_location | replay_location | sync_priorit
y | sync_state
-----+----------+---------+------------------+-------------+-----------------+-------------+---------------+--------------+-------+---------------+----------------+----------------+-----------------+-------------
--+------------
-bash-4.2$ tail -n 100 /var/lib/pgsql/data/pg_log/postgresql-Fri.log
FATAL: could not connect to the primary server: FATAL: no pg_hba.conf entry for replication connection from host "192.168.19.145", user "repuser", SSL off

FATAL: could not connect to the primary server: FATAL: no pg_hba.conf entry for replication connection from host "192.168.19.145", user "repuser", SSL off

主 192.168.19.227
-bash-4.2$ vim pg_hba.conf
host replication repuser 192.168.19.145/32 md5
-bash-4.2$ /usr/bin/pg_ctl reload

yhq1=# select * from pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_location | write_location | flush_location | replay
_location | sync_priority | sync_state
------+----------+---------+------------------+----------------+-----------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+----------------+-------
----------+---------------+------------
2061 | 25404 | repuser | walreceiver | 192.168.19.145 | | 52776 | 2018-12-14 11:40:34.130154+08 | 2112 | streaming | 0/E001068 | 0/E001068 | 0/E001068 | 0/E001
068 | 0 | async
(1 行記錄)
新從 192.168.19.145
數據同步
yhq1=# select *from yhq22;
id | date
----+---------------------
1 | 2018-12-14 09:52:51
1 | 2018-12-14 09:52:53
1 | 2018-12-14 09:52:53
2 | 2018-12-14 10:41:04
2 | 2018-12-14 11:24:06
2 | 2018-12-14 11:24:07
2 | 2018-12-14 11:24:07
2 | 2018-12-14 11:24:08
(8 rows)
-bash-4.2$ tail -n 100 /var/lib/pgsql/data/pg_log/postgresql-Fri.log
LOG: fetching timeline history file for timeline 2 from primary server
LOG: started streaming WAL from primary at 0/E000000 on timeline 1
LOG: replication terminated by primary server
DETAIL: End of WAL reached on timeline 1 at 0/E000098.
LOG: new target timeline is 2
LOG: restarted WAL streaming at 0/E000000 on timeline 2
LOG: redo starts at 0/E000098
-bash-4.2$ ps -ef|grep postgres
root 4627 13226 0 10:37 pts/2 00:00:00 su - postgres
postgres 4628 4627 0 10:37 pts/2 00:00:00 -bash
postgres 4806 1 0 11:31 pts/3 00:00:01 /usr/bin/postgres
postgres 4807 4806 0 11:31 ? 00:00:00 postgres: logger process
postgres 4808 4806 0 11:31 ? 00:00:00 postgres: startup process recovering 00000002000000000000000E
postgres 4809 4806 0 11:31 ? 00:00:00 postgres: checkpointer process
postgres 4810 4806 0 11:31 ? 00:00:00 postgres: writer process
postgres 4811 4806 0 11:31 ? 00:00:00 postgres: stats collector process
postgres 4861 24928 0 11:35 pts/3 00:00:00 psql
postgres 4864 4806 0 11:35 ? 00:00:00 postgres: postgres yhq1 [local] idle
postgres 4940 4806 0 11:40 ? 00:00:04 postgres: wal receiver process streaming 0/E002E40
postgres 5117 4628 0 14:31 pts/2 00:00:00 ps -ef
postgres 5118 4628 0 14:31 pts/2 00:00:00 grep --color=auto postgres
root 24927 30012 0 Dec13 pts/3 00:00:00 su - postgres
postgres 24928 24927 0 Dec13 pts/3 00:00:00 -bash
yhq1=# show synchronous_commit ;
synchronous_commit
--------------------
on
(1 row)


yhq1=# select txid_current_snapshot(); #返回主庫記錄點、備庫記錄點;主庫每增加一條寫入,記錄點的值就會加1
txid_current_snapshot
-----------------------
2117:2117:
(1 行記錄)

yhq1=# select txid_current_snapshot();
txid_current_snapshot
-----------------------
2117:2117:
(1 row)
查看備庫落後主庫多少個字節的wal日誌
yhq1=# select pg_xlog_location_diff(pg_current_xlog_location(),replay_location) from pg_stat_replication;
pg_xlog_location_diff
-----------------------
(0 rows)

postgresql 主從復制並切換