前言
最近在學習postgresql,工作中需要,買了一本《PostgreSQL 修煉之道》按照上面操作,幾次按照上面的都是失敗。後面查看很多資料後,才成功。這是根據這本書第十九章內容修改的地方,希望給廣大學習愛好者幫助。最後希望那些作者,把書寫的詳細一些,把自己做成功的案例寫出來。
一、系統環境
系統平臺:centos 6.5
postgres-XC版本: pgxc-v1.2.1.tar.gz
防火牆關閉 selinux設置SELINUX=disabled
二、集羣規劃
主機名 | IP地址 | 角色 | 端口 | nodename | 數據目錄 |
gtm | 172.16.0.101 | Gtm | 6666 | gtm | /home/pgxc/gtm |
standby | 172.16.0.102 | Gtm的備庫 | 6666 | standby | /home/pgxc/gtm_standby |
cd1 | 172.16.0.103 | Coordinator | 5432 | co1 | /home/pgxc/coordinator |
Datanode | 5433 | dn1 | /home/pgxc/pgdata | ||
Gtm Proxy | 6666 | gtmproxy01 | /home/pgxc/gtm_proxy | ||
cd2 | 172.16.0.104 | Coordinator | 5432 | co2 | /home/pgxc/coordinator |
Datanode | 5433 | dn2 | /home/pgxc/pgdata | ||
Gtm Proxy | 6666 | gtmproxy02 | /home/pgxc/gtm_proxy | ||
cd3 | 172.16.0.105 | Coordinator | 5432 | co3 | /home/pgxc/coordinator |
Datanode | 5433 | dn3 | /home/pgxc/pgdata | ||
Gtm Proxy | 6666 | gtmproxy03 | /home/pgxc/gtm_proxy |
三、安裝依賴包(五臺操作)
yum install -y bison flex perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
四、增加用戶(五臺操作)
groupadd pgxc
useradd pgxc -g pgxc
passwd pgxc
五、源碼安裝(五臺操作)
tar zxvf pgxc-v1.2.1.tar.gz
cd postgres-xc-1.2.1/
./configure --prefix=/opt/pgxc --with-perl --with-python
gmake
gmake install
六、配置環境變量(五臺操作)
su - pgxc
vi .bash_profile
export PGHOME=/opt/pgxc
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'
source .bash_profile
七、初始化GTM
在172.16.0.101這臺機器上運行如下命令:
[root@gtm ~]# su - pgxc
[pgxc@gtm ~]$ initgtm -Z gtm -D /home/pgxc/gtm
編輯/home/pgxc/gtm/gtm.conf文件,如下:
nodename = 'gtm'
listen_addresses = '*'
port = 6666
startup = ACT
配置項的說明如下。
nodename:指定節點的名稱,可能指定爲任意的一個名稱,不能與其他節點的名稱重複。
listen_address:GTM監聽的IP地址,“*”在所有的IP地址上監聽
port:GTM監控的端口
startup:確定GTM啓動後是主庫還是standby。如果是主庫,設置爲“ACT”,如果是standby,則設置爲“STANDBY”。
八、初始化GTM的備庫
在172.16.0.102這臺機器上運行如下命令:
[root@standby ~]# su - pgxc
[pgxc@standby ~]$ initgtm -Z gtm -D /home/pgxc/gtm_standby
編輯/home/pgxc/gtm_standby/gtm.conf文件,如下:
nodename = 'standby'
listen_addresses = '*'
port = 6666
startup = STANDBY
active_host = '172.16.0.101'
active_port =6666
配置項的說明如下。
startup:因爲是GTM的備庫,所以要設置爲“STANDBY”。
active_host:指定連接GTM主庫的IP地址
active_port:指定連接GTM備庫的端口。
九、初始化GTM Proxy
在172.16.0.103,172.16.0.104,172.16.0.105這三臺機器pgxc用戶上,分別執行以下命令:
initgtm -Z gtm_proxy -D /home/pgxc/gtm_proxy
172.16.0.103中配置文件 /home/pgxc/gtm_proxy/gtm_proxy.conf的內容如下:
nodename = 'gtmproxy01'
port = 6666
gtm_host = '172.16.0.101'
gtm_port = 6666
172.16.0.104中配置文件 /home/pgxc/gtm_proxy/gtm_proxy.conf的內容如下:
nodename = 'gtmproxy02'
port = 6666
gtm_host = '172.16.0.101'
gtm_port = 6666
172.16.0.105中配置文件 /home/pgxc/gtm_proxy/gtm_proxy.conf的內容如下:
nodename = 'gtmproxy03'
port = 6666
gtm_host = '172.16.0.101'
gtm_port = 6666
十、初始化Coordinator 、數據節點
1.在172.16.0.103這臺機器上運行如下命令:
initdb -D /home/pgxc/coordinator --nodename co1 -E UTF8 --local=C -U pgxc -W
initdb -D /home/pgxc/pgdata --nodename dn1 -E UTF8 --local=C -U pgxc -W
172.16.0.103這臺機器上 coordinator配置文件/home/pgxc/coordinator/postgresql.conf的內容如下:
# - Connection Settings -
listen_addresses = '*'
port = 5432
max_connections = 100
# DATA NODES AND CONNECTION POOLING
#-------------------------------------------
pooler_port = 6667
min_pool_size = 1
max_pool_size = 100
# GTM CONNECTION
#----------------------
gtm_host = '172.16.0.101'
gtm_port = 6666
pgxc_node_name = 'co1'
172.16.0.103這臺機器上 coordinator配置文件/home/pgxc/coordinator/pg_hba.conf的內容如下:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 172.16.0.0/24 trust
host all all 0.0.0.0/0 md5
172.16.0.103這臺機器上數據節點配置文件/home/pgxc/pgdata/postgresql.conf的內容如下:
# - Connection Settings -
listen_addresses = '*'
port = 5433
max_connections = 100
# DATA NODES AND CONNECTION POOLING
#----------------------------------------
pooler_port = 6668
#min_pool_size = 1
max_pool_size = 100
# GTM CONNECTION
#---------------------------
gtm_host = '172.16.0.101'
gtm_port = 6666
pgxc_node_name = 'dn1'
172.16.0.103這臺機器上數據節點配置文件/home/pgxc/pgdata/pg_hba.conf的內容如下:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 172.16.0.0/24 trust
host all all 0.0.0.0/0 md5
2.在172.16.0.104這臺機器上運行如下命令:
initdb -D /home/pgxc/coordinator --nodename co2 -E UTF8 --local=C -U pgxc -W
initdb -D /home/pgxc/pgdata --nodename dn2 -E UTF8 --local=C -U pgxc -W
172.16.0.104這臺機器上 coordinator配置文件/home/pgxc/coordinator/postgresql.conf的內容如下:
# - Connection Settings -
listen_addresses = '*'
port = 5432
max_connections = 100
# DATA NODES AND CONNECTION POOLING
#-------------------------------------------
pooler_port = 6667
min_pool_size = 1
max_pool_size = 100
# GTM CONNECTION
#----------------------
gtm_host = '172.16.0.101'
gtm_port = 6666
pgxc_node_name = 'co2'
172.16.0.104這臺機器上 coordinator配置文件/home/pgxc/coordinator/pg_hba.conf的內容如下:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 172.16.0.0/24 trust
host all all 0.0.0.0/0 md5
172.16.0.104這臺機器上數據節點配置文件/home/pgxc/pgdata/postgresql.conf的內容如下:
# - Connection Settings -
listen_addresses = '*'
port = 5433
max_connections = 100
# DATA NODES AND CONNECTION POOLING
#----------------------------------------
pooler_port = 6668
#min_pool_size = 1
max_pool_size = 100
# GTM CONNECTION
#---------------------------
gtm_host = '172.16.0.101'
gtm_port = 6666
pgxc_node_name = 'dn2'
172.16.0.104這臺機器上數據節點配置文件/home/pgxc/pgdata/pg_hba.conf的內容如下:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 172.16.0.0/24 trust
host all all 0.0.0.0/0 md5
3. 在172.16.0.105這臺機器上運行如下命令:
initdb -D /home/pgxc/coordinator --nodename co3 -E UTF8 --local=C -U pgxc -W
initdb -D /home/pgxc/pgdata --nodename dn3 -E UTF8 --local=C -U pgxc -W
172.16.0.105這臺機器上 coordinator配置文件/home/pgxc/coordinator/postgresql.conf的內容如下:
# - Connection Settings -
listen_addresses = '*'
port = 5432
max_connections = 100
# DATA NODES AND CONNECTION POOLING
#-------------------------------------------
pooler_port = 6667
min_pool_size = 1
max_pool_size = 100
# GTM CONNECTION
#----------------------
gtm_host = '172.16.0.101'
gtm_port = 6666
pgxc_node_name = 'co3'
172.16.0.105這臺機器上 coordinator配置文件/home/pgxc/coordinator/pg_hba.conf的內容如下:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 172.16.0.0/24 trust
host all all 0.0.0.0/0 md5
172.16.0.105這臺機器上數據節點配置文件/home/pgxc/pgdata/postgresql.conf的內容如下:
# - Connection Settings -
listen_addresses = '*'
port = 5433
max_connections = 100
# DATA NODES AND CONNECTION POOLING
#----------------------------------------
pooler_port = 6668
#min_pool_size = 1
max_pool_size = 100
# GTM CONNECTION
#---------------------------
gtm_host = '172.16.0.101'
gtm_port = 6666
pgxc_node_name = 'dn3'
172.16.0.105這臺機器上數據節點配置文件/home/pgxc/pgdata/pg_hba.conf的內容如下:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 172.16.0.0/24 trust
host all all 0.0.0.0/0 md5
十一、啓動集羣
啓動集羣的順序爲:
GTM
GTM Standby
GTP-Proxy
Datanodes
Coordinators
啓動上面示例的集羣的方法如下。
在172.16.0.101機器啓動gtm,命令如下:
[pgxc@gtm ~]$ gtm_ctl -Z gtm start -D /home/pgxc/gtm
在172.16.0.102機器啓動gtm standby,命令如下:
[pgxc@standby ~]$ gtm_ctl -Z gtm_standby start -D /home/pgxc/gtm_standby
可以用下面的命令查看gtm和gtm standby是否啓動:
[pgxc@gtm ~]$ gtm_ctl -Z gtm status -D /home/pgxc/gtm
gtm_ctl: server is running (PID: 2091)
"-D" "/home/pgxc/gtm"
1 master
[pgxc@standby ~]$ gtm_ctl -Z gtm_standby status -D /home/pgxc/gtm_standby
gtm_ctl: server is running (PID: 2095)
"-D" "/home/pgxc/gtm_standby"
0 slave
以上說明啓動成功。
啓動完gtm和gtm_standby後,就可以啓動gtm_proxy了。在172.17.0.103,172.16.0.104,172.16.0.105這三臺機器上運行如下命令:
gtm_ctl -Z gtm_proxy start -D /home/pgxc/gtm_proxy
啓動完gtm_proxy就可以啓動Datanodes了。在上述三臺機器分別運行如下命令來啓動:
pg_ctl start -D /home/pgxc/pgdata -Z datanode
最後啓動Coordinators。在上述三臺機器上運行如下命令啓動:
pg_ctl start -D /home/pgxc/coordinator -Z coordinator
十二、配置集羣節點信息
在各個Coordinator上,執行如下命令:
下面就執行172.16.0.103主機器上的。其它的大家自己操作。
psql -p 5432 postgres
[pgxc@cd1 ~]$ psql -p 5432 postgres
psql (PGXC , based on PG 9.3.2)
Type "help" for help.
postgres=# select * from pgxc_node;
node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id
-----------+-----------+-----------+--------------+----------------+------------------+-------------
co1 | C | 5432 | localhost | f | f
| 1344656819
postgres=#create node dn1 with(type='datanode',host='172.16.0.103',port=5433,primary,preferred);
postgres=#create node dn2 with(type='datanode',host='172.16.0.104',port=5433);
postgres=#create node dn3 with(type='datanode',host='172.16.0.105',port=5433);
postgres=#create node co2 with(type='coordinator',host='172.16.0.104',port=5432);
postgres=#create node co3 with(type='coordinator',host='172.16.0.105',port=5432);
postgres=# select * from pgxc_node;
node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id
-----------+-----------+-----------+--------------+----------------+------------------+-------------
co1 | C | 5432 | localhost | f | f | 1344656819
dn1 | D | 5433 | 172.16.0.103 | t | t | -560021589
dn2 | D | 5433 | 172.16.0.104 | f | f | 352366662
dn3 | D | 5433 | 172.16.0.105 | f | f | -700122826
co2 | C | 5432 | 172.16.0.104 | f | f | 474101254
co3 | C | 5432 | 172.16.0.105 | f | f | -1046823559
(6 rows)
postgres=#select pgxc_pool_reload();
十三、測試
[pgxc@cd1 ~]$ psql -p 5432 postgres
psql (PGXC , based on PG 9.3.2)
Type "help" for help.
postgres=# select * from pgxc_node;
node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id
-----------+-----------+-----------+--------------+----------------+------------------+-------------
co1 | C | 5432 | localhost | f | f | 1344656819
dn1 | D | 5433 | 172.16.0.103 | t | t | -560021589
dn2 | D | 5433 | 172.16.0.104 | f | f | 352366662
dn3 | D | 5433 | 172.16.0.105 | f | f | -700122826
co2 | C | 5432 | 172.16.0.104 | f | f | 474101254
co3 | C | 5432 | 172.16.0.105 | f | f | -1046823559
(6 rows)
postgres=# create database text;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
postgres | pgxc | UTF8 | C | C |
template0 | pgxc | UTF8 | C | C | =c/pgxc +
| | | | | pgxc=CTc/pgxc
template1 | pgxc | UTF8 | C | C | =c/pgxc +
| | | | | pgxc=CTc/pgxc
text | pgxc | UTF8 | C | C |
(4 rows)
以上說明全部安裝成功。