https://my.oschina.net/Suregogo/blog/464623
https://www.postgres-xl.org/download/
一 Postgres-XL简介
Postgres的-XL是一个基于PostgreSQL数据库的横向扩展开源SQL数据库集群,具有足够的灵活性来处理不同的数据库工作负载:
|
二 组件简介
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是数据节点,分布式存储数据。
三 Postgres-XL环境配置与安装
3.1 集群规划
准备三台Centos7服务器(或者虚拟机),集群规划如下:
主机名 | ip | 角色 | 端口 | nodename | 数据目录 |
gtm | 56.56.56.95 | Gtm | 6666 | one | /data/gtm |
gtm_standby | 56.56.56.96 | Gtm备库 | 9999 | two | /data/gtm_standby |
cd1 | 56.56.56.97 | Coordinator | 5432 | co1 | /data/coordinator |
Datanode | 5433 | dn1 | /data/pgdata | ||
Gtm Proxy | 6666 | gtmpoxy01 | /data/gtm_proxy | ||
cd2 | 56.56.56.98 | Coordinator | 5432 | co2 | /data/coordinator |
Datanode | 5433 | dn2 | /data/pgdata | ||
Gtm Proxy | 6666 | gtmpoxy02 | /data/gtm_proxy | ||
cd3 | 56.56.56.99 | Coordinator | 5432 | co3 | /data/coordinator |
Datanode | 5433 | dn3 | /data/pgdata | ||
Gtm Proxy | 6666 | gtmpoxy03 | /data/gtm_proxy |
6. 5台机器上都配置hosts
56.56.56.95 gtm
56.56.56.96 gtm_standby
56.56.56.97 cd1
56.56.56.98 cd2
56.56.56.99 cd3
|
yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl gcc |
创建用户
useradd pgxl
passwd pgxl
|
3.5 Postgres-XL安装
pg1-pg3每个节点都需安装配置。切换回root用户下,执行如下步骤安装
cd postgres-xl
./configure --prefix=/usr/local/pgxl
make
make install
|
这个是Postgres-XL的拓展包,提供了uuid,fdw等很有用的工具
cd contrib/
make
make install
|
3.6 配置环境变量
进入postgres用户,修改其环境变量,开始编辑
su - pgxl
vim .bash_profile
export PGHOME=/usr/local/pgxl
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
source .bash_profile
|
创建数据目录
7.95 和 GTM 机器 创建数据目录
mkdir -p /data/gtm
|
9696 GTM standby机器
mkdir -p /data/gtm_standby |
97 、98 、99 机器创建对应的数据目录
mkdir -p /data/coordinator
mkdir -p /data/pgdata
mkdir -p /data/gtm_proxy
mkdir -p /data/pgdata_slave |
8. 权限
95机器
chown -R pgxl:pgxl /data/gtm
chown -R pgxl:pgxl /usr/local/pgxl
|
96机器
chown -R pgxl:pgxl /data/gtm_standby
chown -R pgxl:pgxl /usr/local/pgxl
97 98 99
chown -R pgxl:pgxl /data/coordinator
chown -R pgxl:pgxl /data/pgdata
chown -R pgxl:pgxl /data/gtm_proxy
chown -R pgxl:pgxl /data/pgdata_slave
四 集群配置
10 . 初始化gtm
10.1 在56.56.56.95机器上初始化
su - pgxl
initgtm -Z gtm -D /data/gtm
|
10.2 编辑/data/gtm/gtm.conf文件
nodename = 'one' #节点名称,可以指定为任意名称,不能与其他节点名重复
listen_addresses = '*'# GTM监听的ip地址,"*" 表示在所有的ip地址上监听
port = 6666 # GTM监控的端口
startup = ACT #主库设置为 ACT,如果是 Standby,则设置为 STANDBY
|
11.初始化GTM的备库
96机器上初始化
su - pgxl
initgtm -Z gtm -D /data/gtm_standby
|
11.1 编辑 /data/gtm_standby/gtm.conf
nodename = 'two' # 节点名称
listen_addresses = '*' # active_host:指定连接GTM的IP地址
port = 6666 # active_port:指定连接GTM备库的端口
startup = STANDBY # startup: 因为是GTM的备库,所以要设置为STANDBY
active_host = 'gtm'
active_port = 6666 # GTM监控的端口
|
12. 初始化 gtm proxy
在56.56.56.97 98 99 三台机器上分别执行以下命令
su - pgxl
initgtm -Z gtm_proxy -D /data/gtm_proxy
|
13.在97机器配置文件:/data/gtm_proxy/gtm_proxy.conf
nodename = 'gtmproxy01'
listen_addresses = '*'
port = 6666
worker_threads = 1
gtm_host = 'gtm'
gtm_port = 6666
gtm_connect_retry_interval = 5 |
在98机器配置文件:/data/gtm_proxy/gtm_proxy.conf
nodename = 'gtmproxy02'
listen_addresses = '*'
port = 6666
worker_threads = 1
gtm_host = 'gtm'
gtm_port = 6666
gtm_connect_retry_interval = 5 |
在99机器配置文件 /data/gtm_proxy/gtm_proxy.conf
nodename = 'gtmproxy03'
listen_addresses = '*'
port = 6666
worker_threads = 1
gtm_host = 'gtm'
gtm_port = 6666
gtm_connect_retry_interval = 5 |
19.初始化Coordinator 和数据节点
在 97 、 98、 99 三台机器上分别执行以下命令
97
initdb --nodename co1 -D /data/coordinator
initdb --nodename dn1 -D /data/pgdata
98
initdb --nodename co2 -D /data/coordinator
initdb --nodename dn2 -D /data/pgdata
99
initdb --nodename co3 -D /data/coordinator
initdb --nodename dn3 -D /data/pgdata
|
20.修改3台机器的 coordinator配置文件/data/coordinator/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
port = 5432 # (change requires restart)
log_connections = on
gtm_host = 'gtm' # Host name or address of GTM
gtm_port = 6666 # Port of GTM
pgxc_node_name = 'co1~coN' # 根据实际的节点配置为co1、co2、... coN
pooler_port = 16667 |
21.修改3台机器的datanode的配置文件/data/pgdata/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
port = 5433 # (change requires restart)
log_connections = on
gtm_host = 'gtm' # Host name or address of GTM
gtm_port = 6666 # Port of GTM
pgxc_node_name = 'dn1~dnN' # 根据实际的节点配置为dn1、dn2、... dnN
|
注意:datanode 和 Coordinator 的三个节点都要修改
pg_hba.conf
host all all 0.0.0.0/0 trust |
22.启动集群
启动集群的顺序:
GTM GTM standy gtm-proxy datanodes coordinators |
22.1.启动95机器gtm
gtm_ctl -Z gtm start -D /data/gtm gtm_ctl -Z gtm status -D /data/gtm |
22.2.启动96机器standby
gtm_ctl -Z gtm start -D /data/gtm_standby gtm_ctl -Z gtm status -D /data/gtm_standby |
注意:要先启动 gtm 再启动gtm_standby,否则gtm standby会因为无法连接到gtm而失败
22.3 用下面的命令查看gtm和standby启动状态
gtm_ctl -Z gtm status -D /data/gtm
gtm_ctl -Z gtm_standby status -D /data/gtm_standby
|
22.4.在 97 98 99 三台机器上启动proxy
gtm_ctl -Z gtm_proxy start -D /data/gtm_proxy |
22.5 .在 97 98 99 三台机器上启动 datanodes
pg_ctl start -D /data/pgdata -Z datanode |
22.6. ps -ef|grep pgxc 查看启动信息
22.7.在 97 98 99 三台机器上启动coordinator
pg_ctl start -D /data/coordinator -Z coordinator |
23. coordinator 和datanode都要 注册集群节点信息,登录到 97 98 99 上执行
[pgxc@cd1 coordinator]$ psql -p 5432 postgres
LOG: connection received: host=[local]
LOG: connection authorized: user=pgxc database=postgres
psql (PGXC 1.0.4, based on PG 9.1.13)
Type "help" for help.
|
create node co1 with(type='coordinator',host='cd1',port=5432);
create node co2 with(type='coordinator',host='cd2',port=5432);
create node co3 with(type='coordinator',host='cd3',port=5432);
create node dn1 with(type='datanode',host='cd1',port=5433);
create node dn2 with(type='datanode',host='cd2',port=5433);
create node dn3 with(type='datanode',host='cd3',port=5433);
|
postgres=# select * from pgxc_node;
node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id
-----------+-----------+-----------+-----------+----------------+------------------+-------------
co3 | C | 5432 | localhost | f | f | -1046823559
co1 | C | 5432 | cd1 | f | f | 1344656819
co2 | C | 5432 | cd2 | f | f | 474101254
dn1 | D | 5433 | cd1 | f | f | -560021589
dn2 | D | 5433 | cd2 | f | f | 352366662
dn3 | D | 5433 | cd3 | f | f | -700122826
|
不管是修改还是删除重建完成后你都需要重新加载文件 select pgxc_pool_reload()
postgres=# select pgxc_pool_reload();
pgxc_pool_reload
------------------
t
(1 row)
|
24. 测试:
24.1. coordinator1上验证
[pgxl@cd1 coordinator]$ psql -p 5432 postgres
2018-03-14 16:24:12.857 CST [24179] LOG: connection received: host=[local]
2018-03-14 16:24:12.860 CST [24179] LOG: connection authorized: user=pgxl database=postgres
psql (PGXL 10alpha2, based on PG 10beta3 (Postgres-XL 10alpha2))
Type "help" for help.
postgres=# create table t (id int);
2018-03-14 16:24:19.341 CST [24181] LOG: connection received: host=56.56.56.97 port=58702
2018-03-14 16:24:19.342 CST [24181] LOG: connection authorized: user=pgxl database=postgres
CREATE TABLE
insert into t values(1);
insert into t values(2);
insert into t values(3);
insert into t values(4);
insert into t values(5);
insert into t values(6);
insert into t values(7);
insert into t values(8);
insert into t values(9);
insert into t values(10);
|
24.2. coordinator2 上验证
[pgxl@cd2 coordinator]$ psql -p 5432 postgres
2018-03-15 00:28:54.507 CST [22140] LOG: connection received: host=[local]
2018-03-15 00:28:54.510 CST [22140] LOG: connection authorized: user=pgxl database=postgres
psql (PGXL 10alpha2, based on PG 10beta3 (Postgres-XL 10alpha2))
Type "help" for help.
postgres=# select count(*) from t;
count
-------
10
(1 row) |
24.3. coordinator3上验证
[pgxl@cd3 coordinator]$ psql -p 5432 postgres
2018-03-15 00:28:43.477 CST [11719] LOG: connection received: host=[local]
2018-03-15 00:28:43.479 CST [11719] LOG: connection authorized: user=pgxl database=postgres
psql (PGXL 10alpha2, based on PG 10beta3 (Postgres-XL 10alpha2))
Type "help" for help.
postgres=# select count(*) from t;
count
-------
10
(1 row) |
24.4.数据节点1验证
[pgxl@cd1 coordinator]$ psql -p 5433 postgres
2018-03-14 16:33:56.598 CST [24362] LOG: connection received: host=[local]
2018-03-14 16:33:56.600 CST [24362] LOG: connection authorized: user=pgxl database=postgres
psql (PGXL 10alpha2, based on PG 10beta3 (Postgres-XL 10alpha2))
Type "help" for help.
postgres=# select count(*) from t;
count
-------
2
(1 row)
postgres=# select * from t;
id
----
3
5
(2 rows) |
24.5数据节点2
[pgxl@cd2 coordinator]$ psql -p 5433 postgres
2018-03-15 00:30:41.877 CST [22169] LOG: connection received: host=[local]
2018-03-15 00:30:41.879 CST [22169] LOG: connection authorized: user=pgxl database=postgres
psql (PGXL 10alpha2, based on PG 10beta3 (Postgres-XL 10alpha2))
Type "help" for help.
postgres=# select count(*) from t;
count
-------
3
(1 row)
postgres=# select * from t;
id
----
2
6
7
(3 rows) |
24.6. 数据节点3
[pgxl@cd3 coordinator]$ psql -p 5433 postgres
2018-03-15 00:30:32.094 CST [11747] LOG: connection received: host=[local]
2018-03-15 00:30:32.096 CST [11747] LOG: connection authorized: user=pgxl database=postgres
psql (PGXL 10alpha2, based on PG 10beta3 (Postgres-XL 10alpha2))
Type "help" for help.
postgres=# select count(*) from t;
count
-------
5
(1 row)
postgres=# select * from t;
id
----
1
4
8
9
10
(5 rows) |
按hash分片的,貌似没有什么规律