pgpool-II(http://pgpool.projects.postgresql.org/ )是一箇中間 件,工做在PostgreSQL多服 務 器和PostgreSQL數據 庫 客 戶 端之 間。php
它提供瞭如下功能node
連 接池: pgpool -Ⅱ保存 連 接到PostgreSQL服 務 器,並重複利用具備相同屬性的新的 連 接(即用 戶 名,數據 庫 , 協議 的版本),減小 連 接的開 銷 ,並提升了系 統 的總體吞吐量。 複製: pgpool - II能夠管理多個PostgreSQL服務 器。 使用複製功能,能夠 實時備份在 2個或多個物理磁 盤 上,所以即便在硬 盤出故障的時候也不用中止服務。
sql
負載 平衡: 若是數據 庫 是複製,任何服 務 器上 執 行一個SELECT 查 詢 將返回相同的 結 果。 pgpool -Ⅱ採用一個複製功能 優 勢 是,以減小多個服 務 器之 間 分配上的SELECT 查 詢 每一個PostgreSQL服 務 器的 負載 ,提升系 統 的總體吞吐量。在最好的,性能的提升比例的PostgreSQL服 務 器的數量。在同一 時間有 大量用 戶 的 查 詢的時候,負載 平衡的狀況下有最佳的 執 行。 數據庫
鏈接超 過限制 : 有一個關於與 PostgreSQL 的最大並 發連 接數限制,最大 鏈接數超事後 的 連 接被拒 絕 。 設 置最大 連 接數,可是增長的 資 源消耗和影響系 統 性能。 pgpool - II 也有 對 最大 連 接數的限制,但 額 外的 連 接將被排 隊 ,而不是當即返回 錯誤 。 vim
並行查 詢 : 使用並行 查 詢 功能,數據可分佈在多個服 務 器中,以便 查 詢 能夠 執 行全部服 務 器上同 時 減小 總 體 執 行 時間 。 並行 查 詢 的工做 時 候 , 尋 找最佳的大 規 模的數據。bash
進行pgpool搭建前須要配置好postgresql的流複製,操做步驟參考http://xiajie.blog.51cto.com/6044823/1662222
1、安裝
服務器
wget http://www.pgpool.net/download.php?f=pgpool-II-3.4.0.tar.gz tar -zxvf pgpool-3.4.0.tar.gz cd pgpool-II-3.4.0/ ./configure --prefix=/usr/local/pgpool --with-pgsql=path --with-pgsql=/usr/local/pgsql make make install chown postgres.postgres /usr/local/pgpool/ -R chown postgres.postgres /usr/src/pgpool-II-3 -R mkdir /var/run/pgpool chown postgres.postgres /var/run/pgpool/ #切換postgres 用戶安裝一些函數 su - postgres cd /usr/src/pgpool-II-3.4.0/src/sql/ make make install cd pgpool-recovery/ make install cd ../pgpool-regclass/ make install
2、配置
ssh
cd /usr/local/pgpool/etc cp pcp.conf.sample pcp.conf pg_md5 postgres e8a48653851e28c69d0506508fb27fc5 echo "postgres:e8a48653851e28c69d0506508fb27fc5" >> pcp.conf echo "postgres:e8a48653851e28c69d0506508fb27fc5" >> pool_passwd cp pool_hba.conf.sample pool_hba.conf vim pool_hba.conf host all postgres db2 md5 listen_addresses = '*' #容許全部主機監聽 port = 9999 #訪問端口 backend_hostname0 = 'db1' #DBmaster ip backend_port0 = 5432 #DBmaster postgresql 端口 backend_weight0 = 1 #權重 backend_data_directory0 = '/opt/data' #DBmaster 數據庫目錄 backend_flag0 = 'ALLOW_TO_FAILOVER' #容許切換 backend_hostname0 = 'db2' backend_port0 = 5432 backend_weight0 = 1 backend_data_directory0 = '/opt/data' backend_flag0 = 'ALLOW_TO_FAILOVER' enable_pool_hba = on #隨意,自由定製,使用 pool_hba.conf 對client的驗證 pool_passwd = 'pool_passwd' #md5驗證文件 sr_check_user = 'postgres' #用來故障切換的用戶 failover_command = '/usr/local/pgsql/bin/failover_command.sh %d %H /tmp/trigger_file'
故障切換腳本
tcp
vim /usr/local/pgsql/bin/failover_command.sh #! /bin/sh # Failover command for streaming replication. # This script assumes that DB node 0 is primary, and 1 is standby. # # If standby goes down, do nothing. If primary goes down, create a # trigger file so that standby takes over primary node. # # Arguments: $1: failed node id. $2: new master hostname. $3: path to # trigger file. failed_node=$1 new_master=$2 trigger_file=$3 # Do nothing if standby goes down. #if [ $failed_node = 1 ]; then # exit 0; #fi # Create the trigger file. /usr/bin/ssh -T $new_master /bin/touch $trigger_file exit 0; chmod +x /usr/local/pgsql/bin/failover_command.sh
3、調試
啓動命令,帶有日誌輸出
ide
[postgres@db1 etc]$ pgpool -nd >/tmp/pgpool.log 2>&1 & [postgres@db1 etc]$ netstat -ntlp (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:9898 0.0.0.0:* LISTEN 16664/pgpool tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 16664/pgpool tcp 0 0 :::22 :::* LISTEN - tcp 0 0 ::1:25 :::* LISTEN - tcp 0 0 :::9999 :::* LISTEN 16664/pgpool
登陸
[postgres@db1 etc]$ psql -U postgres -h db1 -p 9999 psql (9.2.1) Type "help" for help. postgres=# show pool_nodes; node_id | hostname | port | status | lb_weight | role ---------+----------+------+--------+-----------+--------- 0 | db1 | 5432 | 2 | 0.500000 | primary 1 | db2 | 5432 | 2 | 0.500000 | standby (2 rows) postgres=# create database db0; CREATE DATABASE
2:啓動
3:死啦
測試能夠登陸,能夠讀寫
4、故障切換
首先中止DBmaster
[postgres@db1 etc]$ pg_ctl -m fast stop
登陸查看
[postgres@db1 etc]$ psql -U postgres -h db1 -p 9999 postgres=# show pool_nodes; node_id | hostname | port | status | lb_weight | role ---------+----------+------+--------+-----------+--------- 0 | db1 | 5432 | 3 | 0.500000 | standby 1 | db2 | 5432 | 2 | 0.500000 | primary (2 rows)
此時DBslave顯示的日誌信息
[postgres@db2 data]$ FATAL: replication terminated by primary server
LOG: record with zero length at 0/10000FE0
LOG: trigger file found: /tmp/trigger_file
LOG: redo done at 0/10000F80
LOG: last completed transaction was at log time 2015-06-17 11:05:44.379009+08
LOG: selected new timeline ID: 2
LOG: archive recovery complete
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
DBmaster 已經死啦,狀態切換爲standby;DBslave切換爲primary;測試可讀寫
日誌提示,發現 trigger_file文件,進行切換
DBslave 切換爲 主服務器
recover.conf自動更改成recover.done
[postgres@db2 data]$ ll /opt/data/recovery.done recovery.done
故障切換成功
5、恢復DBmaster
一、同步DBmaster至DBslave
pg_basebackup -D $PGDATA -Fp -Xs -v -h db1 -p 5432 -U postgres
二、修改配置文件
listen_addresses = '*' port = 5432 hot_standby = on
三、修改recover文件
mv recover.done recover.conf vim recover.conf primary_conninfo = 'host=172.16.0.133 port=5432 user=postgres'
四、啓動DBslave
五、添加node
pcp_attach_node -d 5 db1 9898 postgres postgres 0 pcp_attach_node -d 5 db1 9898 postgres postgres 1
登陸查看
postgres=# show pool_nodes; node_id | hostname | port | status | lb_weight | role ---------+----------+------+--------+-----------+--------- 0 | db1 | 5432 | 2 | 0.500000 | primary 1 | db2 | 5432 | 2 | 0.500000 | standby (2 rows)
恢復正常Game Over !