pgpool-II读写分离
生活随笔
收集整理的這篇文章主要介紹了
pgpool-II读写分离
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
IP 角色 端口 192.168.0.31 master 5432 pgpool-II 9999 192.168.0.32 slave 5432
一、基礎(chǔ)環(huán)境配置
host設(shè)置修改名稱,如果在下面的配置中無法使用主機名稱,則使用IP地址
| hostnamectl set-hostname master hostnamectl set-hostname slave 192.168.0.31 master 192.168.0.32 slave |
安裝
PostgreSQL
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # 添加源 rpm -Uvh https://yum.postgresql.org/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm # 下載 yum install postgresql10-server postgresql10 # 安裝 /usr/pgsql-10/bin/postgresql-10-setup initdb# 啟動 systemctl enable postgresql-10.service systemctl start postgresql-10.service# 驗證 su - postgres -c "psql" # 出現(xiàn)以下信息則成功 psql (10.0) Type "help" for help. postgres=## 創(chuàng)建密碼 postgres=# ALTER USER postgres WITH PASSWORD 'postgres';# 查看路徑(/var/lib/pgsql/10/data) postgres=# show data_directory;# 編輯文件 (vi /var/lib/pgsql/10/data/pg_hba.conf) # host all all 127.0.0.1/32 ident 修改為允許所有網(wǎng)絡(luò)登錄,并使用md5方式進(jìn)行認(rèn)證: # host all all 0.0.0.0/0 md5# 編輯文件 (vi /var/lib/pgsql/10/data/postgresql.conf) listen_addresses = '*' # 表示開放外網(wǎng)訪問# 打開防火墻,或者禁用防火墻 sudo firewall-cmd --add-service=postgresql --permanent sudo firewall-cmd --reload # 重啟 systemctl restart postgresql-10.service |
pgpool-II
| 1 2 3 4 5 6 7 8 9 10 11 | # 安裝 yum install https://www.pgpool.net/yum/rpms/4.2/redhat/rhel-7-x86_64/pgpool-II-pg10-4.2.4-1pgdg.rhel7.x86_64.rpm yum install https://www.pgpool.net/yum/rpms/4.2/redhat/rhel-7-x86_64/pgpool-II-pg10-debuginfo-4.2.4-1pgdg.rhel7.x86_64.rpm # 可選 yum install pgpool-II-https://www.pgpool.net/yum/rpms/4.2/redhat/rhel-7-x86_64/pgpool-II-pg10-devel-4.2.4-1pgdg.rhel7.x86_64.rpm yum install pgpool-II-https://www.pgpool.net/yum/rpms/4.2/redhat/rhel-7-x86_64/pgpool-II-pg10-extensions-4.2.4-1pgdg.rhel7.x86_64.rpm#啟動 systemctl enable pgpool.service systemctl start pgpool.service |
二、流復(fù)制
Master
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # 創(chuàng)建用于復(fù)制的用戶 su - postgres psql postgres=# CREATE ROLE pgrepuser REPLICATION LOGIN PASSWORD 'pgreppass'; # 編輯文件 (vi /var/lib/pgsql/10/data/pg_hba.conf) # host replication pgrepuser 0.0.0.0/0 md5# 編輯文件 (vi /var/lib/pgsql/10/data/postgresql.conf),修改配置(根據(jù)實際情況填寫) wal_level = hot_standby archive_mode = on max_wal_sender = 4 wal_keep_segments = 10# 重啟數(shù)據(jù)庫 systemctl restart postgresql-10.service |
Slave
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # 停止服務(wù) systemctl stop postgresql-10.servicesu - postgres # 使用 pg_basebackup 生成備庫 #1. 清空 $PGDATA 目錄 rm -rf /var/lib/pgsql/10/data# pg_basebackup 命令生成備庫 pg_basebackup -D /var/lib/pgsql/10/data -Fp -Xs -v -P -h master -U pgrepuser# 編輯文件 (vi /var/lib/pgsql/10/data/postgresql.conf) hot_standby = on# 新建文件 (vi /var/lib/pgsql/10/data/recovery.conf) #注意PGSQL-12的版本似乎不支持這個文件,直接改到postgresql.conf standby_mode = 'on' primary_conninfo = 'host=master port=5432 user=pgrepuser password=pgreppass' trigger_file = 'failover.now' recovery_target_timeline = 'latest'# 重啟數(shù)據(jù)庫 systemctl restart postgresql-10.service#驗證:在master新增數(shù)據(jù)slave節(jié)點可以看到數(shù)據(jù)。 |
三、讀寫分離
pgpool配置
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | cd /etc/pgpool-II cp -pv pgpool.conf.sample-stream pgpool.conf# 修改 vi pgpool.conf listen_addresses = '*'# 外網(wǎng)訪問 # 0為主庫 backend_hostname0 = 'master backend_port0 = 5432 backend_weight0 = 0 # 分配比例 backend_data_directory0 = '/var/lib/pgsql/10/data' backend_flag0 = 'ALLOW_TO_FAILOVER' backend_hostname1 = 'slave' backend_port1 = 5432 backend_weight1 = 1 backend_data_directory1 = '/var/lib/pgsql/10/data' backend_flag1 = 'ALLOW_TO_FAILOVER' #hba認(rèn)證 enable_pool_hba = on # 執(zhí)行l(wèi)og log_statement = on log_per_node_statement = on # 流復(fù)制 sr_check_user = 'replicator' # 流復(fù)制賬號 sr_check_password = '123456' # 流復(fù)制密碼 # 函數(shù)默認(rèn)分發(fā)到從節(jié)點,過濾如下 black_function_list = 'currval,lastval,nextval,setval,funcw_.*'# 修改 vi pool_hba.conf host all all 0.0.0.0/0 md5# 修改 vi pcp.conf pcp:e10adc3949ba59abbe56e057f20f883e # 密碼為123456# 生成pool_passwd pg_md5 123456# 與 postgresql 用戶密碼一致 pg_md5 -m -u postgres postgres# 啟動pgpool # systemctl restart pgpool.service pgpool -n -d > /etc/pgpool-II/pgpool.log 2>&1 &# 連接 su - postgres psql postgres -h master -p 9999 -U postgres# 節(jié)點信息 postgres=# show pool_nodes; node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay ---------+----------+------+--------+-----------+---------+------------+-------------------+------------------- 0 | master | 5432 | up | 0.000000 | primary | 28 | false | 0 1 | slave | 5432 | up | 1.000000 | standby | 6 | true | 0# 查看日志 tail -f /etc/pgpool-II/pgpool.log # 下面是測試情況: # select 1; 2017-10-30 06:38:25: pid 3637: LOG: DB node id: 1 backend pid: 3658 statement: select * from test where id = 1;# update test set name = 'test' where id = 2; DB node id: 0 backend pid: 8032 statement: update test set name = 'test' where id = 2; #/*REPLICATION*/select 1; # 強制master節(jié)點執(zhí)行 DB node id: 0 backend pid: 8032 statement: /*REPLICATION*/select 1; # DB node id,0表示主節(jié)點執(zhí)行,1表示從節(jié)點 |
四、錯誤解決
端口占用
| 1 2 3 4 5 6 | 2017-10-30 01:50:21: pid 3790: FATAL: failed to bind a socket: "/tmp/.s.PGSQL.9998" 2017-10-30 01:50:21: pid 3790: DETAIL: bind socket failed with error: "Address already in use"# 非正常結(jié)束導(dǎo)致的,刪除以下目錄即可 rm -f /tmp/.s.PGSQL.9999 rm -f /tmp/.s.PGSQL.9898 |
?
五、后續(xù)優(yōu)化
宕機主從切換
| 1 2 | # 修改 vi pgpool.conf follow_master_command = '/etc/pgpool-II/failover_stream.sh' |
新建切換腳本
| 1 2 3 4 5 6 7 8 9 10 11 | #! /bin/sh # Failover command for streaming replication. # Arguments: $1: new master hostname. new_master=$1 trigger_command="$PGHOME/bin/pg_ctl promote -D $PGDATA" # Prompte standby database. /usr/bin/ssh -T $new_master $trigger_command exit 0; |
pgpool集群
配置虛擬ip(delegate_IP),使用WATCHDOG監(jiān)控,服務(wù)A宕機時,服務(wù)B自動接管虛擬IP對外提供服務(wù)。?
總結(jié)
以上是生活随笔為你收集整理的pgpool-II读写分离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爱发4.2.8自动发卡去授权源码
- 下一篇: 低代码开发,全民开发,淘汰职业程序员!