Lightdb Pgpool-II 读写分离使用
目錄
一、介紹
二、pgpool-II下載
三、pgpool-II 安裝
????????1.源碼安裝前提
????????2.解壓編譯安裝
? ? ? ? 3.添加環(huán)境變量
? ? ? ? 4.配置pool_hba.conf
? ? ? ? 5.配置pcp.conf
? ? ? ? 6.配置pool_passwd
? ? ? ? 7.配置pgpool.conf
? ? ? ? 8.pgpool的啟動(dòng)和關(guān)閉
? ? ? ? 9.查看集群狀態(tài)
四、驗(yàn)證讀寫分離
五、測(cè)試(模擬master宕機(jī))
一、介紹
pgpool-II是位于LightDB服務(wù)器和 LightDB 數(shù)據(jù)庫(kù)客戶端之間的中間件,也是現(xiàn)在比較成熟的讀寫分離的中間件,它提供以下功能:
??
- 連接池
????????Pgpool-II維護(hù)與PostgreSQL 服務(wù)器的已建立連接,并在出現(xiàn)具有相同屬性(即用戶名,數(shù)據(jù)庫(kù),協(xié)議版本和其他連接參數(shù),如果有)的新連接時(shí)重用它們。它減少了連接開銷并改善了系統(tǒng)的整體吞吐量。
- 負(fù)載均衡
????????如果復(fù)制了數(shù)據(jù)庫(kù)(因?yàn)橐詮?fù)制模式或主/從模式運(yùn)行),則在任何服務(wù)器上執(zhí)行SELECT查詢都將返回相同的結(jié)果。Pgpool-II 利用復(fù)制功能來(lái)減少每個(gè)PostgreSQL服務(wù)器上的負(fù)載。它通過(guò)在可用服務(wù)器之間分配SELECT查詢來(lái)做到這一點(diǎn),從而提高了系統(tǒng)的整體吞吐量。在理想情況下,讀取性能可以與PostgreSQL服務(wù)器的數(shù)量成比例地提高。在許多用戶同時(shí)執(zhí)行許多只讀查詢的情況下,負(fù)載平衡效果最佳。
- 看門狗
????????跟keepalived一樣,對(duì)其他pgpool-II節(jié)點(diǎn)執(zhí)行生命檢查,以檢測(cè)Pgpoll-II的故障。如果活動(dòng)Pgpool-II發(fā)生故障,則備用 Pgpool-II可以提升為活動(dòng)狀態(tài),并接管虛擬IP
二、pgpool-II下載
Downloads - pgpool Wiki
三、pgpool-II 安裝
????????1.源碼安裝前提
需要有l(wèi)ightdb庫(kù)環(huán)境
GNU make 版本是3.8及以上
#查看版本命令 make --version????????2.解壓編譯安裝
tar -zxvf pgpool-II-4.4.0.tar.gz -C /data1/lightdb/wuxj cd /data1/lightdb/wuxj/pgpool-II-4.4.0#--prefix 安裝路徑 #-with-pgsql lightdb LTHOME環(huán)境路徑 ./configure --prefix=/data1/lightdb/wuxj/pgpool -with-pgsql=/data1/lightdb/lightdb-em-13.8-22.4-d53528018-el7.x86_64/lightdb-x-for-em/13.8-22.4make make install? ? ? ? 3.添加環(huán)境變量
vim ~/.bashrc #添加如下信息 export PGPOOL_HOME=/data1/lightdb/wuxj/pgpool export PATH=${PGPOOL_HOME}/bin:${PATH}#使之生效 source ~/.bashrc? ? ? ? 4.配置pool_hba.conf
#進(jìn)入配置目錄
cd /data1/lightdb/wuxj/pgpool/etc/
#復(fù)制
cp pool_hba.conf.sample pool_hba.conf
增加如下內(nèi)容
host all all 0.0.0.0/0 md5? ? ? ? 5.配置pcp.conf
cp pcp.conf.sample pcp.conf
獲取lightdb用戶MD5密碼
加入lightdb:上面MD5輸出
# USERID:MD5PASSWD lightdb:7550896f8721834867162e708adfe6a6? ? ? ? 6.配置pool_passwd
(master)主節(jié)點(diǎn)登陸后執(zhí)行:select rolname,rolpassword from pg_authid;
[lightdb@lightdb-1 etc]$ ltsql -h10.19.70.49 -p30001 -dpostgres Password for user lightdb: ltsql (13.8, server 13.3) Type "help" for help.lightdb@postgres=# select rolname,rolpassword from pg_authid;rolname | rolpassword ---------------------------+-------------------------------------pg_monitor | pg_read_all_settings | pg_read_all_stats | pg_stat_scan_tables | pg_read_server_files | pg_write_server_files | pg_execute_server_program | pg_signal_backend | lightdb | md54f3305909c6724814ede585e873b845clt_probackup | ltcluster | md58b0b5b2bbd4c2a5f6ae630c8e0e3a961em | md5a7addd43aa6c1b5c21b5a7b7744f9a3d (12 rows)修改pool_passwd增加lightdb用戶的結(jié)果
形式為rolname:rolpassword例如:
lightdb:md54f3305909c6724814ede585e873b845c
? ? ? ? 7.配置pgpool.conf
#內(nèi)容如下:根據(jù)自己的配置進(jìn)行相應(yīng)的修改。相關(guān)目錄需要提前創(chuàng)建。如果相關(guān)目錄沒有權(quán)限則需要賦權(quán)
chown -R lightdb:lightdb?/data1/lightdb/wuxj/pgpool
listen_addresses = '*' port = 9999 socket_dir = '/data1/lightdb/wuxj/pgpool' pcp_listen_addresses = '*' pcp_port = 9898 pcp_socket_dir = '/data1/lightdb/wuxj/pgpool'backend_hostname0 = '10.19.70.49' #主庫(kù) backend_port0 = 30001 backend_weight0 = 1 backend_data_directory0 = '/home/lightdb/lightdb-22.2-5950/lightdb-x/13.3-22.2/data/defaultCluster' #數(shù)據(jù)庫(kù)data位置 backend_flag0 = 'ALLOW_TO_FAILOVER' # backend_hostname1 = '10.19.70.51' #從庫(kù) backend_port1 = 30001 backend_weight1 = 1 backend_data_directory1 = '/home/lightdb/lightdb-22.2-5950/lightdb-x/13.3-22.2/data/defaultCluster' #數(shù)據(jù)庫(kù)data位置 backend_flag1 = 'ALLOW_TO_FAILOVER'enable_pool_hba = onpid_file_name = '/data1/lightdb/wuxj/pgpool/pgpool.pid' ##pid文件位置 logdir = '/data1/lightdb/wuxj/pgpool/logs' ##日志位置replication_mode = off #開啟讀寫分離 load_balance_mode = onmaster_slave_mode = on master_slave_sub_mode = 'stream'sr_check_period = 5 sr_check_user = 'lightdb' ##主庫(kù)創(chuàng)建的用戶 sr_check_password = 'lightdb123' ##密碼 sr_check_database = 'postgres' # # # HEALTH CHECK 健康檢查 # health_check_period = 10 health_check_timeout = 20 health_check_user = 'lightdb' ##主庫(kù)創(chuàng)建的用戶 health_check_password = 'lightdb123' ##密碼 health_check_database = 'postgres' ##檢查的庫(kù)? ? ? ? 8.pgpool的啟動(dòng)和關(guān)閉
#啟動(dòng) pgpool -n -D >> /data1/lightdb/wuxj/pgpool/logs/pgpool.log 2>&1 &#關(guān)閉 pgpool -m fast stop? ? ? ? 9.查看集群狀態(tài)
[lightdb@lightdb-1 etc]$ ltsql -p9999 -dpostgres ltsql (13.8, server 13.3) Type "help" for help.lightdb@postgres=# show pool_nodes;node_id | hostname | port | status | pg_status | lb_weight | role | pg_role | select_cnt | load_balance_node | replication_delay | replication_state | replication_sync_state | last_status_change ---------+-------------+-------+--------+-----------+-----------+---------+---------+------------+-------------------+-------------------+-------------------+------------------------+---------------------0 | 10.19.70.49 | 30001 | up | up | 0.500000 | primary | primary | 0 | false | 0 | | | 2022-12-22 15:05:221 | 10.19.70.51 | 30001 | up | up | 0.500000 | standby | standby | 0 | true | 0 | | | 2022-12-22 15:05:22 (2 rows)lightdb@postgres=#四、驗(yàn)證讀寫分離
?#主節(jié)點(diǎn)
10.19.70.49
#備節(jié)點(diǎn)
10.19.70.51
? ? ? ? 新建兩張測(cè)試表
CREATE table new_test_wuxj( id int8, name varchar(100) );CREATE table new_test_wuxj_2( id int8, name varchar(100) );通過(guò)連接pgpool 對(duì)表進(jìn)行插入和查詢
try {Class.forName("org.postgresql.Driver");Connection connection = DriverManager.getConnection("jdbc:postgresql://10.20.31.205:9999/postgres", "lightdb", "lightdb123");String sql = "insert into new_test_wuxj(id,name) values(?,?)";PreparedStatement preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1,1);preparedStatement.setString(2,"nihao");preparedStatement.execute();preparedStatement = connection.prepareStatement("select * from new_test_wuxj");ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){String name = resultSet.getString("name");System.out.println(name);}resultSet.close();preparedStatement.close();connection.close();} catch (Exception e) {e.printStackTrace();}查看主備日志可以看到寫操作在主庫(kù),讀操作在備庫(kù)。
開啟手動(dòng)事務(wù)
try {Class.forName("org.postgresql.Driver");Connection connection = DriverManager.getConnection("jdbc:postgresql://10.20.31.205:9999/postgres", "lightdb", "lightdb123");String sql = "insert into new_test_wuxj_2(id,name) values(?,?)";connection.setAutoCommit(false);PreparedStatement preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1,1);preparedStatement.setString(2,"讀寫均在主庫(kù)");preparedStatement.execute();preparedStatement = connection.prepareStatement("select * from new_test_wuxj_2");ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){String name = resultSet.getString("name");System.out.println(name);}connection.commit();resultSet.close();preparedStatement.close();connection.close();} catch (Exception e) {e.printStackTrace();}查看主備日志可以看到讀寫操作在主庫(kù)。
五、測(cè)試(模擬master宕機(jī))
1.關(guān)閉master數(shù)據(jù)庫(kù)
查看pgpool ,master關(guān)閉后,從節(jié)點(diǎn)的連接中斷后又成功連接上,發(fā)現(xiàn)master已經(jīng)是standby了,且down機(jī)了,slave升為了primary
2.恢復(fù)原先主節(jié)點(diǎn)
[lightdb@iZvv70ftvto0eaujmkhmu3Z 13.3-22.2]$ lt_ctl -D $LTDATA start waiting for server to start....2022-12-22 16:51:55.584906T,,,,,postmaster,,00000,2022-12-22 16:51:55 CST,0,1592013, LOG: LightDB autoprewarm: prewarm dbnum=0 2022-12-22 16:51:55.602435T,,,,,postmaster,,00000,2022-12-22 16:51:55 CST,0,1592013, LOG: ltaudit extension initialized ......2022-12-22 16:52:01.757518T,,,,,postmaster,,00000,2022-12-22 16:51:55 CST,0,1592013, LOG: redirecting log output to logging collector process 2022-12-22 16:52:01.757518T,,,,,postmaster,,00000,2022-12-22 16:51:55 CST,0,1592013, HINT: Future log output will appear in directory "log". ............. done server started查看集群狀態(tài)master成為了新的standby
總結(jié)
以上是生活随笔為你收集整理的Lightdb Pgpool-II 读写分离使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 07 数据查询语言(DQL)--行选择(
- 下一篇: 运算放大器---功耗(Iq需求)