一.pgpool-II3.6安装配置:(流复制下的:主备模式)
1.安装依赖包:
前提:是先配置好postgreSQL数据库:
yum install gcc-c++ gcc flex readline-devel zlib-devel zlib
无论是备库还是主库,都需要安装pgpool-II;因为需要安装C函数!
useradd postgres
groupadd postgres
-----------------------------------------------------------
su root
mkdir /home/postgres/pgdata
mkdir /pgdata
mkdir -p /www/logs
mkdir -p /usr/local/pgpool-II-3.6.1/run
chown -R postgres:postgres /usr/local/pgpool-II-3.6.1
chown -R postgres:postgres /www
chown -R postgres:postgres /pgdata
chown postgres:postgres /home/postgres/pgdata
1.1基本简介:
pgpool有很多功能,其中最重要的我觉得是如下几个:
提供连接池(负载均衡模式),
复制模式(能通过pgpool分发sql,因此是基于sql语句的分发复制),
主备模式
(依赖其他的复制,如snoly和流复制,但pgpool能把客户端的sql请求根据sql是查询还是修改发送到备库或主库),
并行模式(其实就是把表水平拆分到各个数据节点,一条sql查询时需要从多个数据节点查询数据),
-----------------------------------------------
本文是用主备模式来搭建pg的高可用和负载均衡集群。
要搭建基于postgresql的高可用、负载均衡的数据库集群架构,还有很多技术,
如postgres-xc,
但本文以最常用的postgresql的主备模式(主库加流复制为例来搭建,1主库+多备库,实现高可用和负载均衡)。
高可用即一个节点宕机不影响整体业务运行,
-----------------------------------------------
负载均衡是指客户端发过来的链接请求能均匀的分布到各个数据节点,
负载均衡的时候需要考虑到主库和备库是不同的,主库可读可写而备库只能读,
-----------------------------------------------
因此select语句可以发往主库和备库,而update、insert、delete等要在主库执行,
别的负载均衡软件如lvs是做不到的,但pgpool可以检测sql语句,自动发往不同的节点。
-----------------------------------------------
本文用pgpool-ii来实现高可用和读写分离的负载均衡。
2.SCP网络复制,网络文件传输
下载:
scp 用户名@ip:路径/文件 本地路径
scp -r root@10.29.80.117:/soft/pgpool-II-3.6.1.tar.gz /soft
scp -r root@10.29.80.117:/soft /soft 下载目录
二.(192.168.1.109)开始安装:pgpool-II-3.6(以postgres用户身份安装!谨记)
1.基本说明:
(1).主库(192.168.1.115)
需安装:postgreSQL-9.6.1 和 pgpool-II-3.6.1中的C函数:
账号:root
密码:root
---------------------------------------------------------------------
(2).备库(192.168.1.116)
需安装:postgreSQL-9.6.1 和 pgpool-II-3.6.1中的C函数:
账号:root
密码:root
---------------------------------------------------------------------
(3).pgpool-II-3.6.1 (192.168.1.109)
需安装:pgpool-II-3.6.1:
账号:root
密码:root
2.解压并安装:
./configure \
--prefix=/usr/local/pgpool-II-3.6.1/ \
--with-pgsql=/usr/local/postgreSQL-9.6.1 \
--with-openssl \
make
make install
-------------------------------------
configure 脚本收集你的系统信息并用于编译过程。
你可以通过传递命令行参数到 configure 脚本来改变它的默认行为,
例如安装目录。默认情况下,pgpool-II将安装到 /usr/local 目录。
make 命令编译源码,make install 命令将安装执行文件。你必须对安装目录有写入权限。
-------------------------------------
vim ~/.bash_profile
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/pgpool-II-3.6.1/bin
export PATH
-------------------------------------
vim /home/postgres/.bash_profile
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgpool-II-3.6.1/bin
export PATH
-------------------------------------
source ~/.bash_profile
source /home/postgres/.bash_profile
3.配置文件复制:
cd /usr/local/pgpool-II-3.6.1/etc
cp pcp.conf.sample pcp.conf
cp pgpool.conf.sample-stream pgpool.conf
cp pool_hba.conf.sample pool_hba.conf
3.1 pcp.conf配置:
# USERID:MD5PASSWD MD5加密: 123456
postgres:e10adc3949ba59abbe56e057f20f883e
3.2 pgpool.conf配置:
#pid_file_name = '/usr/local/pgpool-II-3.6.1/run/pgpool.pid'
mkdir -p /usr/local/pgpool-II-3.6.1/run/
#------------------------------------------------------------------------------
# CONNECTIONS
#------------------------------------------------------------------------------
# - pgpool Connection Settings -
listen_addresses = '*'
# Host name or IP address to listen on:
# '*' for all, '' for no TCP/IP connections
# (change requires restart)
port = 9999
# Port number
# (change requires restart)
socket_dir = '/tmp'
# Unix domain socket path
# The Debian package defaults to
# /var/run/postgresql
# (change requires restart)
# - pgpool Communication Manager Connection Settings -
pcp_listen_addresses = '*'
# Host name or IP address for pcp process to listen on:
# '*' for all, '' for no TCP/IP connections
# (change requires restart)
pcp_port = 9898
# Port number for pcp
# (change requires restart)
pcp_socket_dir = '/tmp'
# Unix domain socket path for pcp
# The Debian package defaults to
# /var/run/postgresql
# (change requires restart)
listen_backlog_multiplier = 2
# Set the backlog parameter of listen(2) to
# num_init_children * listen_backlog_multiplier.
# (change requires restart)
serialize_accept = off
# whether to serialize accept() call to avoid thundering herd problem
# (change requires restart)
# - Backend Connection Settings -
# Host name or IP address to connect to for backend 0
backend_hostname0 = '192.168.1.115'
# Port number for backend 0
backend_port0 = 5432
# Weight for backend 0 (only in load balancing mode)
backend_weight0 = 1
# Data directory for backend 0
backend_data_directory0 = '/pgdata'
# Controls various backend behavior
# ALLOW_TO_FAILOVER or DISALLOW_TO_FAILOVER
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_hostname1 = '192.168.1.116'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/pgdata'
backend_flag1 = 'ALLOW_TO_FAILOVER'
-------------------------------------------------------------------------------
enable_pool_hba = on
pool_passwd = 'pool_passwd'
pid_file_name = '/usr/local/pgpool-II-3.6.1/run/pgpool.pid'
logdir = '/tmp'
#要开启负载均衡,需要设置:开启
load_balance_mode = on
replication_mode = off
#要设置主备模式,需要设置:开启
master_slave_mode = on
master_slave_sub_mode = 'stream'
#流复制的贮备模式:开启
sr_check_period = 5
sr_check_user = 'postgres'
sr_check_password = '123456'
3.3 pool_hba.conf配置:
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 trust
host all all ::1/128 trust
三.192.168.1.115(主库) 和192.168.1.116(备库) 配置:
前提是:这两台服务器:已经配置好postgreSQL-9.6.1; 并且已经安装上pgpool-II-3.6.1中的C函数:
cd /www/pgpool-II-3.6.1/src
make
make install
1.在192.168.1.115主库的配置:
1.1 在192.168.1.115(主库)中:postgresql.conf的配置文件:
listen_addresses = '*'
wal_level = hot_standby
max_wal_senders = 10
hot_standby = on
1.2 在$HOME目录中:创建密码文件:.pgpass
# 先确定下当前用户的$HOME地址:
$echo $HOME
.pgpass
192.168.1.116:5432:replication:postgres:123456
2.在192.168.1.116副库的配置:
2.1 在$HOME目录中:创建密码文件:.pgpass
# 先确定下当前用户的$HOME地址:
$echo $HOME
.pgpass
192.168.1.115:5432:replication:postgres:123456
2.2从主库上备份至192.168.1.116上一份备库:
chmod -R 0700 /home/postgres/pgdata
# 通过pg_basebackup -x -h 192.168.1.115 -P -Upostgres -D /home/postgres/pgdata
2.3在192.168.1.116上创建一个recovery.conf文件,内容如下:
/home/postgres/pgdata
standby_mode = 'on'
primary_conninfo = 'user=postgres application_name=192.168.1.116 host=192.168.1.115 port=5432 sslmode=disable sslcompression=1'
3.主库和备库上的lpostgresql的pg_hba.conf配置文件:(必须配置)
/mnt/data/pg_hba.conf
----------------------
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege. 必须开启:取消注释:谨记
#local replication postgres trust
host replication postgres 127.0.0.1/32 trust
host replication postgres 0.0.0.0/0 trust
host replication postgres ::1/128 trust
4.至此启动:192.168.1.116备库:
vim ~/.bash_profile /home/postgres/.bash_profile
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/postgreSQL-9.6.1/bin
export PATH
---------------------------------------------------------------------
pg_ctl -D /home/postgres/pgdata start 启动
pg_ctl -D /home/postgres/pgdata stop 关闭
5.在192.168.1.116上测试贮备模式的pgpool-II
192.168.1.115上:
systemctl start postgresql
192.168.1.109上:
pgpool -f /usr/local/pgpool-II-3.6.1/etc/pgpool.conf
192.168.1.116上:
pg_ctl -D /home/postgres/pgdata start
psql -h 192.168.1.109 -p 9999 -Upostgres -d postgres
三.pgpool-II3.6启动:
启动: pgpool
停止: pgpool stop
pgpool -n -d > /www/logs/pgpool.log 2>&1 &
-----------------------------------------------
pgpool -f /usr/local/pgpool-II-3.6.1/etc/pgpool.conf
psql -h 192.168.1.109 -p 9999 -Upostgres -d postgres