PostgreSQL一主兩從實現及主從切換


第一步:安裝數據庫

一主兩從操作步驟:
在三台機器分別按照步驟1-4安裝pg數據包
1、 安裝
./configure –prefix=/usr/pgsql9.3.4 –with-perl –with-openssl –with-pam –without-ldap –with-libxml –with-libxslt –enable-thread-safety –with-wal-blocksize=16 –with-blocksize=16
make world
make install-world

2、添加用戶
groupadd postgres
useradd -g postgres postgres
passwd postgres

3、修改內核參數
vim /etc/sysctl.conf

#Kernel paramaters required by PostgreSql
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
fs.file-max = 65536
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_default = 1048576
net.core.rmem_max = 1048576
net.core.wmem_default = 262144
net.core.wmem_max = 262144

sysctl -p

vim /etc/security/limits.conf

postgres soft nproc 16384
postgres hard nproc 16384
postgres soft nofile 65536
postgres hard nofile 65536

4、增加.bash_profile環境變量
export PGHOME=/usr/pgsql9.3.4
export PATH=PGHOME/bin:PATH:.
export MANPATH=PGHOME/share/man:MANPATH

 

第二步:配置主數據庫

主:172.18.18.99
su - postgres
123
1》mkdir /data/pgdata/pg_primary
2》mkdir /data/pgdata/pg_primary_data
3》/usr/pgsql9.3.4/bin/initdb -D /data/pgdata/pg_primary/ -E UTF8 –locale=C -U postgres -W

4》數據庫參數
postgresql.conf//修改port等等(直接拿99開發機配置庫配置文件替換)
pg_hba.conf //修改如下

  修改pg_hba.conf:(METHOD 和 最后一項ADDRESS)
  # TYPE  DATABASE        USER            ADDRESS                 METHOD  

  # "local" is for Unix domain socket connections only
  local   all             all                                     md5
  # IPv4 local connections:
  host    all             all             127.0.0.1/32            md5
  # IPv6 local connections:
  #host    all             all             ::1/128                 trust

  host    all             all              172.18.18.0/24          md5
  host    replication     replica     172.18.18.101/32                 md5
  host    replication     replica     172.18.18.100/32                 md5
  注:后兩行作用->增加replica用戶,進行同步

  psql -p 3021 -U postgres -d postgres (主,99機器)
  postgres# CREATE ROLE replica login replication encrypted password 'replica'


  另需要修改postgresql.conf:
  port = 3021
  wal_level = hot_standby  # 這個是設置主為wal的主機
  max_wal_senders = 32 # 這個設置了可以最多有幾個流復制連接,差不多有幾個從,就設置幾個
  wal_keep_segments = 256 # 設置流復制保留的最多的xlog數目
  wal_sender_timeout = 60s # 設置流復制主機發送數據的超時時間     
  max_connections = 100 # 這個設置要注意下,從庫的max_connections必須要大於主庫的          

  啟動主:
  /usr/pgsql9.3.4/bin/pg_ctl restart -D /data/pgdata/pg_primary 

  如若要創建表空間及索引空間,參考《pg數據庫安裝手冊.txt》步驟7

6、從庫:172.18.18.101
su - postgres
123
1》mkdir /data/pgdata/pg_stand_by
2》mkdir /data/pgdata/pg_stand_by_data
3》/usr/pgsql9.3.4/bin/pg_basebackup -F p –progress -D /data/pgdata/pg_stand_by -h 172.18.18.99 -p 3021 -U replica –password
Password:
replica
成功之后,就可以看到這個目錄中有文件了

4》cp  /usr/pgsql9.3.4/share/recovery.conf.sample  /data/pgdata/pg_stand_by/recovery.conf
修改recovery.conf:
standby_mode = on # 這個說明這台機器為從庫
primary_conninfo = ‘host=172.18.18.99 port=3021 user=replica password=replica’ # 這個說明這台機器對應主庫的信息
recovery_target_timeline = ‘latest’ # 這個說明這個流復制同步到最新的數據

5》配置文件修改
postgresql.conf//修改port等等(直接拿99開發機配置庫配置文件替換)
另需要修改:
max_connections = 1000 # 一般查多於寫的應用從庫的最大連接數要比較大
hot_standby = on # 說明這台機器不僅僅是用於數據歸檔,也用於數據查詢
max_standby_streaming_delay = 30s # 數據流備份的最大延遲時間
wal_receiver_status_interval = 1s # 多久向主報告一次從的狀態,當然從每次數據復制都會向主報告狀態,這里只是設置最長的間隔時間
hot_standby_feedback = on # 如果有錯誤的數據復制,是否向主進行反饋

  pg_hba.conf //修改
  # TYPE  DATABASE        USER            ADDRESS                 METHOD  

  # "local" is for Unix domain socket connections only
  local   all             all                                     md5
  # IPv4 local connections:
  host    all             all             127.0.0.1/32            md5
  # IPv6 local connections:
  #host    all             all             ::1/128                 trust

  host    all             all              172.18.18.0/24          md5

  啟動從庫:
  chmod 700 /data/pgdata/pg_stand_by
  /usr/pgsql9.3.4/bin/pg_ctl start -D /data/pgdata/pg_stand_by

 

7、從庫:172.18.18.100

    操作同6

 

8.查看進程:
99機器:
postgres 21374 21359 0 Aug06 ? 00:00:03 postgres: wal sender process replica 172.18.18.101(43399) streaming 0/5008F38
postgres 22136 21359 0 Aug06 ? 00:00:03 postgres: wal sender process replica 172.18.18.100(16065) streaming 0/5008F38

100機器:
ps -ef |grep postgres
postgres 23111 23107 0 Aug06 ? 00:00:13 postgres: wal receiver process streaming 0/5008F38

101機器:
postgres 14229 13892 0 Aug06 ? 00:00:10 postgres: wal receiver process streaming 0/5008FD8

postgres=# select * from pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | state | sent_location | write_location | flush_location | replay_location | sync_priority | sync_state
——-+———-+———+——————+—————+—————–+————-+——————————-+———–+—————+—————-+—————-+—————–+—————+————
21374 | 16384 | replica | walreceiver | 172.18.18.101 | | 43399 | 2015-08-06 16:46:34.428745+08 | streaming | 0/5008FD8 | 0/5008FD8 | 0/5008FD8 | 0/5008FD8 | 0 | async
22136 | 16384 | replica | walreceiver | 172.18.18.100 | | 16065 | 2015-08-06 17:08:42.798029+08 | streaming | 0/5008FD8 | 0/5008FD8 | 0/5008FD8 | 0/5008FD8 | 0 | async

 

主備區分:
1》通過自帶的函數,是備機則是true
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
——————-
f
(1 row)

2》pg_controldata命令
[postgres@KFJK pg_primary]$ /usr/pgsql9.3.4/bin/pg_controldata /data/pgdata/pg_primary
pg_control version number: 937
Catalog version number: 201306121
Database system identifier: 6179727005669384190
Database cluster state: in production

[postgres@localhost pg_stand_by]$ /usr/pgsql9.3.4/bin/pg_controldata /data/pgdata/pg_stand_by pg_control version number: 937 Catalog version number: 201306121 Database system identifier: 6179727005669384190 Database cluster state: in archive recovery 登錄方式: psql -p 3021 -U postgres -d postgres (主,99機器) psql -p 3121 -U postgres -d postgres (從,101機器) psql -p 3221 -U postgres -d postgres (從,100機器) 

 

 

主從切換操作:

1》主庫宕機或者測試主備切換情況下停掉主庫:/usr/pgsql9.3.4/bin/pg_ctl stop -D /data/pgdata/pg_primary -m fast 從庫會報日志錯誤信息: [postgres@localhost pg_log]$ tail -100 postgresql-2015-08-07_000000.csv 2015-08-07 16:55:10.588 CST,,,23894,,55c4726e.5d56,1,,2015-08-07 16:55:10 CST,,0,FATAL,XX000,"could not connect to the primary server: could not connect to server: Connection refused Is the server running on host ""172.18.18.99"" and accepting TCP/IP connections on port 3021? ",,,,,,,,"libpqrcv_connect, libpqwalreceiver.c:106","" 

2》原從庫操作(原主庫宕機情況下將其作為主庫操作):
在之前備機上的recovery.conf中配置trigger_file = ‘/data/pgdata/pg_stand_by/trigger.unl’
touch /data/pgdata/pg_stand_by/trigger.unl
修改 pg_hba.conf:
增加
host replication replica 172.18.18.99/32 md5
host replication replica 172.18.18.100/32 md5
重啟從庫: /usr/pgsql9.3.4/bin/pg_ctl restart -D /data/pgdata/pg_stand_by
查看是否切換成功:/usr/pgsql9.3.4/bin/pg_controldata /data/pgdata/pg_stand_by -》Database cluster state: in production 表示是主庫
recovery.conf文件名字變成了recovery.done

3》原主庫操作(恢復原主庫為從庫):
cp /usr/pgsql9.3.4/share/recovery.conf.sample /data/pgdata/pg_primary/recovery.conf
修改recovery.conf:
recovery_target_timeline = ‘latest’
standby_mode = on
primary_conninfo = ‘host=172.18.18.101 port=3121 user=replica password=replica’
修改postgresql.conf文件:
hot_standby = on
啟動原主庫(當前從庫):/usr/pgsql9.3.4/bin/pg_ctl start -D /data/pgdata/pg_primary
4》修改100機器從庫對應的主庫信息:
修改recovery.conf :
primary_conninfo = ‘host=172.18.18.101 port=3121 user=replica password=replica’
重啟從庫:/usr/pgsql9.3.4/bin/pg_ctl restart -D /data/pgdata/pg_stand_by -m fast
5》檢查主從是否切換成功:
在新的主庫上執行:
postgres=# select * from pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | state | sent_location | write_loca
tion | flush_location | replay_location | sync_priority | sync_state
——-+———-+———+——————+—————+—————–+————-+——————————-+———–+—————+———–
—–+—————-+—————–+—————+————
32162 | 16384 | replica | walreceiver | 172.18.18.99 | | 47980 | 2015-08-11 15:16:12.925255+08 | streaming | 0/7002F38 | 0/7002F38
| 0/7002F38 | 0/7002F38 | 0 | async
32181 | 16384 | replica | walreceiver | 172.18.18.100 | | 13258 | 2015-08-11 15:18:28.106803+08 | streaming | 0/7002F38 | 0/7002F38
| 0/7002F38 | 0/7002F38 | 0 | async
(2 rows)

 表明切換成功 

 


免責聲明!

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



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