1 總體規划
Master庫 |
Slave庫 |
|
操作系統 |
CentOS Linux release 7.5.1804 |
CentOS Linux release 7.5.1804 |
處理器 |
1 |
1 |
內存 |
4G |
4G |
硬盤 |
38G |
38G |
主機名稱 |
SHSNC-DB01 |
SHSNC-DB02 |
IP地址 |
192.168.1.61 |
192.168.1.62 |
具體安裝步驟,可以查看《PostgreSQL數據庫的安裝》,不在本文的介紹范圍內。
2 PostgreSQL主從異步流復制搭建
2.1 參數檢查
檢查主庫postgresql.conf文件是否已經配置以下參數:
listen_addresses = '*' port = 5432 log_destination = 'csvlog' logging_collector = on log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' max_connections = 1000 max_connections = 100 max_connections = hot_standby
若以上參數沒有配置,配置完后需要重啟PostgreSQL數據庫。
2.2 創建同步賬號
在主庫創建同步賬號以及相應的數據庫
$ psql -p 5432 -U postgres postgres psql (9.6.11) Type "help" for help. postgres=# CREATE DATABASE pocdb; CREATE DATABASE postgres=# \c pocdb You are now connected to database "pocdb" as user "postgres". pocdb=# pocdb=# CREATE USER repl ENCRYPTED PASSWORD '123456' REPLICATION; CREATE ROLE
檢查創建用戶的權限:
$ psql -p 5432 -U postgres postgres psql (9.6.11) Type "help" for help. postgres=# \du+ List of roles Role name | Attributes | Member of | Description -----------+------------------------------------------------------------+-----------+------------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} | repl | Replication | {} |
2.3 修改pg_hba.conf文件
在主庫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 host all all 192.168.1.0/24 trust # IPv6 local connections: host all all ::1/128 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 repl 192.168.1.61/32 md5 host replication repl 192.168.1.62/32 md5
需要注意此處用戶名repl是剛才創建同步的用戶,不是postgres。
2.4 reload配置文件
reload主庫配置文件:
$ pg_ctl -D /postgres/pgdata/ reload
server signaled
master配置成功后,slave 安裝基本環境同 master ,區別在於 slave 從庫不需要進行 initdb 初始化數據庫
2.5 備庫數據復制
在備庫使用postgres主機賬號進行操作:
$ pg_basebackup -h 192.168.1.61 -U repl -W -Fp -Pv -Xs -R -D /postgres/pgdata Password: pg_basebackup: initiating base backup, waiting for checkpoint to complete pg_basebackup: checkpoint completed transaction log start point: 0/2000028 on timeline 1 pg_basebackup: starting background WAL receiver 29956/29956 kB (100%), 1/1 tablespace transaction log end point: 0/20000F8 pg_basebackup: waiting for background process to finish streaming ... pg_basebackup: base backup completed
上述表示同步成功
2.6 檢查recovery配置文件
在備庫檢查recovery.conf配置文件
$ cat recovery.conf standby_mode = 'on' primary_conninfo = 'user=repl password=123456 host=192.168.1.61 port=5432 sslmode=disable sslcompression=1'
2.7 啟動備庫
$ pg_ctl -D /postgres/pgdata start
server starting
2.8 搭建后驗證
啟動后在主庫創建一個數據庫並在備庫查看是否已同步:
主庫:
$ psql psql (9.6.11) Type "help" for help. postgres=# create database shsnc; CREATE DATABASE postgres=# \list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- chenzxdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | pocdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | shsnc | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (6 rows) postgres=# \x Expanded display is on. postgres=# select * from pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 29210 usesysid | 16386 usename | repl application_name | walreceiver client_addr | 192.168.1.162 ------>從備庫連接上主庫 client_hostname | client_port | 59590 backend_start | 2018-11-15 17:13:54.269887+08 backend_xmin | state | streaming sent_location | 0/4032A78 write_location | 0/4032A78 flush_location | 0/4032A78 replay_location | 0/4032A78 sync_priority | 0 sync_state | async postgres=#
備庫:
$ psql psql (9.6.11) Type "help" for help. postgres=# \list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- chenzxdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | pocdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | shsnc | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (6 rows) postgres=#
另外,可查看pg_log查看關鍵字"database system is ready to accept read only connections"
$ grep "database system " ./pg_log/*csv ./pg_log/postgresql-2018-11-15_171206.csv:2018-11-15 17:12:06.999 CST,,,4324,,5bed3866.10e4,2,,2018-11-15 17:12:06 CST,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,"" ----->連接上主庫並使用read only模式打開數據庫
3 搭建過程中遇到的問題QA
- pg_basebackup: could not connect to server: FATAL: number of requested standby connections exceeds max_wal_senders (currently 0)
解決方案:在主庫postgres.conf文件中添加以下參數
max_connections = 1000 max_connections = 100 max_connections = hot_standby
- psql: FATAL: the database system is starting up
解決方案:在備庫postgres.conf文件中添加以下參數
hot_standby = on