在CentOS7.2上部署Postgres-XL分布式数据库
1. 下載安裝包
移步這里下載:https://download.csdn.net/download/fm0517/10577703
2. 節(jié)點(diǎn)分類及說明
【GTM】
全局事務(wù)控制節(jié)點(diǎn),保證集群數(shù)據(jù)的一致性,與Coordinator節(jié)點(diǎn)和Datanode節(jié)點(diǎn)不斷通信,是整個(gè)集群的核心節(jié)點(diǎn),只存在一個(gè),可以存在一個(gè)GTM Standby節(jié)點(diǎn),對(duì)GTM實(shí)時(shí)備份。GTM一旦故障,整個(gè)集群立刻無法訪問,此時(shí)可以切換到GTM Standby節(jié)點(diǎn)上。如果部署了GTM Standby節(jié)點(diǎn),就應(yīng)該同時(shí)部署GTM Proxy,一般和Coordinator、Datanode部署在同一臺(tái)服務(wù)器上。GTM Proxy的作用代理Coordinator和Datanode對(duì)GTM的訪問,起到減輕GTM負(fù)載的作用,另外一個(gè)重要的作用是幫助完成GTM的故障切換,當(dāng)GTM節(jié)點(diǎn)發(fā)生故障后,GTM Standby成為新的GTM,此時(shí)Coordinator和Datanode節(jié)點(diǎn)并不需要重新指定GTM地址,只需要GTM Proxy重新連接到新的GTM地址即可。
【Coordinator】
接收數(shù)據(jù)訪問請(qǐng)求的節(jié)點(diǎn),本質(zhì)上是由PG后臺(tái)進(jìn)程組成。接收的一條查詢后,Coordinator節(jié)點(diǎn)執(zhí)行查詢計(jì)劃,然后會(huì)根據(jù)查詢數(shù)據(jù)涉及的數(shù)據(jù)節(jié)點(diǎn)將查詢分發(fā)給相關(guān)的數(shù)據(jù)節(jié)點(diǎn)。寫入數(shù)據(jù)時(shí),也會(huì)根據(jù)不同的數(shù)據(jù)分布策略將數(shù)據(jù)寫入相關(guān)的節(jié)點(diǎn)。可以說Coordinator節(jié)點(diǎn)上保存著集群的全局?jǐn)?shù)據(jù)位置。Coordinator節(jié)點(diǎn)可以任意擴(kuò)展,各個(gè)節(jié)點(diǎn)之間除了訪問地址不同以外是完全對(duì)等的,通過一個(gè)節(jié)點(diǎn)更新的數(shù)據(jù)可以在另一個(gè)節(jié)點(diǎn)上立刻看到。每個(gè)Coordinator節(jié)點(diǎn)可以配置一個(gè)對(duì)應(yīng)的standby節(jié)點(diǎn),避免單點(diǎn)故障。
【Datanode】
實(shí)際存取數(shù)據(jù)的節(jié)點(diǎn),接收Coordinator的請(qǐng)求并執(zhí)行SQL語句存取數(shù)據(jù),節(jié)點(diǎn)之間也會(huì)互相通信。一般的,一個(gè)節(jié)點(diǎn)上的數(shù)據(jù)并不是全局的,數(shù)據(jù)節(jié)點(diǎn)不直接對(duì)外提供數(shù)據(jù)訪問。一個(gè)表的數(shù)據(jù)在數(shù)據(jù)節(jié)點(diǎn)上的分布存在兩種模式:復(fù)制模式和分片模式,復(fù)制模式下,一個(gè)表的數(shù)據(jù)在指定的節(jié)點(diǎn)上存在多個(gè)副本;分片模式下,一個(gè)表的數(shù)據(jù)按照一定的規(guī)則分布在多個(gè)數(shù)據(jù)節(jié)點(diǎn)上,這些節(jié)點(diǎn)共同保存一份完整的數(shù)據(jù)。這兩種模式的選擇是在創(chuàng)建表的時(shí)候執(zhí)行CREATE TABLE語句指定的,具體語法如下:
可以看到,如果DISTRIBUTE BY 后面是REPLICATION,則是復(fù)制模式,其余則是分片模式,HASH指的是按照指定列的哈希值分布數(shù)據(jù),MODULO指的是按照指定列的取摩運(yùn)算分布數(shù)據(jù),ROUNDROBIN指的是按照輪詢的方式分布數(shù)據(jù)。TO NODE指定了數(shù)據(jù)分布的節(jié)點(diǎn)范圍,如果沒有指定則默認(rèn)所有數(shù)據(jù)節(jié)點(diǎn)參與數(shù)據(jù)分布。如果沒有指定分布模式,即使用普通的CREATE TABLE語句,PGXL會(huì)默認(rèn)采用分片模式將數(shù)據(jù)分布到所有數(shù)據(jù)節(jié)點(diǎn)。
3. 主機(jī)規(guī)劃
GTM: server0: 192.168.51.140
Coordinator: server1: 192.168.51.141
Datanode1 Master: server2: 192.168.51.142
Datanode2 Master: server3: 192.168.51.143
Datanode1 Slave: server4: 192.168.51.144
Datanode2 Slave: server5: 192.168.51.145
—–Datanode3用于后面演示動(dòng)態(tài)增刪節(jié)點(diǎn)—–
Datanode3 Master: server6: 192.168.51.146
【下面的步驟4-13,在所有節(jié)點(diǎn)上都要執(zhí)行】
4. 修改主機(jī)hosts文件
# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.51.140 server0 192.168.51.141 server1 192.168.51.142 server2 192.168.51.143 server3 192.168.51.144 server4 192.168.51.145 server5 192.168.51.146 server65. 安裝依賴的軟件包
openssh-clients, flex, bison, readline-devel, zlib-devel, openjade, docbook-style-dsssl, gcc
用rpm命令查看是否已安裝
如果沒裝可以用yum安裝
6. 卸載操作系統(tǒng)自帶的PostgreSQL
# rpm -qa |grep postgresql # rpm -qa |grep postgresql | xargs rpm -e --nodeps # rpm -qa |grep postgresql7. 添加postgres用戶
建立組: # groupadd postgres 建立用戶: # useradd -m -d /home/postgres postgres -g postgres 初始化密碼: # passwd postgres 輸入密碼:12345678(舉例) 注:如果需要?jiǎng)h除postgres用戶,可以以root用戶執(zhí)行命令: # userdel -r postgres8. 配置免密登陸
root用戶
# ssh-keygen # ssh-copy-id -i ~/.ssh/id_rsa.pub root@server0 # ssh-copy-id -i ~/.ssh/id_rsa.pub root@server1 # ssh-copy-id -i ~/.ssh/id_rsa.pub root@server2 # ssh-copy-id -i ~/.ssh/id_rsa.pub root@server3 # ssh-copy-id -i ~/.ssh/id_rsa.pub root@server4 # ssh-copy-id -i ~/.ssh/id_rsa.pub root@server5 # ssh-copy-id -i ~/.ssh/id_rsa.pub root@server6postgres用戶
$ ssh-keygen $ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@server0 $ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@server1 $ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@server2 $ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@server3 $ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@server4 $ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@server5 $ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@server6如果配置完成后,發(fā)現(xiàn)ssh并不免密,可按下面操作進(jìn)行完善:
chmod 755 /home/postgres chmod 700 /home/postgres/.ssh chmod 644 /home/postgres/.ssh/authorized_keys9. 修改內(nèi)核參數(shù)
在/etc/sysctl.conf中添加參數(shù)kernel.sem
# vi /etc/sysctl.conf kernel.sem = 50100 128256000 50100 2560使參數(shù)生效
# sysctl -p驗(yàn)證參數(shù)是否生效。
# ipcs -ls—— Semaphore Limits ——–
max number of arrays = 2560
max semaphores per array = 50100
max semaphores system wide = 128256000
max ops per semop call = 50100
semaphore max value = 32767
10. 配置防火墻
關(guān)閉防火墻
# systemctl stop firewalld # systemctl disable firewalld或在防火墻中開放端口
遇到防護(hù)墻不能關(guān)閉的情況,可使用該步驟!
具體需要開放的端口參加(主機(jī)規(guī)劃部分),以5432為例:
使用這些命令來永久打開一個(gè)新端口(如TCP/5432)。
注:查看防火墻狀態(tài)命令:systemctl status firewalld
注:開啟防火墻:systemctl start firewalld
注:關(guān)閉防火墻:systemctl stop firewalld
11. 關(guān)閉SELinux
查看SELinux狀態(tài),執(zhí)行:
# /usr/sbin/sestatus -v如果SELinux status參數(shù)為enabled即為開啟狀態(tài)
永久關(guān)閉SELinux,執(zhí)行:
將 SELINUX=enforcing 改為 SELINUX=disabled
重啟后生效
12. 安裝Postgres-XL軟件
解壓
# tar -jxvf postgres-xl-9.5r1.6.tar.bz2 # chown -R postgres:postgres postgres-xl-9.5r1.6切換用戶
# su - postgres安裝
$ cd postgres-xl-9.5r1.6 $ ./configure --prefix=/home/postgres/pgxl9.5 $ make $ make install安裝擴(kuò)展
$ cd contrib $ make $ make install13. 配置環(huán)境變量
在文件.bash_profile中添加如下內(nèi)容:
# su - postgres $ vi .bash_profileexport PGHOME=/home/postgres/pgxl9.5 export PGUSER=postgres export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH export PATH=$PGHOME/bin:$PATH使環(huán)境變量生效:
$ source ~/.bashrc驗(yàn)證環(huán)境變量是否生效:
pg_ctl --version顯示:pg_ctl (PostgreSQL) 9.5.8 (Postgres-XL 9.5r1.6)
14. 配置Postgres-XL集群
在GTM節(jié)點(diǎn)上以postgres用戶執(zhí)行:
$ pgxc_ctl ---初次執(zhí)行,會(huì)提示Error說沒有配置文件,忽略即可 PGXC prepare ---執(zhí)行該命令將會(huì)生成一份配置文件模板 PGXC exit --退出 pgxc_ctl交互窗執(zhí)行完成后,在postgres用戶根目錄下,會(huì)生成一個(gè)pgxc_ctl目錄,編輯其中的pgxc_ctl.conf文件
$ vi pgxc_ctl.conf修改為:
#!/usr/bin/env bash# pgxcInstallDir variable is needed if you invoke "deploy" command from pgxc_ctl utility. # If don't you don't need this variable. pgxcInstallDir=$PGHOME pgxlDATA=$PGHOME/data #---- OVERALL ----------------------------------------------------------------------------- # pgxcOwner=postgres # owner of the Postgres-XC databaseo cluster. Here, we use this# both as linus user and database user. This must be# the super user of each coordinator and datanode. pgxcUser=$pgxcOwner # OS user of Postgres-XC ownertmpDir=/tmp # temporary dir used in XC servers localTmpDir=$tmpDir # temporary dir used here locallyconfigBackup=n # If you want config file backup, specify y to this value. configBackupHost=pgxc-linker # host to backup config file configBackupDir=$HOME/pgxc # Backup directory configBackupFile=pgxc_ctl.bak # Backup file name --> Need to synchronize when original changed.#---- GTM ------------------------------------------------------------------------------------#---- GTM Master -----------------------------------------------#---- Overall ---- gtmName=gtm gtmMasterServer=server0 gtmMasterPort=6666 gtmMasterDir=$pgxlDATA/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)#---- Coordinators ----------------------------------------------------------------------------------------------------#---- shortcuts ---------- coordMasterDir=$pgxlDATA/nodes/coord coordSlaveDir=$pgxlDATA/nodes/coord_slave coordArchLogDir=$pgxlDATA/nodes/coord_archlog#---- Overall ------------ coordNames=(coord1) # Master and slave use the same name coordPorts=(5432) # Master ports poolerPorts=(20004) # Master pooler ports coordPgHbaEntries=(0.0.0.0/0) # Assumes that all the coordinator (master/slave) accepts# the same connection# This entry allows only $pgxcOwner to connect.# If you'd like to setup another connection, you should# supply these entries through files specified below.#---- Master ------------- coordMasterServers=server1 # none means this master is not available coordMasterDirs=$coordMasterDir coordMaxWALsernder=10 # max_wal_senders: needed to configure slave. If zero value is specified,# it is expected to supply this parameter explicitly by external files# specified in the following. If you don't configure slaves, leave this value to zero. coordMaxWALSenders=$coordMaxWALsernder# max_wal_senders configuration for each coordinator.#---- Configuration files--- coordExtraConfig=coordExtraConfig # Extra configuration file for coordinators. # This file will be added to all the coordinators'# postgresql.conf # Pleae note that the following sets up minimum parameters which you may want to change. # You can put your postgresql.conf lines here. cat > $coordExtraConfig <<EOF #================================================ # Added to all the coordinator postgresql.conf # Original: $coordExtraConfig log_destination = 'stderr' logging_collector = on log_directory = 'pg_log' listen_addresses = '*' max_connections = 512 EOF# Additional Configuration file for specific coordinator master. # You can define each setting by similar means as above. coordSpecificExtraConfig=(none none) coordExtraPgHba=none # Extra entry for pg_hba.conf. This file will be added to all the coordinators' pg_hba.conf coordSpecificExtraPgHba=(none none)#---- Datanodes -------------------------------------------------------------------------------------------------------#---- Shortcuts -------------- datanodeMasterDir=$pgxlDATA/nodes/dn_master datanodeSlaveDir=$pgxlDATA/nodes/dn_slave datanodeArchLogDir=$pgxlDATA/nodes/datanode_archlog#---- Overall --------------- #primaryDatanode=datanode1 # Primary Node. # At present, xc has a priblem to issue ALTER NODE against the primay node. Until it is fixed, the test will be done # without this feature. primaryDatanode=datanode1 # Primary Node. datanodeNames=(datanode1 datanode2) datanodePorts=(5433 5433) # Master ports datanodePoolerPorts=(20005 20005) # Master pooler ports datanodePgHbaEntries=(0.0.0.0/0) # Assumes that all the coordinator (master/slave) accepts# the same connection# This list sets up pg_hba.conf for $pgxcOwner user.# If you'd like to setup other entries, supply them# through extra configuration files specified below. # Note: The above parameter is extracted as "host all all 0.0.0.0/0 trust". If you don't want # such setups, specify the value () to this variable and suplly what you want using datanodeExtraPgHba # and/or datanodeSpecificExtraPgHba variables. #datanodePgHbaEntries=(::1/128) # Same as above but for IPv6 addresses#---- Master ---------------- datanodeMasterServers=(server2 server3) # none means this master is not available.# This means that there should be the master but is down.# The cluster is not operational until the master is# recovered and ready to run. datanodeMasterDirs=($datanodeMasterDir/dn1 $datanodeMasterDir/dn2) datanodeMaxWalSender=10 # max_wal_senders: needed to configure slave. If zero value is # specified, it is expected this parameter is explicitly supplied# by external configuration files.# If you don't configure slaves, leave this value zero. datanodeMaxWALSenders=($datanodeMaxWalSender $datanodeMaxWalSender)# max_wal_senders configuration for each datanode#---- Slave ----------------- datanodeSlave=y # Specify y if you configure at least one coordiantor slave. Otherwise, the following# configuration parameters will be set to empty values.# If no effective server names are found (that is, every servers are specified as none),# then datanodeSlave value will be set to n and all the following values will be set to# empty values. datanodeSlaveServers=(server4 server5) # value none means this slave is not available datanodeSlavePorts=(15433 15433) # value none means this slave is not available datanodeSlavePoolerPorts=(20015 20015) # value none means this slave is not available datanodeSlaveSync=y # If datanode slave is connected in synchronized mode datanodeSlaveDirs=($datanodeSlaveDir $datanodeSlaveDir) datanodeArchLogDirs=( $datanodeArchLogDir $datanodeArchLogDir)# ---- Configuration files --- # You may supply your bash script to setup extra config lines and extra pg_hba.conf entries here. # These files will go to corresponding files for the master. # Or you may supply these files manually. datanodeExtraConfig=none # Extra configuration file for datanodes. This file will be added to all the # datanodes' postgresql.conf datanodeSpecificExtraConfig=(none none) datanodeExtraPgHba=none # Extra entry for pg_hba.conf. This file will be added to all the datanodes' postgresql.conf datanodeSpecificExtraPgHba=(none none)15.初始化集群
在GTM節(jié)點(diǎn)上以postgres用戶執(zhí)行:
初始化集群:
啟動(dòng)集群:
$ pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf start all停止集群
$ pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all16.刪除集群
在所有節(jié)點(diǎn)上以postgres用戶執(zhí)行:
$ rm /home/postgres/pgxl9.5/data/nodes總結(jié)
以上是生活随笔為你收集整理的在CentOS7.2上部署Postgres-XL分布式数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 已被管理员、加密政策或凭据存储停用
- 下一篇: SQL语句遍历树结构的数据表