Postgresql Replication实验记录
此文以Postgresql 10版本为例!
如未指定,下述命令在所有节点执行!
系统资源及组件规划
节点名称 | 系统名称 | cpu/内存 | 网卡 | 磁盘 | IP地址 | OS | 节点角色 |
---|---|---|---|---|---|---|---|
PGsql1 | pgsql1 | 2C/4G | ens33 | 128G | 192.168.0.10 | CentOS7 | Master |
PGsql2 | pgsql2 | 2C/4G | ens33 | 128G | 192.168.0.20 | CentOS7 | Slave |
二、系统软件安装与设置
1、安装基本软件
yum -y install vim lrzsz bash-completion
2、设置名称解析
echo 192.168.0.10 pgsql1 >> /etc/hosts
echo 192.168.0.20 pgsql2 >> /etc/hosts
3、设置NTP
yum -y install chrony
systemctl start chronyd
systemctl enable chronyd
systemctl status chronyd
chronyc sources
4、设置SELinux、防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
三、搭建Postgresql高可用集群
1、安装Postgresql
配置YUM源:
参考地址:https://www.postgresql.org/download
yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
安装Postgresql:
yum -y install postgresql10-server
配置环境变量:
su - postgres
export PATH=$PATH:/usr/pgsql-10/bin
2、配置Postgresql主节点
初始化PGsql1节点:
/usr/pgsql-10/bin/postgresql-10-setup initdb
修改/var/lib/pgsql/10/data/postgresql.conf:
listen_addresses = '*'
wal_level = replica
synchronous_commit = on
修改/var/lib/pgsql/10/data/pg_hba.conf,添加如下内容:
# IPv4 local connections:
host all all 0.0.0.0/0 md5
# replication privilege.
host replication repluser 192.168.0.0/24 md5
启动Postgresql,并设置自启动:
systemctl start postgresql-10
systemctl enable postgresql-10
systemctl status postgresql-10
su - postgres
psql
ALTER USER postgres WITH ENCRYPTED PASSWORD '111111';
\du
\q
su - postgres
psql
CREATE USER repluser WITH REPLICATION PASSWORD '111111';
\du
\q
su - postgres
psql
CREATE DATABASE db;
\c db
CREATE TABLE tb (
id int NOT NULL,
name varchar(255) NULL,
PRIMARY KEY (id)
);
在PGsql1节点上插入数据:
INSERT INTO tb (id,name) VALUES (1,‘MysqL’);
在PGsql1节点上查看数据:
SELECT * FROM tb;
\q
2、配置Postgresql从节点
备份PGsql1节点数据:
su - postgres
pg_basebackup -h pgsql1 -U repluser -D /var/lib/pgsql/10/data -P -v
配置PGsql2节点:
cp /usr/pgsql-10/share/recovery.conf.sample /var/lib/pgsql/10/data/recovery.conf
修改/var/lib/pgsql/10/data/recovery.conf,添加如下内容:
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=192.168.0.10 port=5432 user=repluser password=111111'
启动Postgresql,并设置自启动:
systemctl start postgresql-10
systemctl enable postgresql-10
systemctl status postgresql-10
4、验证Postgresql Replication
su - postgres
psql
\c postgres
SELECT * FROM pg_stat_replication;
\q
在PGsql2节点上查看数据:
su - postgres
psql
\c db
SELECT * FROM tb;
\q
在PGsql1节点上插入数据:
su - postgres
psql
\c db
INSERT INTO tb (id,name) VALUES (2,'Redis');
SELECT * FROM tb;
\q
在PGsql2节点上查看数据:
su - postgres
psql
\c db
SELECT * FROM tb;
\q
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。