上篇博客 介绍了 PostgreSQL-XC 基本原理,对 PostgreSQL-XC 体系结构和组件有了初步认识,今天计划在虚拟机上安装 PostgreSQL-XC, 以做进一步学习分析,下面是 PostgreSQL-XC 安装的整个过程。
安装准备
1.1 requiremetns 硬件需求:官方建议使用 64 位的系统,内存至少 4GB 系统需求:64bit CentOS 5.4 或者其它其它 LINUX 系统 软件需求:GNU make version: 3.80 or newer Flex: 2.5.31 or later Perl: 5.8 or later GCC: Recent versions of GCC are recommendable
软件需求,具体可参考 http://postgres-xc.sourceforge.net/docs/1_0/install-requirements.html
1.2 实际安装环境 两台笔记本虚拟机 RHEL 6.2 ,内存 512 M IP 192.168.1.35 ;192.168.1.36
1.3 安装规划 一个GTM节点,两个协调(Coordinator)节点,二个数据节点;这是第一次安装,为了操作简便,没有安装GTM-Standby,节点和 GTM-Proxy,这两个节点可以后期测试。 192.168.1.35 GTM节点: gtm 6666 协调节点一: coord1 1921 协调节点二: coord1 1925
192.168.1.36 数据节点一: db_1 15431 数据节点二: db_2 15432
1.4 安装规划图
系统配置
两台主机上都操作。 2.1 创建 pgxc 系统用户并赋权
1 2 3 4 5 6 7 8 9
[root@redhatB bin] [root@redhatB bin] [root@redhatB bin] Changing password for user pgxc. New password: BAD PASSWORD: it is based on a dictionary word Retype new password: passwd: all authentication tokens updated successfully. [root@redhatB pgxc]
2.2系统层面其它配置 根据需要配置其它系统层参数,由于是在虚拟机上运行,其它系统参数暂不修改。
安装 Postgres-XC
两台主机上都安装 Postgres-XC 软件
3.1 pgxc_v1.0beta2 下载https://sourceforge.net/projects/postgres-xc/files/ 下载 pgxc_v1.0beta2.tar.gz
3.2 将安装包解压 ( 两台 )
1 2
cd /opt/soft_bak tar zxvf pgxc_v1.0 beta2.tar .gz
备注:解压后,产生目录 pgxc
3.3 编译配置
1
./configure --prefix=/opt/pgsql_xc --with-segsize=8 --with-wal-segsize=64 --with-wal-blocksize=64 --with-perl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
备注:conifugre 过程中如有提示缺少相关包,yum 安装即可。
3.4 编译 gmake 备注:编译后,如果出现”All of PostgreSQL successfully made. Ready to install.”,说明编译成功。
3.5 安装 PostgreSQL XC
备注:安装后如果提示 “PostgreSQL installation complete.” 说明安装成功。
配置数据节点
192.168.1.36 配置以下: 4.1 修改环境变量( .bash_profile )
1 2 3 4 5 6 7 8 9 10
export PGPORT =15431 export PGDATA =/pgdata_xc/db_1/pg_root export LANG =en_US.utf8export PGHOME =/opt/pgsql_xc export LD_LIBRARY_PATH =$PGHOME /lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib export DATE =`date +"%Y%m%d%H%M" ` export PATH =$PGHOME /bin:$PATH:. export MANPATH =$PGHOME /share/man:$MANPATH alias rm ='rm -i' alias ll ='ls -lh'
4.2 创建数据目录
1 2
pgxc@redhatB pgdata_xc ]$ mkdir -p /pgdata_xc /db_1/pg_root [pgxc@redhatB pgdata_xc ]$ mkdir -p /pgdata_xc /db_2/pg_root
4.3 初始化数据节点1
1
initdb -D /pgdata_xc/db_1/pg_root --nodename db_1 -E UTF8 --locale=C -U postgres -W
4.4 修改数据节点一配置文件 postgresql.conf 参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
port = 15431 log_destination = 'csvlog' logging_collector = on log_directory = 'pg_log' log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' log_truncate_on_rotation = on log_rotation_age = 1 d log_rotation_size = 10 MB gtm_host = 'localhost' gtm_host = '192.168.1.35' gtm_port = 6666 pgxc_node_name = 'db_1' pooler_port = 6667 max_pool_size = 100
4.5修改数据节点一 pg_hba.conf ,增加以下
1 2
host all all 192.168 .1 .35 /32 trust host all all 0.0 .0 .0 /0 md5
4.6 初始化数据节点2
1
initdb -D /pgdata_xc/db_2/pg_root --nodename db_2 -E UTF8 --locale=C -U postgres -W
4.7 修改数据节点二配置文件 postgresql.conf 参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
port = 15432 log_destination = 'csvlog' logging_collector = on log_directory = 'pg_log' log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' log_truncate_on_rotation = on log_rotation_age = 1 d log_rotation_size = 10 MB gtm_host = 'localhost' gtm_host = '192.168.1.35' gtm_port = 6666 pgxc_node_name = 'db_2' pooler_port = 6667 max_pool_size = 100
4.8 修改数据节点一 pg_hba.conf ,增加以下
1 2
host all all 192.168 .1 .35 /32 trust host all all 0.0 .0 .0 /0 md5
配置 GTM 节点
192.168.1.35 配置以下 5.1 修改 .bash_profile
1 2 3 4 5 6 7 8 9 10
export PGPORT =1921 export PGDATA =/database/1922/pgdata1/pgdata_xc/coord1 export LANG =en_US.utf8export PGHOME =/opt/pgsql_xcexport LD_LIBRARY_PATH =$PGHOME /lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib export DATE =`date +"%Y%m%d%H%M" ` export PATH =$PGHOME /bin:$PATH:. export MANPATH =$PGHOME /share/man:$MANPATH alias rm ='rm -i' alias ll ='ls -lh'
5.2 创建 gtm 数据目录
1
[pgxc@redhat 6 pgdata_xc]$ mkdir -p /database/ 1922 /pgdata1/ pgdata_xc/gtm
5.3 install gtm
1 2 3 4 5 6 7 8 9
[pgxc@redhat6 bin]$ initgtm -Z gtm -D /database/1922/pgdata1/pgdata_xc/gtm The files belonging to this GTM system will be owned by user "pgxc" . This user must also own the server process. fixing permissions on existing directory /database/1922/pgdata1/pgdata_xc/gtm .. . ok creating configuration files .. . ok Success. You can now start the GTM server using: gtm -D /database/1922/pgdata1/pgdata_xc/gtm or gtm_ctl -Z gtm -D /database/1922/pgdata1/pgdata_xc/gtm -l logfile start
配置 Coordinator 节点
192.168.1.35 配置以下
6.1 创建 coordinator 数据目录
1 2
[pgxc@redhat 6 pgdata_xc]$ mkdir -p /database/ 1922 /pgdata1/ pgdata_xc/coord1 [pgxc@redhat 6 pgdata_xc]$ mkdir -p /database/ 1922 /pgdata1/ pgdata_xc/coord2
6.2 install coord1
1
initdb -D /database/1922/pgdata1/pgdata_xc/coord1 --nodename coord1 -E UTF8 --locale=C -U postgres -W
6.3 修改 coord1 配置文件 postgresql.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
listen_addresses = '*' port = 1921 log_destination = 'csvlog' logging_collector = on log_directory = 'pg_log' log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' log_truncate_on_rotation = on log_rotation_age = 1 d log_rotation_size = 10 MB gtm_host = 'localhost' gtm_host = 'localhost' gtm_port = 6666 pgxc_node_name = 'coord1' pooler_port = 6667 max_pool_size = 100
6.4 配置 coord1 pg_hba.conf
1 2
host all all 192.168 .1 .36 /32 trust host all all 0.0 .0 .0 /0 md5
6.5 install coord2
1
initdb -D /database/1922/pgdata1/pgdata_xc/coord2 --nodename coord2 -E UTF8 --locale=C -U postgres -W
6.6 配置 coord2 配置文件 postgresql.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
listen_addresses = '*' port = 1925 log_destination = 'csvlog' logging_collector = on log_directory = 'pg_log' log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' log_truncate_on_rotation = on log_rotation_age = 1 d log_rotation_size = 10 MB gtm_host = 'localhost' gtm_host = 'localhost' gtm_port = 6666 pgxc_node_name = 'coord2' pooler_port = 6668 max_pool_size = 100
6.7 配置 coord1 pg_hba.conf
1 2
host all all 192.168 .1 .36 /32 trust host all all 0.0 .0 .0 /0 md5
6.8 修改防火墙,打开相应端口 ( 两台) 为了测试简便,先暂时关闭防火墙和 seliniux。
启动 PostgreSQL-XC
7.1 start gtm
1 2
[pgxc@redhat 6 bin]$ gtm -D /database/ 1922 /pgdata1/ pgdata_xc/gtm & [1 ] 6185
查看 gtm 是否启动
1 2 3 4 5 6 7
[pgxc@redhat 6 bin]$ gtm_ctl status -S gtm -D /database/ 1922 /pgdata1/ pgdata_xc/gtm pid: 6185 data: /database/ 1922 /pgdata1/ pgdata_xc/gtm active: 1 [pgxc@redhat 6 bin]$ ps -ef | grep gtm pgxc 6185 6135 0 20 :50 pts/4 00:00:00 gtm -D / database/1922/ pgdata1/pgdata_xc/ gtm 备注: gtm 进程已经正常启动。
7.2 启动数据节点( 192.168.1.36 )
1 2
postgres -X -D /pgdata_xc/db_1/pg_root -p 15431 -i & postgres -X -D /pgdata_xc/db_2/pg_root -p 15432 -i &
备注:-X 表示datanode 节点。 查看数据节点是否启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[pgxc@redhatB pg_log]$ ps -ef | grep pgxc root 371 351 0 09:16 pts/0 00:00:00 su - pgxc pgxc 372 371 0 09:16 pts/0 00:00:00 -bash root 420 398 0 09:16 pts/1 00:00:00 su - pgxc pgxc 421 420 0 09:16 pts/1 00:00:00 -bash pgxc 588 421 0 09:28 pts/1 00:00:00 postgres -X -D /pgdata_xc/db_1/pg_root -p 15431 -i pgxc 589 588 0 09:28 ? 00:00:00 postgres: logger process pgxc 591 588 0 09:28 ? 00:00:00 postgres: writer process pgxc 592 588 0 09:28 ? 00:00:00 postgres: wal writer process pgxc 593 588 0 09:28 ? 00:00:00 postgres: autovacuum launcher process pgxc 594 588 0 09:28 ? 00:00:00 postgres: stats collector process pgxc 595 421 0 09:28 pts/1 00:00:00 postgres -X -D /pgdata_xc/db_2/pg_root -p 15432 -i pgxc 596 595 0 09:28 ? 00:00:00 postgres: logger process pgxc 598 595 0 09:28 ? 00:00:00 postgres: writer process pgxc 599 595 0 09:28 ? 00:00:00 postgres: wal writer process pgxc 600 595 0 09:28 ? 00:00:00 postgres: autovacuum launcher process pgxc 601 595 0 09:28 ? 00:00:00 postgres: stats collector process
7.3 启动 coordinator 节点
1 2
postgres -C -D /database/1922 /pgdata1/pgdata_xc/coord1 -p 1921 -i & postgres -C -D /database/1922 /pgdata1/pgdata_xc/coord2 -p 1925 -i &
备注:-C 表示 coordinator 节点。
查看 coordinator 节点是否启来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
pgxc 11633 8961 0 10 :25 pts /3 00 :00 :00 postgres -C -D /database /1922 /pgdata1 /pgdata_xc /coord1 -p 1921 -i pgxc 11634 11633 0 10 :25 ? 00 :00 :00 postgres : logger process pgxc 11637 11633 0 10 :25 ? 00 :00 :00 postgres : pooler process pgxc 11638 11633 0 10 :25 ? 00 :00 :00 postgres : writer process pgxc 11639 11633 0 10 :25 ? 00 :00 :00 postgres : wal writer process pgxc 11640 11633 0 10 :25 ? 00 :00 :00 postgres : autovacuum launcher process pgxc 11641 11633 0 10 :25 ? 00 :00 :00 postgres : stats collector process pgxc 11643 8961 0 10 :25 pts /3 00 :00 :00 postgres -C -D /database /1922 /pgdata1 /pgdata_xc /coord2 -p 1925 -i pgxc 11644 11643 0 10 :26 ? 00 :00 :00 postgres : logger process pgxc 11647 11643 0 10 :26 ? 00 :00 :00 postgres : pooler process pgxc 11648 11643 0 10 :26 ? 00 :00 :00 postgres : writer process pgxc 11649 11643 0 10 :26 ? 00 :00 :00 postgres : wal writer process pgxc 11650 11643 0 10 :26 ? 00 :00 :00 postgres : autovacuum launcher process pgxc 11651 11643 0 10 :26 ? 00 :00 :00 postgres : stats collector process pgxc 12203 11633 0 10 :53 ? 00 :00 :00 postgres : postgres postgres ::1(13531) idle pgxc 12372 11643 0 11 :01 ? 00 :00 :00 postgres : postgres postgres ::1(14789) idle pgxc 12661 11643 0 11 :16 ? 00 :00 :00 postgres : francs francs ::1(14825) idle
备注:coord1,coord2 节点分别多了个pooler process 进程,pooler process 用来与数据节点进行通信的。
7.4 查看GTM,POOL连接
1 2 3 4 5 6 7 8 9
[root@redhat6 ~] # netstat -anp | grep gtm tcp 0 0 0 .0 .0 .0 :6666 0 .0 .0 .0 :* LISTEN 11620 /gtm tcp 0 0 :::6666 :::* LISTEN 11620 /gtm tcp 0 0 ::1 :6666 ::1 :52228 ESTABLISHED 11620 /gtm [root@redhat6 ~] # ps -ef | grep pool pgxc 11637 11633 0 10 :25 ? 00 :00 :00 postgres : pooler process pgxc 11647 11643 0 10 :26 ? 00 :00 :00 postgres : pooler process root 13375 11658 0 11 :53 pts /2 00 :00 :00 grep pool
备注:如果到了这步,没有看到 pooler process ,或者没有 gtm 相关连接,说明配置有问题。
注册节点
8.4 在 coord1,cord2 上注册数据节点
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
CREATE NODE db_1 WITH (TYPE='datanode',HOST = '192.168 .1 .36 ', PORT=15431 ); CREATE NODE db_2 WITH (TYPE='datanode',HOST = '192.168 .1 .36 ', PORT=15432 ); CREATE NODE coord2 WITH (TYPE='coordinator',HOST = 'localhost', PORT=1925 ); CREATE NODE coord1 WITH (TYPE='coordinator',HOST = 'localhost', PORT=1921 ); [pgxc@redhat6 gtm]$ psql -p 1921 -U postgres psql (PGXC 1.0 beta2, based on PG 9.1 .3 ) Type "help" for help.postgres=# select * from pgxc_node; node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id -----------+-----------+-----------+-----------+----------------+------------------+------------ coord1 | C | 5432 | localhost | f | f | 1885696643 (1 row) postgres=# CREATE NODE db_1 WITH (TYPE='datanode',HOST = '192.168 .1 .36 ', PORT=15431 ,PRIMARY, PREFERRED); CREATE NODE postgres=# CREATE NODE db_2 WITH (TYPE='datanode',HOST = '192.168 .1 .36 ', PORT=15432 ); CREATE NODE postgres=# CREATE NODE coord2 WITH (TYPE='coordinator',HOST = '192.168 .1 .35 ', PORT=1925 ); CREATE NODE postgres=# select * from pgxc_node; node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id -----------+-----------+-----------+--------------+----------------+------------------+------------- coord1 | C | 5432 | localhost | f | f | 1885696643 db_1 | D | 15431 | 192 .168 .1 .36 | f | f | 1356996994 db_2 | D | 15432 | 192 .168 .1 .36 | f | f | -822936791 coord2 | C | 1925 | localhost | f | f | -1197102633 postgres=# select pgxc_pool_reload(); pgxc_pool_reload ------------------ t (1 row)
备注:到了这里, PostgreSQL-XC 安装完成,接下来验证下。
测试
9.1 coord1 创建测试库和表
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
[pgxc@redhat6 coord2]$ psql -p 1921 -U postgres psql (PGXC 1.0beta2, based on PG 9.1.3) Type "help" for help. postgres= List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+---------+-------+------------------------ francs | postgres | UTF8 | C | C | =Tc/postgres + | | | | | postgres=CTc/postgres + | | | | | francs=C*T*c*/postgres postgres | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres (4 rows) postgres=# create database test_xc; CREATE DATABASE postgres=# /l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+---------+-------+------------------------ postgres | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres test_xc | postgres | UTF8 | C | C | (5 rows) postgres=# /c test_xc You are now connected to database "test_xc" as user "postgres". test_xc=# create table test_1 (id integer,name varchar(32)); CREATE TABLE test_xc=# insert into test_1 select generate_series(1,100),'test_xc'; INSERT 0 100
备注:在 coord1 节点上创建了测试库 test_xc,并在里面创建了一张表,接下来看看 coord2, db_1,db_2 节点情况。
9.2 coord2 上验证
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
[pgxc@redhat6 pg_log]$ psql -p 1925 -U postgres psql (PGXC 1.0beta2, based on PG 9.1.3) Type "help" for help. postgres =# /l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+---------+-------+------------------------ postgres | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres =CTc/postgres template1 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres =CTc/postgres test_xc | postgres | UTF8 | C | C | (5 rows) postgres =# /c test_xc You are now connected to database "test_xc" as user "postgres" . test_xc =# /d List of relations Schema | Name | Type | Owner --------+--------+-------+---------- public | test_1 | table | postgres (1 row) test_xc =# select count(*) from test_1; count ------- 100 (1 row)
9.3 数据节点 db_1 上验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[pgxc@redhatB pg_log]$ psql -p 15431 -U postgres -d test_xc psql (PGXC 1.0beta2, based on PG 9.1.3) Type "help" for help. test_xc =# /d WARNING: Do not have a GTM snapshot available WARNING: Do not have a GTM snapshot available List of relations Schema | Name | Type | Owner --------+--------+-------+---------- public | test_1 | table | postgres (1 row) test_xc =# select count(*) from test_1; WARNING: Do not have a GTM snapshot available WARNING: Do not have a GTM snapshot available count ------- 42 (1 row)
9.4 数据节点 db_2 上验证
1 2 3 4 5 6 7 8 9 10
[pgxc@redhatB pg_log]$ psql -p 15432 -U postgres -d test_ xc psql (PGXC 1.0beta2, based on PG 9.1.3) Type "help" for help. test_xc=# select count(*) from test_ 1; WARNING: Do not have a GTM snapshot available WARNING: Do not have a GTM snapshot available count ------- 58 (1 row)
备注:表共有100条记录,数据节点一分布了 42 条,数据节点二分布了 58 条,可见数据已经分片到数据节点上。 当然在创建表时,也可以设置 replication 模式,这样数据就会完全复制到每个节点。
常见错误
1 2 3
[pgxc@redhat6 coord1]$ psql -p 1921 -U postgres postgres=# create database test; ERROR: Failed to get pooled connections
备注:如果在 coord1 节点上操作时,报 “ERROR: Failed to get pooled connections”,可能的原因很多,可能是节点没有注册好,这时要查看 pgxc_node 视图;也有可能是 pg_hba.conf,防火墙,selinux 等问题,总之逐一排查。
总结
本次测试只是简单将 PostgreSQL-XC 搭建起来了,其中 gmt_standby,GTM-Proxy 没有配置,这个以后可以测试下;
由于在 PostgreSQL-XC 体系中,coordinator 并不存储数据,数据被分片在数据节点中,这种机制与Greenplum有点类似;而 coordinator 节点新增了 pooler process 进程,个人觉得 coordinator 很像一个连接池。
PostgreSQL-XC 所谓的多主节点( muti-master) 同时对外服务,实际上对应用服务的是 coordinator 节点,而不是数据节点本身;原来俺的理解是数据节点同时读写(多份数据,mater 同时读写),而在 PostgreSQL-XC 中并不是这样。
由于每个数据节点存储一部分数据,那么如果数据节点 down 掉,整个 PostgreSQL-XC 将不可用,不知是否有 更好的方案,例如给每个数据节点配置个 standby 节点?
由于在 PostgreSQL-XC 体系中数据分片在多个数据节点中,IO 性能会提升,同时对网络压力会提升;具体性能目前还没有测试。 6.尽管疑问重多,但还是要感谢来自日本的 PostgreSQL-XC 开发团队,他们为 PostgreSQL 带来了 muti-master的集群方案。
参考
http://postgres-xc.sourceforge.net/ http://postgres-xc.sourceforge.net/docs/1_0/index.html http://blog.163.com/digoal@126/blog/static/16387704020121952051174/ http://michael.otacoo.com/tag/postgres-xc/
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/237864.html