postgresql主從切換(promote方式主從切換)


主:192.168.1.130

從:192.168.1.131

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]$

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


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



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');
寫入新增數據,重庫啟動后,模擬差異數據是否同步到從庫



5、將原來的主庫部署成為重庫
5.1 創建recovery.conf文件
在原來主庫的data目錄下創建recovery.conf文件
standby_mode='on'
recovery_target_timeline = 'latest'
primary_conninfo = 'host=192.168.1.131 port=5432 user=repl password=repl'

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與主庫保持一致

5.3 驗證剛才從庫寫入的數據是否同步過來
查看新備庫中數據是否與現主庫內容相同,9.6以后的版本應該會自動同步差異

 

-- The End --


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM