postgresql 9.5 pgpool 主从复制 以及错误解决

PostgreSQL+pgpool-II复制方案

这里不做功能的描述,只写搭建的过程和遇到的一些问题

1 系统

[root@mysqlhq ~]# cat /etc/redhat-release
Kylin Linux release 3.3.1707 (Core)

2 安装pg 复制

 详细见 https://www.cnblogs.com/yhq1314/p/10119556.html

3 安装pgpool

使用yum安装,网上大多是源码tar的安装,

下载rpm包

https://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/repoview/

-rw-r--r-- 1 root root 716528 12月 24 11:13 pgpool-II-95-3.5.18-1.rhel7.x86_64.rpm
-rw-r--r-- 1 root root 1706944 12月 24 11:14 pgpool-II-95-debuginfo-3.5.18-1.rhel7.x86_64.rpm
-rw-r--r-- 1 root root 14384 12月 24 11:12 pgpool-II-95-devel-3.5.18-1.rhel7.x86_64.rpm

[root@mysql3 soft]# yum install -y pgpool-II-95-3.5.18-1.rhel7.x86_64.rpm
[root@mysql3 soft]# cd /etc/pgpool-II

添加Pgpool-II运行用户
[root@mysql3 data]# useradd pgpool
[root@mysql3 data]# passwd pgpool
Changing password for user pgpool.
New password: ##pgpool
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@mysql3 data]# chown -R pgpool.pgpool /etc/pgpool-II
[root@mysql3 data]# mkdir -p /var/run/pgpool/
[root@mysql3 data]# chown pgpool.pgpool /var/run/pgpool/
[root@mysql3 pgpool-II]# pwd
/etc/pgpool-II

[root@mysql3 pgpool-II]# vim pool_hba.conf
# TYPE DATABASE USER CIDR-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
host all all 192.168.19.227/32 md5
host all all ::1/128 md5

[root@mysql3 pgpool-II]# pg_md5 postgres
e8a48653851e28c69d0506508fb27fc5
[root@mysql3 pgpool-II]# vim pcp.conf
# USERID:MD5PASSWD
postgres:e8a48653851e28c69d0506508fb27fc5
[root@mysql3 pgpool-II]# chmod u+s /sbin/ifconfig && chmod u+s /usr/sbin
[root@mysql3 pgpool-II]# vim pgpool.conf
# CONNECTIONS
listen_addresses = '*'
port = 9999
pcp_listen_addresses = '*'
pcp_port = 9898

# - Backend Connection Settings -
backend_hostname0 = '192.168.19.227'
backend_port0 = 5432
backend_weight0 = 1
backend_data_directory0 = '/var/lib/pgsql/data'
backend_flag0 = 'ALLOW_TO_FAILOVER'

backend_hostname1 = '192.168.19.145'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/var/lib/pgsql/data'
backend_flag1 = 'ALLOW_TO_FAILOVER'

# - Authentication -
enable_pool_hba = on
pool_passwd = 'pool_passwd'

# FILE LOCATIONS
pid_file_name = '/var/run/pgpool/pgpool.pid'

# REPLICATION MODE
replication_mode = off
load_balance_mode = on
master_slave_mode = on

sr_check_period = 5
sr_check_user = 'postgres'
sr_check_password = 'postgres'
sr_check_database = 'postgres'

# HEALTH CHECK
health_check_period = 10
health_check_timeout = 20
health_check_user = 'postgres'
health_check_password = 'postgres'
health_check_database = 'postgres'

# FAILOVER AND FAILBACK
failover_command = '/etc/pgpool-II/failover_stream.sh %d %H /tmp/trigger_file0'

# WATCHDOG
use_watchdog = on

wd_hostname = '192.168.19.227' #145
# - Virtual IP control Setting -

delegate_IP = ''
if_cmd_path = '/sbin'
if_up_cmd = 'ifconfig eth0:0 inet $_IP_$ netmask 255.255.255.0'
if_down_cmd = 'ifconfig eth0:0 down'

heartbeat_destination0 = 'slave1'
heartbeat_device0 = 'eth0'

other_pgpool_hostname0 = 'slave'

[root@mysql3 pgpool-II]# vim failover_stream.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=192.168.19.227
new_master=192.168.19.145
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;

[root@mysql3 pgpool-II]# chown postgres:postgres failover_stream.sh &&chmod 777 failover_stream.sh
[root@mysql3 pgpool-II]# mkdir /var/log/pgpool
[root@mysql3 pgpool-II]# chown -R postgres.postgres /var/log/pgpool
[root@mysql3 pgpool-II]# pgpool -n -d -D > /var/log/pgpool/pgpool.log 2>&1 & #启动pgpool
[1] 29885

su - postgres

错误1 
-bash-4.2$ psql -h 192.168.19.227 -p 9999
psql: 无法联接到服务器: 拒绝连接
服务器是否在主机 "192.168.19.227" 上运行并且准备接受在端口
9999 上的 TCP/IP 联接?

#这里只有一个pgpool所以
2018-12-24 15:01:29: pid 29885: FATAL: invalid watchdog configuration. other pgpools setting is not defined

use_watchdog = off

#健康检查出错
2018-12-24 15:05:16: pid 29939: DEBUG: doing health check against database:postgres user:postgres
2018-12-24 15:05:16: pid 29939: DEBUG: Backend DB node 0 status is 3
2018-12-24 15:05:16: pid 29939: DEBUG: Backend DB node 1 status is 3
2018-12-24 15:05:16: pid 29939: DEBUG: health check: clearing alarm
2018-12-24 15:05:16: pid 29939: DEBUG: health check: clearing alarm

注释掉健康检查的几个参数

[root@mysql3 pgpool-II]# pgpool reload  #重新加载
[root@mysql3 pgpool-II]# ps -ef|grep pgpool
[root@mysql3 pgpool-II]# netstat -lnt|grep 9999

错误2 

#pg的错误日志
-bash-4.2$ tail -f -n 100 postgresql-Mon.log
STATEMENT: show user;
FATAL: no pg_hba.conf entry for host "192.168.19.227", user "postgres", database "postgres", SSL off

#master slave都更新此文件,然后reload
-bash-4.2$ vim pg_hba.conf
host all all 192.168.19.227/32 md5

 

#登录 master
-bash-4.2$ psql -h 192.168.19.227 -p 5432
口令:
psql (9.5.2)
输入 "help" 来获取帮助信息.

postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
----------------+------------
192.168.19.145 | async
postgres=# \q
-bash-4.2$ psql -h 192.168.19.227 -p 5432 -U postgres
用户 postgres 的口令:
psql (9.5.2)
输入 "help" 来获取帮助信息.

错误3

-bash-4.2$ psql -h 192.168.19.227 -p 9999 -U postgres
psql: 服务器意外地关闭了联接
这种现象通常意味着服务器在处理请求之前
或者正在处理请求的时候意外中止
-bash-4.2$ psql -h 192.168.19.227 -p 9999 -U postgres -d postgres
psql: 服务器意外地关闭了联接
这种现象通常意味着服务器在处理请求之前
或者正在处理请求的时候意外中止
2018-12-24 15:38:37: pid 30728: DEBUG: reading startup packet
2018-12-24 15:38:37: pid 30728: DETAIL: Protocol Major: 1234 Minor: 5679 database: user:
2018-12-24 15:38:37: pid 30728: DEBUG: forwarding error message to frontend
2018-12-24 15:38:37: pid 30728: FATAL: pgpool is not accepting any new connections
2018-12-24 15:38:37: pid 30728: DETAIL: all backend nodes are down, pgpool requires at least one valid node
2018-12-24 15:38:37: pid 30728: HINT: repair the backend nodes and restart pgpool
2018-12-24 15:38:37: pid 29939: DEBUG: reaper handler
2018-12-24 15:38:37: pid 29939: LOG: child process with pid: 30728 exits with status 256
2018-12-24 15:38:37: pid 29939: LOG: fork a new child process with pid: 30766
2018-12-24 15:38:37: pid 29939: DEBUG: reaper handler: exiting normally
2018-12-24 15:38:37: pid 30766: DEBUG: initializing backend status

-bash-4.2$ psql -p 9999 -U postgres -d postgres
psql: ERROR: pgpool is not accepting any new connections
描述: all backend nodes are down, pgpool requires at least one valid node
提示: repair the backend nodes and restart pgpool
-bash-4.2$ psql -h 127.0.0.1 -p 9999 -U postgres -d postgres
psql: 服务器意外地关闭了联接
这种现象通常意味着服务器在处理请求之前
或者正在处理请求的时候意外中止

#这里stop 然后重启pgpool,查看到完整的启动日志,有重大发现

2018-12-24 15:51:50: pid 31010: DEBUG: loading hba configuration
2018-12-24 15:51:50: pid 31010: DETAIL: loading file :"/etc/pgpool-II/pool_hba.conf" for client authentication configuration file
2018-12-24 15:51:50: pid 31010: LOG: Backend status file /var/log/pgpool/pgpool_status discarded
2018-12-24 15:51:50: pid 31010: DEBUG: pool_coninfo_size: num_init_children (32) * max_pool (4) * MAX_NUM_BACKENDS (128) * sizeof(ConnectionInfo) (136) = 2228224 bytes requested for shared memory
2018-12-24 15:51:50: pid 31010: DEBUG: ProcessInfo: num_init_children (32) * sizeof(ProcessInfo) (32) = 1024 bytes requested for shared memory
2018-12-24 15:51:50: pid 31010: DEBUG: Request info are: sizeof(POOL_REQUEST_INFO) 5304 bytes requested for shared memory
2018-12-24 15:51:50: pid 31010: DEBUG: Recovery management area: sizeof(int) 4 bytes requested for shared memory
2018-12-24 15:51:50: pid 31010: LOG: Setting up socket for 0.0.0.0:9999
2018-12-24 15:51:50: pid 31010: LOG: Setting up socket for :::9999
2018-12-24 15:51:50: pid 31011: DEBUG: initializing backend status
2018-12-24 15:51:50: pid 31012: DEBUG: initializing backend status

2018-12-24 15:51:50: pid 31041: DEBUG: initializing backend status
2018-12-24 15:51:50: pid 31010: LOG: pgpool-II successfully started. version 3.6.2 (subaruboshi)
2018-12-24 15:51:50: pid 31010: DEBUG: find_primary_node: not in streaming replication mode
2018-12-24 15:51:50: pid 31042: DEBUG: initializing backend status
2018-12-24 15:51:50: pid 31043: DEBUG: I am PCP child with pid:31043
2018-12-24 15:51:50: pid 31016: DEBUG: initializing backend status
2018-12-24 15:51:50: pid 31044: DEBUG: I am 31044
2018-12-24 15:51:50: pid 31044: DEBUG: initializing backend status

[root@mysql3 pgpool-II]# vim pgpool.conf
master_slave_sub_mode = 'stream'
[root@mysql3 pgpool-II]# pgpool reload

错误4

-bash-4.2$ psql -U postgres -d postgres -h 192.168.19.227 -p 9999
psql: FATAL: client authentication failed
描述: no pool_hba.conf entry for host "192.168.19.227", user "postgres", database "postgres", SSL off
提示: see pgpool log for details

[root@mysql3 pgpool-II]# vim pool_hba.conf #添加一行
host all all 192.168.19.227/32 md5

错误5
-bash-4.2$ psql -U postgres -d postgres -h 192.168.19.227 -p 9999
psql: FATAL: md5 authentication failed
描述: pool_passwd file does not contain an entry for "postgres"

[root@mysql3 pgpool-II]# cat pool_passwd
[root@mysql3 pgpool-II]# pg_md5 -m -p -u postgres pool_passwd
password:
[root@mysql3 pgpool-II]# cat pool_passwd #此文件
postgres:md53175bce1d3201d16594cebf9d7eb3f9d
[root@mysql3 pgpool-II]# pgpool reload
-bash-4.2$ psql -U postgres -d postgres -h 192.168.19.227 -p 9999 #正常登录
用户 postgres 的口令:
psql (9.5.2)
postgres=# show pool_nodes;
node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay
---------+----------------+------+--------+-----------+--------+------------+-------------------+-------------------
0 | 192.168.19.227 | 5432 | up | 0.500000 | master | 0 | false | 0
1 | 192.168.19.145 | 5432 | up | 0.500000 | slave | 0 | true | 0

hq=# \c test1_user
您现在已经连接到数据库 "test1_user",用户 "postgres".
test1_user=# \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+---------------+--------+------------
public | test1_user111 | 数据表 | test1_user
public | testcase | 数据表 | test1_user
(2 行记录)

test1_user=# select * from public.testcase;
id | task_class | age
----+------------+-----
(0 行记录)
test1_user=# insert into public.testcase(id,task_class,age) values(1,1,18); #插入记录
INSERT 0 1
test1_user=# select * from public.testcase;
id | task_class | age
----+------------+-----
1 | 1 | 18

#连接master库,查询刚刚数据
-bash-4.2$ psql -p 5432
psql (9.5.2)
输入 "help" 来获取帮助信息.

postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
------------+------------+----------+-------------+-------------+---------------------------
exampledb | dbuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/dbuser +
| | | | | dbuser=CTc/dbuser
hq | dbuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
mydb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
mypg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
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
test1_user | test1_user | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/test1_user +
| | | | | test1_user=CTc/test1_user
test2_user | test1_user | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
yhq1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(10 行记录)

postgres=# \c test1_user;
您现在已经连接到数据库 "test1_user",用户 "postgres".
test1_user=# \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+---------------+--------+------------
public | test1_user111 | 数据表 | test1_user
public | testcase | 数据表 | test1_user
(2 行记录)

test1_user=# select * from public.testcase;
id | task_class | age
----+------------+-----
1 | 1 | 18
(1 行记录)

#登录slave,查询刚刚的数据
-bash-4.2$ psql -p 5432
psql (9.5.2)
Type "help" for help.

postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+------------+----------+-------------+-------------+---------------------------
exampledb | dbuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/dbuser +
| | | | | dbuser=CTc/dbuser
hq | dbuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
mydb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
mypg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
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
test1_user | test1_user | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/test1_user +
| | | | | test1_user=CTc/test1_user
test2_user | test1_user | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
yhq1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(10 rows)

postgres=# \c test1_user
You are now connected to database "test1_user" as user "postgres".
test1_user=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+------------
public | test1_user111 | table | test1_user
public | testcase | table | test1_user
(2 rows)

test1_user=# select * from public.testcase;
id | task_class | age
----+------------+-----
1 | 1 | 18
(1 row)

这里只搭建了pgpool的集群环境,没有验证主从切换的情况(切换脚本待验证

 

posted @ 2018-12-24 17:19  春困秋乏夏打盹  阅读(6005)  评论(0编辑  收藏  举报