一、集羣的規劃
| hostname |ip | roule|port|
| :-----| -----😐 :–😐
| gtm| 192.168.1.1 | gtm | 6666 |
| datanode1|192.168.1.5 | coordinator & datanode1 & gtm proxy|5432 5433
| datanode2 |192.168.1.6 | coordinator & datanode2 & gtm proxy |
| backupnode1|192.168.17 | datanode1 slaver |15433
| backupnode2|192.168.1.8| datanode2 slaver & gtm slaver |20001
注:角色都可以根據自己的需求來劃分,可用靈活的調整
二、創建用戶並裝備環境,以下操作無特殊說明,每個節點都需要進行操作
1、創建postgres用戶,並且創建ssh免密登陸的目錄,授予權限
useradd postgres
passwd postgres
su - postgres
mkdir ~/.ssh
chmod 700 ~/.ssh
2、關閉防火牆和selinux
systemctl stop firewalld
systemctl disable firewalld #重啓不會啓動
systemctl status firewalld #查看一下狀態,確定一下
關閉selinux,編輯/etc/selinux/config文件
將SELINUX的值設置爲disabled
3、進行域名解析,配置/etc/hosts,在末尾添加如下內容
192.168.1.1 gtm
192.168.1.5 datanode1
192.168.1.6 datanode2
192.168.1.7 backupnode1
192.168.1.8 backupnode2
4、配置gtm到其他節點免密ssh登陸,在gtm節點操作
su – postgres
ssh-keygen -t rsa #這裏一定要一直按回車,不要輸入任何字符
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys postgres@datanode1:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys postgres@datanode2:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys postgres@backupnode1:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys postgres@backupnode2:~/.ssh/authorized_keys
注:我們配置完可以驗證一下,在gtm上用postgres用戶ssh其他機器
#ssh postgres@datanode1 其他測試方法相同,看gtm能否免密登陸其他設備
三、安裝postgress數據庫,我們採用的源碼安裝方式,每個節點需要安裝,切換爲root用戶
1、安裝基礎環境
yum install -y make gcc tar readline readline-devel zlib zlib-devel flex bison openjade docbook-style-dsssl
2、上傳postgres-xl-9.5r1.6.tar.gz壓縮包
3、安裝postgres-xl和pgxc_ctl
tar -zxvf postgres-xl-9.5r1.6.tar.gz
cd postgres-xl
./configure --prefix=/home/postgres/pgxl/
make
make install
cd contrib/
make
make install
4、配置環境變量,在每個節點操作,需切換到postgres用戶
su – postgres
# vi ~/.bashrc
export PGHOME=/usr/local/pgsql
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
# source ~/.bashrc
注:可以用echo $PGHOME查看變量是否生效
5、每個節點目錄權限設置,切換爲root用戶
su root
# chown -R postgres:postgres /usr/local/pgsql
6、配置集羣配置文件,僅在gtm上配置,先生成一份模板,在修改其中內容,然後同步到其他設備,
gxc_ctl ---忽略錯誤提示
PGXC prepare ---該命令會生成一份模板配置文件
PGXC ^C
編輯/home/postgres/pgxc_ctl/pgxc_ctl.conf
#---- GTM Master -----------------------------------------------
#---- Overall ----
gtmName=gtm
gtmMasterServer=gtm
gtmMasterPort=6666
gtmMasterDir=$HOME/pgxc/nodes/gtm
#---- Configuration ---
gtmExtraConfig=none # Will be added gtm.conf for both Master and Slave (done at initilization only)
gtmMasterSpecificExtraConfig=none # Will be added to Master's gtm.conf (done at initialization only)
#---- GTM Slave -----------------------------------------------
# Because GTM is a key component to maintain database consistency, you may want to configure GTM slave
# for backup.
#---- Overall ------
gtmSlave=y # Specify y if you configure GTM Slave. Otherwise, GTM slave will not be configured and
# all the following variables will be reset.
gtmSlaveName=gtm_slaver
gtmSlaveServer=backupnode1 # value none means GTM slave is not available. Give none if you don't configure GTM Slave.
gtmSlavePort=20001 # Not used if you don't configure GTM slave.
gtmSlaveDir=$HOME/pgxc/nodes/gtm # Not used if you don't configure GTM slave.
# Please note that when you have GTM failover, then there will be no slave available until you configure the slave
# again. (pgxc_add_gtm_slave function will handle it)
#---- Configuration ----
gtmSlaveSpecificExtraConfig=none # Will be added to Slave's gtm.conf (done at initialization only)
注:我只貼出部分配置,每個模塊都一樣,根據自己的規劃圖來配置
四、初始化集羣並且啓動集羣,在gtm上初始化,操作會自動初始化(配置)各個節點。
pgxc_ctl init all #會初始化其他節點
pgxc_ctl monitor all #查看節點是否運行
五、測試集羣
可以在任意節點上輸入以下命令,查看除gtm以外的所有節點的配置情況:
select * from pgxc_node;
簡單的測試,在datanode1節點上創建一個數據庫test,並創建一個表test,插入數據:
psql -p 20004 -U postgres
CREATE DATABASE test;
\c test
CREATE TABLE test(id int);
INSERT INTO test VALUES(1);
SELECT * FROM test;
\q
在datanode2節點上查看剛剛在datanode1節點的更改:
psql -p 20004 -U postgres
\c test
SELECT * FROM test;
\q
測試是有數據,至此我們的pg_xl集羣搭建完畢!