環境:
OS:CentOs 7
Postgres-11.6 源碼編譯
1.2 主庫安裝
1.2.1 安裝介質准備
下載地址: https://www.postgresql.org/ftp/source/
我這里下載的是11.6版本,介質如下:
postgresql-11.6.tar.gz
這里下載的源碼,所以下面的步驟是源碼安裝
1.2.2 安裝依賴包
yum install readline
yum install gcc
yum -y install -y readline-devel
yum install zlib-devel
1.2.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.2.4 創建相應的用戶
[root@localhost opt]# groupadd postgres
[root@localhost opt]# useradd -g postgres postgres
1.2.5 創建數據及日志目錄,並做相應授權
[root@localhost soft]#mkdir -p /opt/postgresql-11.6/{data,log}
[root@localhost soft]#chown -R postgres:postgres /opt/postgresql-11.6
1.2.6 初始化數據庫
#su - postgres
[postgres@localhost bin]$ cd /opt/postgresql-11.6/bin
[postgres@localhost bin]$ ./initdb -D /opt/postgresql-11.6/data/
1.2.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.2.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.2.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.2.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.2.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.2.12 主從配置
1.2.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.2.12.2 修改配置文件(pg_hba.conf)
在該文件最后添加如下兩行:
host replication repl 192.168.1.0/24 md5
host all repl 192.168.1.0/24 trust
1.2.12.3 修改配置文件(postgresql.conf)
找到相應的參數進行如下配置修改
wal_level = replica
archive_mode = on
archive_command = 'cp %p /opt/postgresql-11.6/data/pg_archive/%f'
##%p = path of file to archive
##%f = file name only
max_wal_senders = 6
wal_keep_segments = 10240
wal_sender_timeout = 60s
1.2.12.4 創建歸檔日志目錄
mkdir -p /opt/postgresql-11.6/data/pg_archive
1.2.12.5 重啟主庫
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log restart
1.3 從庫安裝
1.3.1 安裝
從庫的安裝跟主庫安裝步驟一致,需要啟動數據庫
1.3.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.3.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.3.4 同步主庫的數據文件
[postgres@localhost postgresql-11.6]$ pg_basebackup -Fp --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目錄下的所有文件都同步過來了
[postgres@localhost data]$ pwd
/opt/postgresql-11.6/data
[postgres@localhost data]$ ls -al
total 56
drwxrwxr-x 20 postgres postgres 4096 Jan 14 11:11 .
drwxr-xr-x 9 postgres postgres 94 Jan 14 11:08 ..
-rw------- 1 postgres postgres 224 Jan 14 11:11 backup_label
drwx------ 6 postgres postgres 54 Jan 14 11:11 base
drwx------ 2 postgres postgres 4096 Jan 14 11:11 global
drwxrwxr-x 2 postgres postgres 166 Jan 14 11:11 pg_archive
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_commit_ts
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_dynshmem
-rw------- 1 postgres postgres 4719 Jan 14 11:11 pg_hba.conf
-rw------- 1 postgres postgres 1636 Jan 14 11:11 pg_ident.conf
drwx------ 4 postgres postgres 68 Jan 14 11:11 pg_logical
drwx------ 4 postgres postgres 36 Jan 14 11:11 pg_multixact
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_notify
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_replslot
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_serial
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_snapshots
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_stat
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_stat_tmp
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_subtrans
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_tblspc
drwx------ 2 postgres postgres 6 Jan 14 11:11 pg_twophase
-rw------- 1 postgres postgres 3 Jan 14 11:11 PG_VERSION
drwx------ 3 postgres postgres 60 Jan 14 11:11 pg_wal
drwx------ 2 postgres postgres 18 Jan 14 11:11 pg_xact
-rw------- 1 postgres postgres 88 Jan 14 11:11 postgresql.auto.conf
-rw------- 1 postgres postgres 24073 Jan 14 11:11 postgresql.conf
1.3.4 創建recovery.conf文件
從模板文件拷貝到data目錄
[postgres@localhost share]$ cp /opt/postgresql-11.6/share/recovery.conf.sample /opt/postgresql-11.6/data/recovery.conf
對其進行修改,參數如下:
在data目錄下創建recovery.conf文件,內容如下
standby_mode = on # 這個說明這台機器為從庫
primary_conninfo = 'host=192.168.1.130 port=5432 user=repl password=repl' # 這個說明這台機器對應主庫的信息
recovery_target_timeline = 'latest' # 這個說明這個流復制同步到最新的數據
1.3.5 修改從庫postgresql.conf文件
修改如下內容項:
max_connections = 1000 #一般查多於寫的應用從庫的最大連接數要比較大
hot_standby = on #說明這台機器不僅僅是用於數據歸檔,也用於數據查詢
max_standby_streaming_delay = 30s #數據流備份的最大延遲時間
wal_receiver_status_interval = 1s #多久向主報告一次從的狀態,當然從每次數據復制都會向主報告狀態,這里只是設置最長的間隔時間
hot_standby_feedback = on #如果有錯誤的數據復制,是否向主進行反饋
1.3.6 啟動從庫
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start
1.3.7 驗證
1.3.7.1 查看同步情況
[postgres@localhost pg_wal]$ psql
postgres=# \x on
Expanded display is on.
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 28450
usesysid | 16411
usename | repl
application_name | walreceiver
client_addr | 192.168.1.131
client_hostname |
client_port | 42388
backend_start | 2020-01-14 11:30:41.314367+08
backend_xmin | 600
state | streaming
sent_lsn | 0/8000140
write_lsn | 0/8000140
flush_lsn | 0/8000140
replay_lsn | 0/8000140
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
1.3.7.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');
查看從庫是否同步
1.4 主從切換(promote方式主從切換)
1.4.1 主備庫進程查看
主庫
[postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
Database cluster state: in production
[postgres@localhost data]$
備庫
[postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
Database cluster state: in archive recovery
[postgres@localhost data]$
1.4.2 停掉主庫
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log stop
查看狀態已經處於stop狀態
[postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
Database cluster state: shut down
1.4.3 提升從庫為主庫
這個時候從庫保持運行狀態,不需要停掉
[postgres@localhost data]$ pg_ctl promote -D /opt/postgresql-11.6/data/
waiting for server to promote.... done
server promoted
查看狀態
[postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
Database cluster state: in production
1.4.4 驗證
這個時候從庫的recovery.conf文件會自動命名為recovery.done
嘗試在原來的從庫寫入數據
insert into tb_hxl01 values(20,'name6');
insert into tb_hxl01 values(21,'name7');
insert into tb_hxl01 values(22,'name8');
insert into tb_hxl01 values(23,'name9');
insert into tb_hxl01 values(24,'name10');
insert into tb_hxl01 values(25,'name10');
寫入新增數據,重庫啟動后,模擬差異數據是否同步到從庫
1.4.5 將原來的主庫部署成為重庫
1.4.5.1 創建recovery.conf文件
在原來主庫的data目錄下創建recovery.conf文件
1.4.5.2 啟動
[postgres@localhost data]$ pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start
查看狀態
[postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
Database cluster state: in archive recovery
這個時候發現數據庫無法連接
[postgres@localhost log]$ psql
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
查看錯誤日志postgres.log
2020-01-19 15:07:55.297 CST [31189] FATAL: hot standby is not possible because max_connections = 100 is a lower setting than on the master server (its value was 1000)
解決辦法,修改當前數據庫的max_connections與主庫保持一致
1.4.5.3 驗證剛才從庫寫入的數據是否同步過來
查看新備庫中數據是否與現主庫內容相同,9.6以后的版本應該會自動同步差異
