@(學(xué)習(xí))[PostgreSQL, pgpool-II, 高可用]
PostgreSQLpgpool-II復(fù)制方案Pgpool-II介紹pgpool-II安裝1 安裝pgpool-II yum源并安裝pgpool-II2 添加Pgpool-II運(yùn)行用戶3 設(shè)置pcpconf4 設(shè)置Pgpool-II配置文件5 配置backend信息6 修改認(rèn)證方式61 修改pgpool-II的認(rèn)證方式為md562 修改PostgreSQL的認(rèn)證方式為md57 測試pgpool-II同步安裝pgpoolAdmin不推薦1 解壓pgpoolAdmin至web目錄使用戶能訪問其php2 php-fpm運(yùn)行用戶和pgpool用戶統(tǒng)一3 根據(jù)向?qū)瓿砂惭b4 一些細(xì)節(jié)說明5 phpini的disable_functions設(shè)置6 pgpoolAdmin中pcp_stop_pgpool參數(shù)總結(jié)
pgpool-II是PostgreSQL服務(wù)器之間一種有效的中間件和PostgreSQL數(shù)據(jù)庫客戶端。它提供了以下功能。
連接池 pgpool-II保存到PostgreSQL服務(wù)器的連接,當(dāng)一個相同新連接(如用戶名、數(shù)據(jù)庫、協(xié)議版本)進(jìn)來時,重用他們。它減少了連接開銷,提高了系統(tǒng)的整體吞吐量。 復(fù)制 pgpool-II可以管理多個PostgreSQL服務(wù)器。使用復(fù)制功能可以使2個或更多的物理磁盤上創(chuàng)建一個實(shí)時備份,這樣服務(wù)不會因服務(wù)器的磁盤故障而中斷。 負(fù)載平衡 如果數(shù)據(jù)庫是復(fù)制的,在任何服務(wù)器上執(zhí)行一個SELECT查詢會返回相同的結(jié)果。pgpool-II復(fù)制特性的優(yōu)勢在于減少每個PostgreSQL服務(wù)器上的負(fù)載,因為它可以使用分布在多個服務(wù)器之間進(jìn)行SELECT查詢,從而提高系統(tǒng)的整體吞吐量。最好是查詢和PostgreSQL服務(wù)器數(shù)量成一定比例,多用戶同時執(zhí)行多查詢達(dá)到負(fù)載均衡最好的效果。 限制連接數(shù) PostgreSQL的最大并發(fā)連接數(shù)有一定限制的,當(dāng)超過限制的連接數(shù)后,連接會被拒絕。然而,設(shè)置增加最大連接數(shù)又會增加資源消耗,影響系統(tǒng)性能。pgpool-II也有最大連接數(shù)限制,但超過的連接進(jìn)來時是進(jìn)行立即排隊,而不是返回一個錯誤。
pgpool-II交互PostgreSQL的后端和前端協(xié)議時,起著繼電器的作用。因此,數(shù)據(jù)庫應(yīng)用程序(前端)認(rèn)為pgpool-II是真實(shí)的PostgreSQL服務(wù)器,服務(wù)器(后端)認(rèn)為pgpool-II是它的客戶端之一。因為pgpool-II在服務(wù)器和客戶端是透明的,所以pgpool-II可以使用現(xiàn)有的數(shù)據(jù)庫應(yīng)用程序而做到幾乎不修改它們。
環(huán)境: CentOS6.8 PostgreSQL9.5 pgpool-II-pg95-3.6.1
從官網(wǎng)找到相關(guān)yum源rpm包,使用rpm安裝后,并使用yum安裝pgpool-II
rpm -ivh http://www.pgpool.net/yum/rpms/3.6/redhat/rhel-6-x86_64/pgpool-II-release-3.6-1.noarch.rpmyum -y install pgpool-II-pg95yum -y install pgpool-II-pg95-debuginfoyum -y install pgpool-II-pg95-develyum -y install pgpool-II-pg95-extensionschkconfig pgpool on # 添加開機(jī)啟動service start/stop pgpool # 服務(wù)啟/停添加Pgpool-II運(yùn)行用戶,并給相關(guān)目錄的讀寫權(quán)限
[root@im110 pgpool-II]# useradd pgpool [root@im110 pgpool-II]# passwd pgpoolChanging passWord for user pgpool.New password: Retype new password: passwd: all authentication tokens updated successfully.[root@im110 pgpool-II]# chown -R pgpool.pgpool /etc/pgpool-II[root@im110 pgpool-II]# mkdir -p /var/run/pgpool/[root@im110 pgpool-II]# chown pgpool.pgpool /var/run/pgpool/修改啟動腳本的pgpool運(yùn)行用戶:
內(nèi)容格式為如下,一行一個,#號為注釋標(biāo)識
username:[md5 encrypted password]
[md5 encrypted password] 可以使用如下命令生成
$ pg_md5 pgpoolba777e4c2f15c11ea8ac3be7e0440aa0使用pg_md5 -p會隱藏輸入的密碼
$ pg_md5 -ppassword: your_password配置文件pcp.conf必須允許pgpool執(zhí)行用戶可讀。
pgpool.conf是Pgpool-II的主配置文件。啟動Pgpool-II時可以使用 -f 參數(shù)指定 pgpool.conf路徑, 默認(rèn)是使用/etc/pgpool.conf.
Pgpool-II每個模式對應(yīng)的配置文件模板
復(fù)制一份作為你的配置文件:
# cd /etc# cp pgpool.conf.sample-replication pgpool.conf在pgpool.conf中加入如下格式的配置,其中0為backend主機(jī)號,不能重復(fù)。
backend_socket_dir = '/tmp'backend_hostname0 = '10.1.0.110'backend_port0 = 5432backend_weight0 = 1backend_data_directory0 = '/var/lib/pgsql/9.5/data/'backend_flag0 = 'ALLOW_TO_FAILOVER'為了更安全,修改認(rèn)證方式為md5
vim /etc/pgpool-II/pool_hba.conf
vim pg_hba.conf
這里作簡單測試,在pgpool-II入口創(chuàng)建數(shù)據(jù)庫,看各節(jié)點(diǎn)是否自動創(chuàng)建,刪除后,看各節(jié)點(diǎn)是否自動刪除。
[root@im109 ~]# psql -U postgres -h 10.1.0.115 -p 9999Password for user postgres: psql (9.5.6)Type "help" for help.postgres=# show pool_nodes; node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay ---------+------------+-------+--------+-----------+--------+------------+-------------------+------------------- 0 | 10.1.0.110 | 54321 | up | 0.500000 | master | 0 | true | 0 1 | 10.1.0.109 | 54321 | up | 0.500000 | slave | 0 | false | 0(2 rows)postgres=# postgres=# /l List of databases Name | Owner | Encoding | Collate | Ctype | access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres wiseucmsg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (4 rows)postgres=# create database test01;CREATE DATABASEpostgres=# /q[root@im109 ~]# psql -U postgres -h 10.1.0.110 -p 54321Password for user postgres: psql (9.5.6)Type "help" for help.postgres=# /l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres test01 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | wiseucmsg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (5 rows)postgres=# /q[root@im109 ~]# psql -U postgres -h 10.1.0.109 -p 54321 Password for user postgres: psql (9.5.6)Type "help" for help.postgres=# /l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres test01 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | wiseucmsg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (5 rows)postgres=# /q[root@im109 ~]# psql -U postgres -h 10.1.0.115 -p 9999 Password for user postgres: psql (9.5.6)Type "help" for help.postgres=# /l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres test01 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | wiseucmsg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (5 rows)postgres=# drop database test01;DROP DATABASEpostgres=# /l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres wiseucmsg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (4 rows)postgres=# /q[root@im109 ~]# psql -U postgres -h 10.1.0.110 -p 54321Password for user postgres: psql (9.5.6)Type "help" for help.postgres=# /l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres wiseucmsg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (4 rows)postgres=# /q[root@im109 ~]# psql -U postgres -h 10.1.0.109 -p 54321Password for user postgres: psql (9.5.6)Type "help" for help.postgres=# /l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres wiseucmsg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (4 rows)postgres=# /q[root@im109 ~]#測試成功,說明從pgpool-II入口操作,各節(jié)點(diǎn)會同步數(shù)據(jù)。
雖然官網(wǎng)提供此工具,用于頁面管理pgpool,但是我覺得不安全,且可能bug較多。以下文中會有一些bug列出來。 安裝方法:
pgpoolAdmin使用pcp命令控制pgpool啟停,因此需要統(tǒng)一php和pgpool運(yùn)行用戶,以便pgpoolAdmin從頁面控制pgpool啟停。可參考上文中2.2步驟操作。
訪問安裝頁面,完成檢測,完成安裝。 http://yourweb/pgpooladmin/install/
從上圖看到,3.5下面的勾處未有文字提示,我此處為通過。因為我裝了php-psql擴(kuò)展。從源碼中也可以看出,是檢測pgsql擴(kuò)展。
.pcppass
文件格式如下(pgpool3.5以下用到): 它的作用是用于指定連接postgresql的連接串,包括主機(jī)、端口、用戶、密碼。
完全設(shè)置好的完整功能如下: 3.4.1) 顯示pgpoolAdmin運(yùn)行機(jī)器的hostname 若php報warning,可在hosts中添加IP對應(yīng)主機(jī)名
3.4.2) 此處為pgpoolAdmin的登錄用戶,superuser: yes表示在pgpool數(shù)據(jù)庫中,用戶為管理員用戶。此數(shù)據(jù)庫可以pgpool下,也可分離到其它postgresql中。若此處非yes,則節(jié)點(diǎn)相關(guān)操作為不可操作的灰色。代碼中并未指定host,所以只能是使用socket。
3.4.3) 頁面中顯示節(jié)點(diǎn)是否連接,是在pgpool中show pool_nodes命令下的節(jié)點(diǎn)status.
[root@im109 ~]# psql -U postgres -h 10.1.0.115 -p 9999Password for user postgres: psql (9.5.6)Type "help" for help.postgres=# show pool_nodes; node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay ---------+------------+-------+--------+-----------+--------+------------+-------------------+------------------- 0 | 10.1.0.110 | 54321 | up | 0.500000 | master | 0 | true | 0 1 | 10.1.0.109 | 54321 | up | 0.500000 | slave | 0 | false | 0(2 rows)其它命令參考官方文檔: http://www.pgpool.net/docs/latest/en/html/reference.html
3.4.4) pgpool.conf 設(shè)置中的健康檢查,檢查postgressql是否啟動 此處是通過連接各節(jié)點(diǎn)是否成功來判斷的,因此需要在各節(jié)點(diǎn)創(chuàng)建用于連接的角色。
源代碼中php調(diào)用的exec執(zhí)行pcp相關(guān)命令, 因此需要將php.ini配置文件中的disable_functions中的exec去掉,以允許php使用該函數(shù)。
在pgpool-II3.6中,pcp_stop_pgpool用法如下:
root@im110 pgpooladmin]# pcp_stop_pgpool --helppcp_stop_pgpool - terminate pgpool-IIUsage:pcp_stop_pgpool [OPTION...] Options: -U, --username=NAME username for PCP authentication -h, --host=HOSTNAME pgpool-II host -p, --port=PORT PCP port number -w, --no-password never prompt for password -W, --password force password prompt (should happen automatically) -m, --mode=MODE MODE can be "smart", "fast", or "immediate" -d, --debug enable debug message (optional) -v, --verbose output verbose messages -?, --help print this helppgpoolAdmin中需要做相應(yīng)修改。
|
新聞熱點(diǎn)
疑難解答
圖片精選