pgxl9.5安装部署

来源:互联网 发布:windows xp自带壁纸 编辑:程序博客网 时间:2024/06/06 04:02

一 Postgres-XL简介

Postgres的-XL是一个基于PostgreSQL数据库的横向扩展开源SQL数据库集群,具有足够的灵活性来处理不同的数据库工作负载:
- 完全ACID,保持事务一致性
- OLTP 写频繁的业务
- 需要MPP并行性商业智能/大数据分析
- 操作数据存储
- Key-value 存储
- GIS的地理空间
- 混合业务工作环境
- 多租户服务提供商托管环境
- Web 2.0
Postgres-XL架构
这里写图片描述

二 组件简介

Global Transaction Monitor (GTM)
全局事务管理器,确保群集范围内的事务一致性。 GTM负责发放事务ID和快照作为其多版本并发控制的一部分。
集群可选地配置一个备用GTM,以改进可用性。此外,可以在协调器间配置代理GTM, 可用于改善可扩展性,减少GTM的通信量。

GTM Standby
GTM的备节点,在pgxc,pgxl中,GTM控制所有的全局事务分配,如果出现问题,就会导致整个集群不可用,为了增加可用性,增加该备用节点。当GTM出现问题时,GTM Standby可以升级为GTM,保证集群正常工作。

GTM-Proxy
GTM需要与所有的Coordinators通信,为了降低压力,可以在每个Coordinator机器上部署一个GTM-Proxy。

Coordinator
协调员管理用户会话,并与GTM和数据节点进行交互。协调员解析,并计划查询,并给语句中的每一个组件发送下一个序列化的全局性计划。
为节省机器,通常此服务和数据节点部署在一起。

Data Node
数据节点是数据实际存储的地方。数据的分布可以由DBA来配置。为了提高可用性,可以配置数据节点的热备以便进行故障转移准备。

总结:gtm是负责ACID的,保证分布式数据库全局事务一致性。得益于此,就算数据节点是分布的,但是你在主节点操作增删改查事务时,就如同只操作一个数据库一样简单。Coordinator是调度的,将操作指令发送到各个数据节点。datanodes是数据节点,分布式存储数据。

三、pgxl安装配置

3.1主机规划
三台主机

主机名 IP 角色 端口 nodename 数据目录


主机名称 ip 角色 端口 nodename 数据目录 pg1 192.168.0.125 GTM 6666 gtm DATA/gtm pg2 192.168.0.127 Coordinator、Datanode 5432、5433 coord1、dn1 DATA/coord、DATA/dn pg3 192.168.0.128 Coordinator、Datanode 5432、5433 coord2、dn2 DATA/coord、DATA/dn

每台主机host映射配置

192.168.0.125 pg1192.168.0.126 pg2192.168.0.127 pg3

pg1上部署gtm,Coordinator与Datanode节点一般部署在同一台机器上。实际上,GTM-proxy,Coordinator与Datanode节点一般都在同一个机器上,使用时避免端口号与连接池端口号重叠!规划pg2,pg3作为协调节点与数据节点。

3.2 系统环境设置
关闭防火墙(centos7):
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service

selinux设置:

[root@localhost ~]#vim /etc/selinux/config

设置SELINUX=disabled,保存退出。

SELINUX=disabled

安装依赖包:

[root@localhost ~]# yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl 

3.3 新建用户
每个节点都建立用户postgres,并且建立.ssh目录,并配置相应的权限:

[root@localhost ~]# useradd postgres[root@localhost ~]# passwd postgres[root@localhost ~]# su - postgres[root@localhost ~]# mkdir ~/.ssh[root@localhost ~]# chmod 700 ~/.ssh

3.4 ssh免密码登录
仅仅在pg1节点配置,pg2,pg3不执行如下操作:

[root@localhost ~]# su - postgres[postgres@localhost ~]# ssh-keygen -t rsa[postgres@localhost ~]# cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys[postgres@localhost ~]# chmod 600 ~/.ssh/authorized_keys

将刚生成的认证文件拷贝到pg2到pg3中,使得pg1可以免密码登录pg2~pg3的任意一个节点:

[postgres@localhost ~]# scp ~/.ssh/authorized_keys postgres@pg2:~/.ssh/[postgres@localhost ~]# scp ~/.ssh/authorized_keys postgres@pg3:~/.ssh/

3.5 Postgres-XL安装
pg1-pg3每个节点都需安装配置。切换回root用户下,执行如下步骤安装

[root@localhost ~]# tar -zxvf postgres-xl-9.5r1.3.tar.gz[root@localhost ~]# cd postgres-xl-9.5r1.3[root@localhost ~postgres-xl-9.5r1.3]# ./configure --prefix=/home/postgres/pgxl9.5/[root@localhost ~postgres-xl-9.5r1.3]# make[root@localhost ~postgres-xl-9.5r1.3]# make install[root@localhost ~postgres-xl-9.5r1.3]# cd contrib/  [root@localhost ~contrib]# make[root@localhost ~contrib]# make install

cortrib中有很多postgres很牛的工具,一般要装上。如ltree,uuid,postgres_fdw等等。
3.6 配置环境变量
进入postgres用户,修改其环境变量,开始编辑

[root@localhost ~]#su - postgres[postgres@localhost ~]#vim .bashrc

在打开的文件末尾,新增如下变量配置:

export PGHOME=/home/postgres/pgxl9.5export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATHexport PATH=$PGHOME/bin:$PATH

按住esc,然后输入:wq!保存退出。输入以下命令对更改重启生效。

[root@localhost ~]# source .bashrc#输入以下语句,如果输出变量结果,代表生效[root@localhost ~]# echo $PGHOME#应该输出/home/postgres/pgxl9.5代表生效

如上操作,除特别强调以外,是pg1-pg3节点都要配置安装的。
四 Postgres-XL组件配置
4.1 创建节点数据目录
pg1-pg3每个节点创建DATA如下:

[root@localhost~]# cd /home/postgres[root@postgres~]# mkdir DATA[root@localhost~]# chown -R postgres.postgres /home/postgres

4.2 初始化GTM

在pg1机器上,切换到postgres用户下执行初始化:

[root@localhost ~]# su - postgres[postgres@localhost ~]# initgtm -Z gtm -D /home/postgres/DATA/gtm

打开编辑gtm.conf文件设置如下:[postgres@localhost ~]# vim /home/postgres/DATA/gtm/gtm.conf

nodename = 'gtm'   #节点名称,任意指定,不能与其他节点重复listen_addresses = '*' #GTM监听的ip地址,*代表监听所有的集群ipport =6666 #gtm监控的端口号startup = ACT #act代表gtm是主库,如果是standy,设置为'STANDBY'

4.3 初始化GTM Proxy

于pg2上执行以下命令:

[root@localhost ~]#  su - postgres[root@localhost ~]#  initgtm -Z gtm_proxy -D /home/postgres/DATA/gtm_proxy#pg3上起名字叫gtm_proxy2

修改gtm_proxy.conf文件配置如下:

nodename='gtm_proxy1'port=6666gtm_host='pg1'gtm_port=6666

pg3上设置同理如上。

4.3 初始化Coordinators
在pg2上执行如下命令:

initdb -D /home/postgres/DATA/coord --nodename coord1 -E UTF8 --locale=C -U postgres -W #pg3上节点名称执行这句时写coord2

修改postgresql.conf与pg_hba.conf文件配置如下:

[postgres@localhost ~]# vim /home/postgres/DATA/coord/postgresql.conf
# - Connection Settings -listen_addresses = '*'port = 5432max_connections = 100# DATA NODES AND CONNECTION POOLING#----------------------------------pooler_port = 6667      max_pool_size = 100# GTM CONNECTION#--------------------------gtm_host = 'pg1'   #gtm所在的主机地址gtm_port = 6666    #gtm配置中,gtm端口号配置为6666pgxc_node_name = 'coord1'

设置pg_hba.conf

[postgres@localhost ~]# vim /home/postgres/DATA/coord/pg_hba.conf
# "local" is for Unix domain socket connections onlylocal   all             all                                     trust#信任访问主机# IPv4 local connections:host    all             all             0.0.0.0/0            trust# IPv6 local connections:host    all             all             ::1/128                 trust

在pg2中生成coord2,并配置,步骤如上。注意节点名称都要将coord1换成当下的coord2。

4.4初始化Datanode
pg2中初始化dn1,要在postgres用户下执行。

[root@localhost ~]#  su - postgres[postgres@localhost ~]#  initdb -D /home/postgres/DATA/dn --nodename dn1 -E UTF8 --locale=C -U postgres -W#pg3中是dn2

配置postgresql.conf,pg_hba.conf:

[postgres@localhost ~]#  vim /home/postgres/DATA/dn/postgresql.conf
CONNECTIONS AND AUTHENTICATION#------------------------------------listen_addresses = '*'port =5433max_connections = 100# DATA NODES AND CONNECTION POOLING#----------------------------------------------pooler_port = 6668 # 同一台机器要使用不同的连接池端口,如coord1与dn1部署同一个机器,coord1是6667,dn1是6668max_pool_size = 100# GTM CONNECTION#-----------------------------gtm_host = 'pg1' gtm_port = 6666 #gtm端口号pgxc_node_name = 'dn1'
[postgres@localhost ~]# vim /home/postgres/DATA/dn/pg_hba.conf
# "local" is for Unix domain socket connections onlylocal   all             all                                     trust# IPv4 local connections:host    all             all             0.0.0.0/0            trust# IPv6 local connections:host    all             all             ::1/128                 trust

配置完dn1,同理配置dn2,注意dn1替换成dn2。

到这里我们基本上完成了大部分工作,接下来就是启动和注册了
4.5 启动Postgres-XL集群
启动顺序是GTM-GTM Standby-GTM-Proxy-Datanodes-Coordinators。

4.5.1 启动gtm
在pg1机器上,在postgres用户下启动gtm

[postgres@localhost ~]# gtm_ctl start -Z gtm -D /home/postgres/DATA/gtm

4.5.2 启动gtm-proxy

在pg2机器上,启动gtm-proxy:

[postgres@localhost ~]# gtm_ctl start -Z gtm_proxy -D /home/postgres/DATA/gtm_proxy

同理启动pg3上的gtm-proxy。

4.5.3 启动datanode

在pg2上启动dn1示范如下:

[postgres@localhost ~]# pg_ctl start -Z datanode -D /home/postgres/DATA/dn

同理启动pg3上的dn。
4.5.4 启动coordinator

在pg2机器上,启动coordinator节点coord:

[postgres@localhost ~]# pg_ctl start -Z coordinator -D /home/postgres/DATA/coord

同理启动pg3上的coord。

六 Postgres-XL集群配置与测试

6.1 集群配置

在pg2以postgres用户下进入psql。coord1配置端口号是5432,直接以psql加端口号进入配置。

[postgres@localhost]$ psql -p 5432psql (PGXL 9.5r1.3, based on PG 9.5.4 (Postgres-XL 9.5r1.3))Type "help" for help.postgres=# select * from pgxc_node;postgres=# alter node coord1 with (type=coordinator,host='pg2', port=5432);postgres=# create node coord2 with (type=coordinator,host='pg3', port=5432);postgres=# create node dn1 with (type=datanode, host='pg2',port=5433,primary,preferred);postgres=# create node dn2 with (type=datanode, host='pg3',port=5433);postgres=#select pgxc_pool_reload();postgres=# select * from pgxc_node;

配置pg2上的dn1如下,以当初配置的5433端口号进入psql:

[postgres@localhost]$ psql -p 5433psql (PGXL 9.5r1.3, based on PG 9.5.4 (Postgres-XL 9.5r1.3))Type "help" for help.postgres=# select * from pgxc_node;postgres=# create node coord1 with (type=coordinator,host='pg2', port=5432);postgres=# create node coord2 with (type=coordinator,host='pg3', port=5432);postgres=# alter node dn1 with (type=datanode, host='pg2',port=5433,primary,preferred);postgres=# create node dn2 with (type=datanode, host='pg3',port=5433);postgres=#select pgxc_pool_reload();postgres=# select * from pgxc_node;

pg3同理

6.2 创建测试表与数据
只能在pg1中coord1,coord2节点中操作,pg2,pg3数据节点都是只读的,所以我们选择coord1(端口号为5432)中创建测试数据。

[postgres@localhost]$ psql -p 5432psql (PGXL 9.5r1.3, based on PG 9.5.4 (Postgres-XL 9.5r1.3))Type "help" for help.postgres=#  create table test1(id integer,name varchar(20));postgres=#  insert into test1(id,name) values(1,'测试');postgres=#  insert into test1(id,name) values(2,'测试');postgres=#  insert into test1(id,name) values(3,'测试');postgres=#  insert into test1(id,name) values(4,'测试');postgres=#  insert into test1(id,name) values(5,'测试');postgres=#  insert into test1(id,name) values(6,'测试');postgres=#  insert into test1(id,name) values(7,'测试');postgres=#  insert into test1(id,name) values(8,'测试');

通过我的本机测试发现,在coord2,dn1,dn2节点中都已创建了一个test1表。且coord1=coord2,而dn1中test1表有5条数据,dn2有3条。
总结:coord1,coord2这种协调处理节点,是操作数据库的主节点,拥有完整的数据视图。
dn1到dn2是数据节点,只读的,存储数据,由coordinator协调分配存储到哪个节点,所以不同节点分配到的数据数量不一。(一开始我以为错了,原来我理解错了分布式)。

注意:由于所有的数据节点组成了完整的数据视图,所以一个数据节点down机,整个pgxl都启动不了了,所以实际生产中,为了提高可用性,一定要配置数据节点的热备以便进行故障转移准备。

遇到的问题

postgres=# create table test1(id integer,name varchar(20));LOG:  failed to connect to node, connection string (host=192.168.0.125 port=1925 dbname=postgres user=postgres application_name=pgxc sslmode=disable options='-c remotetype=coordinator -c parentnode=coord1  -c DateStyle=iso,mdy -c timezone=prc -c geqo=on -c intervalstyle=postgres -c lc_monetary=C'), connection error (fe_sendauth: no password supplied        )WARNING:  can not connect to node 16384WARNING:  Health map updated to reflect DOWN node (16384)LOG:  Pooler could not open a connection to node 16384LOG:  failed to acquire connectionsSTATEMENT:  create table test1(id integer,name varchar(20));ERROR:  Failed to get pooled connectionsHINT:  This may happen because one or more nodes are currently unreachable, either because of node or network failure.         Its also possible that the target node may have hit the connection limit or the pooler is configured with low connections.         Please check if all nodes are running fine and also review max_connections and max_pool_size configuration parametersSTATEMENT:  create table test1(id integer,name varchar(20));

我的问题是节点的监听配置没有被gtm监听到,希望各位注意。

注:本文借鉴http://blog.csdn.net/freeland1/article/details/52346669
至于集群初始化和启动,我测试没有完全成功,如果问题得到解决我会再补充进来。

0 0
原创粉丝点击