1、安装环境
[root@pg_master ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
主IP: hostname:pg_master ip:192.168.109.232
从IP: hostname:pg_standby ip:192.168.109.233
关闭防火墙: service iptables status
service iptables stop
2、主从节点安装Postgresql
只显示在主节点安装的信息,从节点操作一致
下载安装:
[root@pg_master ~]# yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6.8-x86_64/pgdg-centos96-9.6-3.noarch.rpm
**************安装下载信息,成功后显示如下***********************
Total size: 2.7 k
Installed size: 2.7 k
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : pgdg-centos96-9.6-3.noarch 1/1
Verifying : pgdg-centos96-9.6-3.noarch 1/1
Installed:
pgdg-centos96.noarch 0:9.6-3
Complete!
创建用户组并安装客户端:
[root@pg_master ~]# groupadd postgres
[root@pg_master ~]# useradd -g postgres postgres
[root@pg_master ~]# passwd postgres
Changing password for user postgres.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
[root@pg_master ~]# yum install postgresql96
*********************下载安装信息,成功后显示如下******************************
Total download size: 1.7 M
Installed size: 8.4 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): postgresql96-9.6.10-1PGDG.rhel6.x86_64.rpm | 1.4 MB 00:03
(2/2): postgresql96-libs-9.6.10-1PGDG.rhel6.x86_64.rpm | 289 kB 00:00
-----------------------------------------------------------------------------------------------------------
Total 268 kB/s | 1.7 MB 00:06
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : postgresql96-libs-9.6.10-1PGDG.rhel6.x86_64 1/2
Installing : postgresql96-9.6.10-1PGDG.rhel6.x86_64 2/2
Verifying : postgresql96-libs-9.6.10-1PGDG.rhel6.x86_64 1/2
Verifying : postgresql96-9.6.10-1PGDG.rhel6.x86_64 2/2
Installed:
postgresql96.x86_64 0:9.6.10-1PGDG.rhel6
Dependency Installed:
postgresql96-libs.x86_64 0:9.6.10-1PGDG.rhel6
Complete!
安装服务器
[root@pg_master ~]# yum install postgresql96-server
Total download size: 5.0 M
Installed size: 19 M
Is this ok [y/N]: y
Downloading Packages:
postgresql96-server-9.6.10-1PGDG.rhel6.x86_64.rpm | 5.0 MB 00:06
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : postgresql96-server-9.6.10-1PGDG.rhel6.x86_64 1/1
Verifying : postgresql96-server-9.6.10-1PGDG.rhel6.x86_64 1/1
Installed:
postgresql96-server.x86_64 0:9.6.10-1PGDG.rhel6
Complete!
初始化数据库并启动数据
[root@pg_master ~]# service postgresql-9.6 initdb
Initializing database: [ OK ]
[root@pg_master ~]# chkconfig postgresql-9.6 on
[root@pg_master ~]# service postgresql-9.6 start
Starting postgresql-9.6 service: [ OK ]
目前为止主从节点postgresql安装完毕!(从节点同样按上述步骤进行)
3、主从配置
主节点配置:
[root@pg_master ~]# su - postgres
[postgres@pg_master ~]$ psql
psql (9.6.10)
Type "help" for help.
--用户名密码可自行根据情况配置
postgres=# create role repl(用户名) login replication encrypted password 'passwd'(密码);
--配置pg_hba.conf文件
postgres=# \q
[postgres@pg_master ~]$ vi /var/lib/pgsql/9.6/data/pg_hba.conf
--新增如下配置
# TYPE DATABASE USER ADDRESS METHOD
host replication repl 192.168.109.0/24 md5
host all repl 192.168.109.0/24 trust
host all all 0.0.0.0/0 md5
--配置postgresql.conf文件
[postgres@pg_master ~]$ vi /var/lib/pgsql/9.6/data/postgresql.conf
# - Connection Settings -
listen_addresses = '*' #允许所有端口访问(可自行设置ip)# what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)
max_connections =512 #从库的 max_connections要大于主库 (change requires restart)
wal_level = hot_standby #热备模式
max_wal_senders= 6 #可以设置最多几个流复制链接,差不多有几个从,就设置多少
wal_keep_segments = 10240 #重要配置
wal_sender_timeout = 60s #有些是wal_send_timeout,稍微注意下
archive_mode = on #允许归档
archive_command = 'cp %p /var/lib/pgsql/9.6/data/pg_archive/%f' #根据实际情况设置
--建立归档文件和日志文件
[postgres@pg_master ~]$ mkdir /var/lib/pgsql/9.6/data/pg_archive
[postgres@pg_master ~]$ chown -R postgres.postgres /var/lib/pgsql/9.6/data/pg_archive
[postgres@pg_master ~]$ mkdir /var/log/pgsql-log
[postgres@pg_master ~]$ chown -R postgres.postgres /var/log/pgsql-log
[postgres@pg_master ~]$ service postgresql-9.6 restart
从节点配置:
--同步
[root@pg_standby ~]# su - postgres
[postgres@pg_standby ~]$ rm -rf /var/lib/pgsql/9.6/data/*
[postgres@pg_standby ~]$ pg_basebackup -h 192.168.109.232 -U repl -D /var/lib/pgsql/9.6/data -X stream -P
--修改配置文件
[postgres@pg_standby ~]$ cp /usr/pgsql-9.6/share/recovery.conf.sample /var/lib/pgsql/9.6/data/recovery.conf
[postgres@pg_standby ~]$ vi /var/lib/pgsql/9.6/data/recovery.conf
--增加以下几行
standby_mode = on
primary_conninfo = 'host=192.168.109.232 port=5432 user=repl password=passwd'
trigger_file = '/var/lib/pgsql/9.6/data/trigger.kenyon'
recovery_target_timeline = 'latest'
[postgres@pg_standby ~]$ vi /var/lib/pgsql/9.6/data/postgresql.conf
# - Connection Settings -
listen_addresses = '*' ##允许所有端口访问(可自行设置ip) what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)
max_connections =1024 # #从库的 max_connections要大于主库(change requires restart)
wal_level = hot_standby #热备模式
hot_standby = on #说明这台机器不仅仅用于数据归档,也用于查询
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s #多久向主报告一次从的状态。
hot_standby_feedback = on #如果有错误的数据复制,是否向主进行范例
--重启
[postgres@pg_standby ~]$ service postgresql-9.6 restart
4、主从配置情况检验
--主库查看
[root@pg_master ~]# su - postgres
[postgres@pg_master ~]$ psql
psql (9.6.10)
Type "help" for help.
postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
-----------------+------------
192.168.109.233 | async
(1 row)
postgres=# select * from pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | b
ackend_start | backend_xmin | state | sent_location | write_location | flush_location | replay_
location | sync_priority | sync_state
------+----------+---------+------------------+-----------------+-----------------+-------------+----------
---------------------+--------------+-----------+---------------+----------------+----------------+--------
---------+---------------+------------
4777 | 16384 | repl | walreceiver | 192.168.109.233 | | 42860 | 2018-09-1
1 14:17:17.697104+08 | 1680 | streaming | 0/4016BA0 | 0/4016BA0 | 0/4016BA0 | 0/4016B
A0 | 0 | async
(1 row)
--主库创建一个数据库
postgres=# create database test;
CREATE DATABASE
postgres=# \l // 查看当前所有数据库
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
--查看从库中是否已经同步
[postgres@pg_standby ~]$ psql
psql (9.6.10)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
参考:https://blog.csdn.net/weixin_41048363/article/details/80310285
https://www.2cto.com/database/201712/704490.html