環境:
OS:CentOs 7
Postgres-11.6
1.安裝步驟
1.1 環境部署
數據庫部署
節點 ip 角色
Host01 192.168.1.130 主
Host02 192.168.1.131 從
1.2 配置等效連接
因為我們需要將主庫的歸檔日志通過scp免密傳輸到備庫
等效連接配置可以參考
https://www.cnblogs.com/hxlasky/p/12204180.html
1.3 主庫安裝
1.3.1 安裝介質准備
下載地址: https://www.postgresql.org/ftp/source/
我這里下載的是11.6版本,介質如下:
postgresql-11.6.tar.gz
這里下載的源碼,所以下面的步驟是源碼安裝
1.3.2 安裝依賴包
yum install readline
yum install gcc
yum -y install -y readline-devel
yum install zlib-devel
1.3.3 編譯安裝
[root@localhost soft]# tar -xvf postgresql-11.6.tar.gz
[root@localhost soft]#mkdir -p /opt/postgresql-11.6
[root@localhost soft]# cd postgresql-11.6
[root@localhost soft]#./configure --prefix=/opt/postgresql-11.6
[root@localhost soft]#make
[root@localhost soft]#make install
1.3.4 創建相應的用戶
[root@localhost opt]# groupadd postgres
[root@localhost opt]# useradd -g postgres postgres
1.3.5 創建數據及日志目錄,並做相應授權
[root@localhost soft]#mkdir -p /opt/postgresql-11.6/{data,log}
[root@localhost soft]#chown -R postgres:postgres /opt/postgresql-11.6
1.3.6 初始化數據庫
#su - postgres
[postgres@localhost bin]$ cd /opt/postgresql-11.6/bin
[postgres@localhost bin]$ ./initdb -D /opt/postgresql-11.6/data/
1.3.7 啟動數據庫
[postgres@localhost bin]$ cd /opt/postgresql-11.6/bin
[postgres@localhost bin]$./pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start
1.3.8 修改環境變量
[postgres@localhost ~]$ more .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/opt/postgresql-11.6/bin
export PATH
1.3.9 登陸使用
[postgres@localhost bin]$cd /opt/postgresql-11.6/bin
[postgres@localhost bin]$ ./psql
psql (11.6)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
1.3.10 修改postgres用戶的訪問密碼並測試建庫建表
PostgreSQL 數據庫默認會創建一個postgres的數據庫用戶作為數據庫的管理員,默認密碼為空,我們需要修改為指定的密碼,這里設定為postgres.
su - postgres
psql
# ALTER USER postgres WITH PASSWORD 'postgres';
# select * from pg_shadow ;
# create database hxl;
# \c hxl
project=# create table person(id integer, name text);
project=# insert into person values (1, 'hxl');
project=# select * from person;
1.3.11 配置postgresql允許遠程訪問
只需要修改data目錄下的pg_hba.conf和postgresql.conf這兩個文件:
pg_hba.conf:配置對數據庫的訪問權限;
postgresql.conf:配置PostgreSQL數據庫服務器的相應的參數
vim /opt/postgresql-11.6/data/pg_hba.conf
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
重新加載配置文件
su - postgres
pg_ctl -D /opt/postgresql-11.6/data reload
修改postgresql.conf
vim /opt/postgresql-11.6/data/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
修改該改參數需要重啟動
pg_ctl -D /opt/postgresql-11.6/data -l /opt/postgresql-11.6/log/postgres.log stop
pg_ctl -D /opt/postgresql-11.6/data -l /opt/postgresql-11.6/log/postgres.log start
到這里主庫已經按照好了,下面進行主庫的配置
1.3.12 主從配置
1.3.12.1 創建同步賬號
[postgres@localhost data]$ psql
psql (11.6)
Type "help" for help.
postgres=# CREATE ROLE repl login replication encrypted password 'repl';
CREATE ROLE
1.3.12.2 修改配置文件(pg_hba.conf)
在該文件最后添加如下兩行:
host replication repl 192.168.1.0/24 md5
host all repl 192.168.1.0/24 trust
1.3.12.3 修改配置文件(postgresql.conf)
找到相應的參數進行如下配置修改
wal_level = replica ##這個是設置主為wal的主機
archive_mode = on
archive_command = 'ssh 192.168.1.131 test ! -f /opt/postgresql-11.6/data/pg_archive/%f && scp %p 192.168.1.131:/opt/postgresql-11.6/data/pg_archive/%f'
max_wal_senders = 6 ##這個設置了可以最多有幾個流復制連接,差不多有幾個從,就設置幾個
wal_keep_segments = 10240 ##設置流復制保留的最多的xlog數目
wal_sender_timeout = 60s ##設置流復制主機發送數據的超時時間
1.3.12.4 創建歸檔日志目錄
mkdir -p /opt/postgresql-11.6/data/pg_archive
1.3.12.5 重啟主庫
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log restart
1.3.12.6 檢查歸檔是否傳輸到備庫
在主庫上執行如下命令
psql -c "select pg_switch_wal()"
或是執行
postgres=# select pg_switch_wal();
可以看到已經生成了2個新的歸檔日志
歸檔日志相應的傳輸到備庫
1.4 從庫安裝
1.4.1 安裝
從庫的安裝跟主庫安裝步驟一致,需要啟動數據庫
1.4.2 停掉從庫
若從庫的數據庫已經在運行的話,事先將其停掉
[postgres@localhost data]$ pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log stop
waiting for server to shut down.... done
server stopped
1.4.3 准備data目錄
從庫安裝完成后,不初始化,若已經初始化,刪除其data目錄
若之前安裝的pg有data目錄的話需要將其刪除掉,並創建一個空的相同的目錄
su - postgres
[postgres@localhost postgresql-11.6]$ cd /opt/postgresql-11.6
[postgres@localhost postgresql-11.6]$ mv data bakdata
[postgres@localhost postgresql-11.6]$ mkdir data
root用戶下修改權限
chown -R postgres:postgres /opt/postgresql-11.6
chmod 0700 /opt/postgresql-11.6/data
1.4.4 基礎同步主庫的數據文件
[postgres@localhost postgresql-11.6]$ pg_basebackup -RFp --progress -D /opt/postgresql-11.6/data -h 192.168.1.130 -p 5432 -U repl --password
Password:
113625/113625 kB (100%), 1/1 tablespace
可以看到data目錄下的所有文件都同步過來了,使用了R參數會生成一個recovery.conf文件,下面我們直接修改該文件即可
[postgres@localhost data]$ pwd
/opt/postgresql-11.6/data
[postgres@localhost data]$ ls -al
[postgres@localhost data]$ ls -1
backup_label
base
global
pg_archive
pg_commit_ts
pg_dynshmem
pg_hba.conf
pg_ident.conf
pg_logical
pg_multixact
pg_notify
pg_replslot
pg_serial
pg_snapshots
pg_stat
pg_stat_tmp
pg_subtrans
pg_tblspc
pg_twophase
PG_VERSION
pg_wal
pg_xact
postgresql.auto.conf
postgresql.conf
recovery.conf
1.4.4 修改recovery.conf文件
前面的步驟已經生成了recovery.conf文件,
該文件的內容是這樣的
[postgres@localhost data]$ more recovery.conf
standby_mode = 'on'
primary_conninfo = 'user=repl password=repl host=192.168.1.130 port=5432 sslmode=disable sslcompression=0 target_session_attrs=any'
我們這里不做流復制, primary_conninfo先注釋掉,修改該文件,內容如下:
restore_command = 'cp /opt/postgresql-11.6/data/pg_archive/%f %p'
standby_mode = on
1.4.5 修改從庫postgresql.conf文件
修改如下內容項:
max_connections = 1000 #一般查多於寫的應用從庫的最大連接數要比較大
hot_standby = on #說明這台機器不僅僅是用於數據歸檔,也用於數據查詢
max_standby_streaming_delay = 30s #數據流備份的最大延遲時間
wal_receiver_status_interval = 1s #多久向主報告一次從的狀態,當然從每次數據復制都會向主報告狀態,這里只是設置最長的間隔時間
hot_standby_feedback = on #如果有錯誤的數據復制,是否向主進行反饋
1.4.5 啟動從庫
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start
1.4.6 驗證
1.4.6.1 查看同步情況
[postgres@localhost pg_wal]$ psql
postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication;
(0 rows)
這里不是流復制,所有沒有顯示
1.4.6.2 主庫嘗試創建對象看是否同步到從庫
psql -h localhost -U uhxl -d hxl
create table tb_test
(
id bigserial primary key not null,
name varchar(64)
);
insert into tb_test(name) values('name1');
insert into tb_test(name) values('name2');
insert into tb_test(name) values('name3');
insert into tb_test(name) values('name4');
insert into tb_test(name) values('name5');
查看從庫是否同步
這種歸檔模式的同步需要主庫上執行檢查點切換后,主庫的數據才會同步到從庫
或是手工進行檢查點的切換
select pg_switch_wal();
為啥要切換生成日志,因為架設的是基於文件的備庫,只有歸檔傳輸到備庫后才會應用,否則記錄還在主庫的xlog中.