此文以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 |
yum -y install vim lrzsz bash-completion
echo 192.168.0.10 pgsql1 >> /etc/hosts echo 192.168.0.20 pgsql2 >> /etc/hosts
yum -y install chrony
systemctl start chronyd systemctl enable chronyd systemctl status chronyd
chronyc sources
systemctl stop firewalld systemctl disable firewalld setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
配置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
修改.bash_profile,添加如下内容:
export PATH=$PATH:/usr/pgsql-10/bin
初始化PGSQL1节点:
/usr/pgsql-10/bin/postgresql-10-setup initdb
在PGSQL1节点上配置远程登录和复制权限:
修改/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
在PGSQL1节点上修改数据库密码:
su - postgres psql ALTER USER postgres WITH ENCRYPTED PASSWORD '111111'; \du \q
在PGSQL1节点上创建复制用户:
su - postgres psql CREATE USER repluser WITH REPLICATION PASSWORD '111111'; \du \q
在PGSQL1节点上创建数据库和表:
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
备份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
在PGSQL1节点上查看PGSQL2节点状态:
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