环境:
OS:CentOs 7Postgres-11.6
1.安装步骤
1.1 环境部署数据库部署节点 ip ⾓⾊
Host01 192.168.1.130 主Host02 192.168.1.131 从
1.2 配置等效连接
因为我们需要将主库的归档⽇志通过scp免密传输到备库等效连接配置可以参考
https://www.cnblogs.com/hxlasky/p/12204180.html1.3 主库安装
1.3.1 安装介质准备
下载地址: https://www.postgresql.org/ftp/source/我这⾥下载的是11.6版本,介质如下:postgresql-11.6.tar.gz
这⾥下载的源码,所以下⾯的步骤是源码安装1.3.2 安装依赖包yum install readlineyum install gcc
yum -y install -y readline-develyum install zlib-devel
1.3.3 编译安装
[root@localhost soft]# tar -xvf postgresql-11.6.tar.gz[root@localhost soft]#mkdir -p /opt/postgresql-11.6[root@localhost soft]# cd postgresql-11.6
[root@localhost soft]#./configure --prefix=/opt/postgresql-11.6[root@localhost soft]#make
[root@localhost soft]#make install
1.3.4 创建相应的⽤户
[root@localhost opt]# groupadd postgres
[root@localhost opt]# useradd -g postgres postgres
1.3.5 创建数据及⽇志⽬录,并做相应授权
[root@localhost soft]#mkdir -p /opt/postgresql-11.6/{data,log}
[root@localhost soft]#chown -R postgres:postgres /opt/postgresql-11.61.3.6 初始化数据库#su - postgres
[postgres@localhost bin]$ cd /opt/postgresql-11.6/bin
[postgres@localhost bin]$ ./initdb -D /opt/postgresql-11.6/data/
1.3.7 启动数据库
[postgres@localhost bin]$ cd /opt/postgresql-11.6/bin
[postgres@localhost bin]$./pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start
1.3.8 修改环境变量
[postgres@localhost ~]$ more .bash_profile# .bash_profile
# Get the aliases and functionsif [ -f ~/.bashrc ]; then . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/opt/postgresql-11.6/binexport PATH
1.3.9 登陆使⽤
[postgres@localhost bin]$cd /opt/postgresql-11.6/bin[postgres@localhost bin]$ ./psqlpsql (11.6)
Type \"help\" for help.
postgres=# \\du
List of roles
Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
1.3.10 修改postgres⽤户的访问密码并测试建库建表
PostgreSQL 数据库默认会创建⼀个postgres的数据库⽤户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这⾥设定为postgres.su - postgrespsql
# ALTER USER postgres WITH PASSWORD 'postgres';# select * from pg_shadow ;# create database hxl;# \\c hxl
project=# create table person(id integer, name text);project=# insert into person values (1, 'hxl');project=# select * from person;
1.3.11 配置postgresql允许远程访问
只需要修改data⽬录下的pg_hba.conf和postgresql.conf这两个⽂件:pg_hba.conf:配置对数据库的访问权限;
postgresql.conf:配置PostgreSQL数据库服务器的相应的参数vim /opt/postgresql-11.6/data/pg_hba.conf# IPv4 local connections:
host all all 127.0.0.1/32 trusthost all all 0.0.0.0/0 md5
重新加载配置⽂件su - postgres
pg_ctl -D /opt/postgresql-11.6/data reload
修改postgresql.conf
vim /opt/postgresql-11.6/data/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;修改该改参数需要重启动
pg_ctl -D /opt/postgresql-11.6/data -l /opt/postgresql-11.6/log/postgres.log stoppg_ctl -D /opt/postgresql-11.6/data -l /opt/postgresql-11.6/log/postgres.log start到这⾥主库已经按照好了,下⾯进⾏主库的配置
1.3.12 主从配置 1.3.12.1 创建同步账号
[postgres@localhost data]$ psqlpsql (11.6)
Type \"help\" for help.
postgres=# CREATE ROLE repl login replication encrypted password 'repl';CREATE ROLE
1.3.12.2 修改配置⽂件(pg_hba.conf)
在该⽂件最后添加如下两⾏:
host replication repl 192.168.1.0/24 md5host all repl 192.168.1.0/24 trust
1.3.12.3 修改配置⽂件(postgresql.conf)找到相应的参数进⾏如下配置修改
wal_level = replica ##这个是设置主为wal的主机archive_mode = on
archive_command = 'ssh 192.168.1.131 test ! -f /opt/postgresql-11.6/data/pg_archive/%f && scp %p 192.168.1.131:/opt/postgresql-11.6/data/pg_archive/%f'
max_wal_senders = 6 ##这个设置了可以最多有⼏个流复制连接,差不多有⼏个从,就设置⼏个wal_keep_segments = 10240 ##设置流复制保留的最多的xlog数⽬wal_sender_timeout = 60s ##设置流复制主机发送数据的超时时间
1.3.12.4 创建归档⽇志⽬录
mkdir -p /opt/postgresql-11.6/data/pg_archive
1.3.12.5 重启主库
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log restart1.3.12.6 检查归档是否传输到备库在主库上执⾏如下命令
psql -c \"select pg_switch_wal()\"或是执⾏
postgres=# select pg_switch_wal();可以看到已经⽣成了2个新的归档⽇志归档⽇志相应的传输到备库
1.4 从库安装
1.4.1 安装
从库的安装跟主库安装步骤⼀致,需要启动数据库
1.4.2 停掉从库
若从库的数据库已经在运⾏的话,事先将其停掉
[postgres@localhost data]$ pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log stopwaiting for server to shut down.... doneserver stopped
1.4.3 准备data⽬录
从库安装完成后,不初始化,若已经初始化,删除其data⽬录
若之前安装的pg有data⽬录的话需要将其删除掉,并创建⼀个空的相同的⽬录su - postgres
[postgres@localhost postgresql-11.6]$ cd /opt/postgresql-11.6[postgres@localhost postgresql-11.6]$ mv data bakdata[postgres@localhost postgresql-11.6]$ mkdir data
root⽤户下修改权限
chown -R postgres:postgres /opt/postgresql-11.6chmod 0700 /opt/postgresql-11.6/data
1.4.4 基础同步主库的数据⽂件
[postgres@localhost postgresql-11.6]$ pg_basebackup -RFp --progress -D /opt/postgresql-11.6/data -h 192.168.1.130 -p 5432 -U repl --passwordPassword:
113625/113625 kB (100%), 1/1 tablespace
可以看到data⽬录下的所有⽂件都同步过来了,使⽤了R参数会⽣成⼀个recovery.conf⽂件,下⾯我们直接修改该⽂件即可[postgres@localhost data]$ pwd/opt/postgresql-11.6/data
[postgres@localhost data]$ ls -al[postgres@localhost data]$ ls -1backup_labelbaseglobal
pg_archivepg_commit_tspg_dynshmempg_hba.confpg_ident.confpg_logicalpg_multixactpg_notifypg_replslotpg_serial
pg_snapshotspg_stat
pg_stat_tmppg_subtranspg_tblspcpg_twophasePG_VERSIONpg_walpg_xact
postgresql.auto.confpostgresql.confrecovery.conf
1.4.4 修改recovery.conf⽂件
前⾯的步骤已经⽣成了recovery.conf⽂件,该⽂件的内容是这样的
[postgres@localhost data]$ more recovery.conf standby_mode = 'on'
primary_conninfo = 'user=repl password=repl host=192.168.1.130 port=5432 sslmode=disable sslcompression=0target_session_attrs=any'
我们这⾥不做流复制, primary_conninfo先注释掉,修改该⽂件,内容如下:restore_command = 'cp /opt/postgresql-11.6/data/pg_archive/%f %p'standby_mode = on
1.4.5 修改从库postgresql.conf⽂件修改如下内容项:
max_connections = 1000 #⼀般查多于写的应⽤从库的最⼤连接数要⽐较⼤hot_standby = on #说明这台机器不仅仅是⽤于数据归档,也⽤于数据查询max_standby_streaming_delay = 30s #数据流备份的最⼤延迟时间
wal_receiver_status_interval = 1s #多久向主报告⼀次从的状态,当然从每次数据复制都会向主报告状态,这⾥只是设置最长的间隔时间hot_standby_feedback = on #如果有错误的数据复制,是否向主进⾏反馈
1.4.5 启动从库
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start
1.4.6 验证
1.4.6.1 查看同步情况
[postgres@localhost pg_wal]$ psqlpostgres=# \\x
Expanded display is on.
postgres=# select * from pg_stat_replication;(0 rows)
这⾥不是流复制,所有没有显⽰
1.4.6.2 主库尝试创建对象看是否同步到从库psql -h localhost -U uhxl -d hxlcreate table tb_test(
id bigserial primary key not null, name varchar(64));
insert into tb_test(name) values('name1');insert into tb_test(name) values('name2');insert into tb_test(name) values('name3');insert into tb_test(name) values('name4');insert into tb_test(name) values('name5');
查看从库是否同步
这种归档模式的同步需要主库上执⾏检查点切换后,主库的数据才会同步到从库或是⼿⼯进⾏检查点的切换select pg_switch_wal();
为啥要切换⽣成⽇志,因为架设的是基于⽂件的备库,只有归档传输到备库后才会应⽤,否则记录还在主库的xlog中.
因篇幅问题不能全部显示,请点此查看更多更全内容