postgres主從搭建步驟




-----------------------------------------------------  刪除備庫用戶
psql.exe -U postgres -h 127.0.0.1 -p 5432 -c "drop user if exists repl;"


---------------------------------------------------   新增備庫用戶
psql.exe -U postgres -h 127.0.0.1 -p 5432 -c "create user repl with login replication password 'repl';" postgres


----------------------------------------------------- 修改pg_hba.conf文件:

# replication privilege.
host    replication     repl            10.33.47.9/32		    md5


-----------------------------------------------------  修改 postgres.conf文件:

---- 取消註釋wal_level = replica, 改爲: wal_level = hot_standby

wal_level = hot_standby

----- 取消註釋max_wal_senders = 10

max_wal_senders = 10

----- 取消註釋wal_keep_segments = 0, 改爲:wal_keep_segments = 256

wal_keep_segments = 256

-----------------------------------------------重啓主庫


net stop postgresql-x64-11

net start postgresql-x64-11

---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------

------ 停止從庫
net stop postgresql-x64-11


------ 刪除從庫data目錄下文件
cd /d D:\PostgreSQL\11
for /d %i in (data\*.*) do (rd /s /q %i)
del /f /s /q data\*.*

------------------------------------------------------------


----- 使用 pg_basebackup 生成備庫

cd /d D:\PostgreSQL\11\bin

--- 下述命令執行需要輸入從庫密碼: repl
pg_basebackup.exe -D "D:\PostgreSQL\11\data" -Fp -Xs --progress -v -U repl -h 10.12.66.50 -p 5432


----修改配置文件postgresql.conf,配置備庫的流複製參數,如下

---取消註釋
hot_standby = on

-- 取消註釋:wal_receiver_status_interval = 10s, 改爲 wal_receiver_status_interval = 2s
wal_receiver_status_interval = 2s

---- 取消註釋:hot_standby_feedback = off,  改爲hot_standby_feedback = on
hot_standby_feedback = on

---------------------------------------

修改配置文件 recover.conf
cd /d D:\PostgreSQL\11
copy /y  share\recovery.conf.sample   data\recovery.conf

配置如下參數
--- 取消註釋
recovery_target_timeline = 'latest'

---修改standby_mode = off爲standby_mode = on
standby_mode = on

primary_conninfo = 'host=10.12.66.50 port=5432 user=repl password=repl'

-- 修改trigger_file = '', 爲:trigger_file = 'tgfile'
trigger_file = 'tgfile'

重啓:

net start  postgresql-x64-11




------------------------------------驗證

--- 主庫中查詢
select * from pg_stat_replication;

-- 查詢被分庫
psql.exe -U postgres -h 10.33.47.9 -c "select datname from pg_database" postgres

-- 主庫中建立新庫
psql.exe -U postgres -h 10.12.66.50 -c "create database cmsdb" postgres

---再次查詢備份庫
psql.exe -U postgres -h 10.33.47.9 -c "select datname from pg_database" postgres


--備份機查詢用戶
psql.exe -U postgres -h 10.33.47.9 -c "select * from pg_user" postgres

--主機創建用戶
psql.exe -U postgres -h 10.12.66.50 -c "create user test01 with password 'test01' " postgres


--備機再次查詢用戶
psql.exe -U postgres -h 10.33.47.9 -c "select * from pg_user" postgres


---------------------------------------------------------------------

psql.exe -U postgres -h 10.33.47.9 -c "select * from pg_tables where tablename='tb_test01'" postgres

psql.exe -U postgres -h 10.12.66.50 -c " create table tb_test01(id int, code varchar(64))" postgres

psql.exe -U postgres -h 10.33.47.9 -c "select * from pg_tables where tablename='tb_test01'" postgres































發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章