博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POSTGRESQL10.3源码安装主从搭建
阅读量:2432 次
发布时间:2019-05-10

本文共 6573 字,大约阅读时间需要 21 分钟。

一、下载POSTGRESQL源码安装包及主机配置
https://www.postgresql.org/ftp/source/v10.3/
虚拟机环境
node1  192.168.159.151
node2  192.168.159.152
操作系统为redhat6.5
数据库为postgresql10.3
两个节点均配置/etc/hosts
vi /etc/hosts
node1  192.168.159.151
node2  192.168.159.152
二、编译安装
(1)创建postgres用户
useradd -m -r -s /bin/bash -u 5432 postgres
(2)安装相关依赖包
yum install gettext gcc make perl python perl-ExtUtils-Embed   readline-devel   zlib-devel    openssl-devel   libxml2-devel  cmake  gcc-c++ libxslt-devel  openldap-devel  pam-devel  python-devel  cyrus-sasl-devel  libgcrypt-devel  libgpg-error-devel  libstdc++-devel
(3)配置POSTGRES
./configure --prefix=/opt/postgresql-10.3 --with-segsize=8 --with-wal-segsize=64 --with-wal-blocksize=16 --with-blocksize=16 --with-libedit-preferred --with-perl --with-python --with-openssl --with-libxml --with-libxslt --enable-profiling --enable-thread-safety --enable-nls=zh_CN
最后几行出现以下黄色输出即配置正确,否则根据报错提示继续安装依赖包
configure: using CPPFLAGS= -D_GNU_SOURCE -I/usr/include/libxml2 
configure: using LDFLAGS=  -Wl,--as-needed
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/dynloader/linux.c to src/backend/port/dynloader.c
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
(4)编译
make && make install
最后几行出现以下黄色输出即配置正确
make[1]: Leaving directory `/opt/postgresql-10.3/src'
make -C config install
make[1]: Entering directory `/opt/postgresql-10.3/config'
/bin/mkdir -p '/opt/postgresql-10.3/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/opt/postgresql-10.3/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/opt/postgresql-10.3/lib/pgxs/config/missing'
make[1]: Leaving directory `/opt/postgresql-10.3/config'
PostgreSQL installation complete.
(5)安装
make world && make install -world
最后几行出现以下黄色输出即配置正确
make[1]: Leaving directory `/opt/postgresql-10.3/src'
make -C config install
make[1]: Entering directory `/opt/postgresql-10.3/config'
/bin/mkdir -p '/opt/postgresql-10.3/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/opt/postgresql-10.3/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/opt/postgresql-10.3/lib/pgxs/config/missing'
make[1]: Leaving directory `/opt/postgresql-10.3/config'
PostgreSQL installation complete.
make: Leaving directory `/opt/postgresql-10.3'
(6)创建相关目录及配置环境变量
mkdir -p /data/pgdata/serverlog
mkdir /data/pg
su - postgres
vi .bash_profile  (删除原来的所有,以下黄色部分直接复制粘贴)
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
# postgres
#PostgreSQL端口
PGPORT=5432
#PostgreSQL数据目录
PGDATA=/data/pgdata
export PGPORT PGDATA 
#所使用的语言
export LANG=zh_CN.utf8
#PostgreSQL 安装目录
export PGHOME=/data/pg
#PostgreSQL 连接库文件
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
#将PostgreSQL的命令行添加到 PATH 环境变量
export PATH=$PGHOME/bin:$PATH
#PostgreSQL的 man 手册
export MANPATH=$PGHOME/share/man:$MANPATH
#PostgreSQL的默认用户
export PGUSER=postgres
#PostgreSQL默认主机地址
export PGHOST=127.0.0.1
#默认的数据库名
export PGDATABASE=postgres
#定义日志存放目录
PGLOG="$PGDATA/serverlog"
source .bash_profile
(7)初始化数据库
#执行数据库初始化脚本
root用户登录
chown -R postgres.postgres /data/
su - postgres
$/opt/postgresql-10.3/bin/initdb --encoding=utf8 -D /data/pg/data
警告:为本地连接启动了 "trust" 认证.
你可以通过编辑 pg_hba.conf 更改或你下次
行 initdb 时使用 -A或者--auth-local和--auth-host选项.
Success. You can now start the database server using:
启动数据库
su - postgres
/opt/postgresql-10.3/bin/pg_ctl -D /data/pg/data -l logfile start
(8)相关命令拷贝
root用户
mkdir /data/pg/bin
cp /opt/postgresql-10.3/bin/*   /data/pg/bin
chown -R postgres.postgres  /data/pg/bin
三、postgresql主从搭建
1、主库配置

(1)创建一个用户复制的用户replica
su - postgres
psql
CREATE ROLE replica login replication encrypted password 'replica';
 
(2)修改pg_hba.conf文件,指定replica登录网络(最后一添加)
vi /data/pg/data/pg_hba.conf
host    replication     replica            192.168.159.0/24
          md5
host    all          replica           192.168.159.0/24
          trust
 
(3)主库配置文件修改以下几项,其他不变
vi
/data/pg/
data/ postgresql.conf
listen_addresses = '*'
wal_level = hot_standby  #热备模式
max_wal_senders= 6 #可以设置最多几个流复制链接,差不多有几个从,就设置多少
wal_keep_segments = 20  #重要配置(这里的设置会影响到pg_wal目录下文件的数量,建议不要设置太大,20就够了)
wal_send_timeout = 60s
max_connections = 512 #从库的 max_connections要大于主库
archive_mode = on #允许归档
#archive_command = 'cp %p /url/path%f'
    #根据实际情况设置

2、从库环境
(1)把备库的数据文件夹目录清空
rm -rf /var/lib/pgsql/10/data/*
(2)在备库上运行
pg_basebackup -F p --progress -D /data/pg/data/ -h 192.168.159.151 -p 5432 -U replica --password
输入密码replica 
!!!注意,复制完成后,在备库一定要将数据目录下的所有文件重新授权
chown -R postgres.postgres
/data/pg
/data/
 
(3)创建recovery.conf 文件
cp  /opt/postgresql-10.3/share/recovery.conf.sample
/data/pg
/data/ recovery.conf
 
vi
/data/pg
/data/ recovery.conf
standby_mode = on
primary_conninfo = 'host=192.168.159.151 port=5432 user=replica password=replica'
recovery_target_timeline = 'latest'
trigger_file = '
/data/pg
/data/
trigger.kenyon '
 
(4)配置
postgresql.conf文件
vi
/data/pg
/data/ postgresql.conf
listen_addresses ='*'
wal_level = hot_standby
max_connections =1000 
#一般从的最大链接要大于主的
hot_standby =on  
#说明这台机器不仅仅用于数据归档,也用于查询
max_standby_streaming_delay =30s
wal_receiver_status_interval = 10s  
#多久向主报告一次从的状态
hot_standby_feedback = on   
#如果有错误的数据复制,是否向主进行范例

(5) 启动备库

su - postgres
/opt/postgresql-10.3/bin/pg_ctl -D /data/pg/data/ -l logfile start
如果无法启动,到主库复制文件
postmaster.opts到备库 如下操作:
scp
/data/pg/data /postmaster.opts 192.168.159.152:
/data/pg/data/
chown -R postgres.postgres
/data/pg/data/
cd /data/pg/
chmod 700 data/
 
3、验证主从功能
主库查询
su - postgres
psql
postgres=# select client_addr,sync_state from pg_stat_replication;
   client_addr   | sync_state
-----------------+------------
 192.168.159.152 | async
(1 row)
发现登陆postgres时出现以下问题
-bash-4.1$ 
root用户执行
cp /etc/skel/.bash* /var/lib/pgsql/
再次登陆即可变成
[postgres@node1 ~]$
--未完待续

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28371090/viewspace-2155400/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28371090/viewspace-2155400/

你可能感兴趣的文章
滴滴裁员 2000 人,具体补偿方案已出
查看>>
余生,做个不焦虑的程序员!
查看>>
Spring Boot 中的响应式编程和 WebFlux 入门
查看>>
如何从零开始两天撸一个微信小程序?!(内含源码)
查看>>
女神?御姐?文艺?这样的程序媛你绝没见过! | 程序员有话说
查看>>
“软件外包城”下的马鞍山 | 程序员有话说
查看>>
那些上相亲网站的程序员,后来怎么样了?
查看>>
程序员如何实现财富自由?
查看>>
你我的父母,都在被互联网“割韭菜”
查看>>
程序员下班后都忙些啥?| 程序员有话说
查看>>
Java 帝国对 Python 的渗透能成功吗?
查看>>
程序员写代码没激情该怎么破?
查看>>
我是如何从低端面畜到高端面霸的?
查看>>
百面机器学习!算法工程师面试宝典!| 码书
查看>>
漫画 | 一个前端渣渣的成功逆袭
查看>>
与吴恩达并肩战斗,她是 AI 界的女超人!|人物志
查看>>
微信手机 WeOS 的可行性到底有多大?
查看>>
阿里面试,我挂在了第四轮……
查看>>
C++ 程序员到高级架构师,必须经历的三个阶段
查看>>
和 Java、C# 等语言对比后,Python 简直酷上天了!
查看>>