postgres主从配置

运维开发技术交流群欢迎你们加入一块儿学习(QQ:722381733)web

开始部署postgres主从若是没不会安装postgres的请去上一个博文中查看)sql

这里我使用了两台服务器部署数据库

主:192.168.254.21服务器

从:192.168.254.22运维

 

主配置(192.168.254.21)dom

  一、进入部署postgres服务的用户socket

 

[root@web1 ~]# su - postgres
Last login: Thu May 23 18:17:45 CST 2019 on pts/0
[postgres@web1 ~]$ 

 

  二、启动服务ide

[postgres@web1 ~]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log
waiting for server to start.... done
server started
[postgres@web1 ~]$ 

  三、进入postgres数据库,建立用户并受权post

[postgres@web1 ~]$ psql 
psql (10.5)
Type "help" for help.

postgres=# CREATE ROLE replica login replication encrypted password 'replica';
CREATE ROLE
postgres=# 

  四、检查因此库用户后退出学习

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 replica   | Replication                                                | {}
 test      | Superuser                                                  | {}

postgres=# \q
[postgres@web1 ~]$ 

  五、进入安装目录的库文件配置目录

[postgres@web1 ~]$ cd /usr/local/pgsql-10.5/data/
[postgres@web1 data]$ ls
base          pg_hba.conf             pg_multixact  pg_snapshots  pg_tblspc    pg_xact                     postmaster.opts
global        pg_hba.conf.2019-05-23  pg_notify     pg_stat       pg_twophase  postgresql.auto.conf        postmaster.pid
pg_commit_ts  pg_ident.conf           pg_replslot   pg_stat_tmp   PG_VERSION   postgresql.conf
pg_dynshmem   pg_logical              pg_serial     pg_subtrans   pg_wal       postgresql.conf.2019-05-23
[postgres@web1 data]$ 

  六、修改postgresql.conf配置文件,修改以下(修改时记得备份)

wal_level = hot_standby        #热备模式
max_wal_senders = 32         #这个设置了能够最多有几个流复制链接,差很少有几个从,就设置几个
wal_keep_segments = 10240   #设置流复制保留的最多的xlog数目(重要配置)
wal_sender_timeout = 60s    #设置流复制主机发送数据的超时时间
max_connections = 100         #这个设置要注意下,从库的max_connections必需要大于主库的

  七、打开pg_hba.conf配置文件,设置以下(修改时记得备份)

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
#host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0               md5     #全部ip均可以经过密码链接
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
host    replication     replica          192.168.254.22/32       md5

  八、保存退出后重启主服务器上的postgres库

[postgres@web1 data]$ pg_ctl restart -l /usr/local/pgsql-10.5/log/pg_server.log
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started
[postgres@web1 data]$ 

***在此主就配置完了,如今配置从服务器***

 

从配置(192.168.254.22)

  一、进入部署postgres服务的用户

[root@web2 ~]# su - postgres
Last login: Thu May 23 19:27:48 CST 2019 on pts/0
[postgres@web2 ~]$ 

  二、这里为了方便我直接删除data目录用于备份主服务的数据,若是有特殊的要求建议新建个data1,而后用来作从存储目录(线上项目禁用rm,可经过mv移到垃圾箱)

[postgres@web2 ~]$ rm /usr/local/pgsql-10.5/data/* -r
[postgres@web2 ~]$ 

  三、拷贝主数据库中的数据,注意指定的目录,若是新建了目录就指定到新建的目录。

[postgres@web2 ~]$ pg_basebackup -F p --progress -D /usr/local/pgsql-10.5/data/ -h 192.168.254.21 -p 5432 -U replica --password
Password: 
31380/31380 kB (100%), 1/1 tablespace
[postgres@web2 ~]$ 

***注这里要求输入密码,就是主数据库刚刚新建用户时建的密码(replica)***

  四、复制配置文件并更名,注意文件路径及文件名称

[postgres@web2 ~]$ cp /usr/local/pgsql-10.5/share/recovery.conf.sample /usr/local/pgsql-10.5/data/recovery.conf
[postgres@web2 ~]$ cd /usr/local/pgsql-10.5/data/
[postgres@web2 data]$ ls
backup_label  pg_dynshmem             pg_logical    pg_serial     pg_subtrans  pg_wal                postgresql.conf.2019-05-23
base          pg_hba.conf             pg_multixact  pg_snapshots  pg_tblspc    pg_xact               recovery.conf
global        pg_hba.conf.2019-05-23  pg_notify     pg_stat       pg_twophase  postgresql.auto.conf
pg_commit_ts  pg_ident.conf           pg_replslot   pg_stat_tmp   PG_VERSION   postgresql.conf
[postgres@web2 data]$ ls /usr/local/pgsql-10.5/data/ |grep recovery.conf
recovery.conf
[postgres@web2 data]$ 

  五、编辑recovery.conf文件,配置以下(修改是记得备份)

standby_mode = on    #说明这台postgres,为从库
primary_conninfo = 'host=192.168.254.21 port=5432 user=replica password=replica'    #配置要同步的ip及库用户还有密码
recovery_target_timeline = 'latest'    #流复制同步到最新的数据

  六、编辑postgresql.conf文件,配置以下

listen_addresses = '*'    #这表示监听的ip,这里我设置监听全部ip,也能够设置成从本机ip
wal_level = hot_standby        #注:这个名称要与主的一致
max_connections = 1000        #最大链接数要大于主的
hot_standby = on        #说明这台机器不单单用于数据归档,也用于查询 
max_standby_streaming_delay = 30s    #数据流备份最大延迟
wal_receiver_status_interval = 1s    #多久向主报告一次从的状态,固然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
hot_standby_feedback = on    #若是有错误的数据复制,是否向主进行反馈

  七、配置完成后启动从数据库服务

[postgres@web2 data]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log
waiting for server to start.... done
server started
[postgres@web2 data]$ 

 

***这里主从就配置完成啦,接下来就是测试***

 

测试

  主:一、psql   #进入数据器

      二、\l   #查看全部库

      三、create database test;   #新建库

      四、\l   #检查是否建立

[postgres@web1 data]$ psql 
psql (10.5)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 name      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 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
(4 rows)

postgres=# create database test;
CREATE DATABASE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 name      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 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 | 
(5 rows)

postgres=# 

    

  从:一、psql   #进入数据库
     二、\l   #查看库是否与主的一致

[postgres@web2 data]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log
waiting for server to start.... done
server started
[postgres@web2 data]$ psql 
psql (10.5)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 name      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 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 | 
(5 rows)

postgres=# 

***测试没问题哈,这个也是意料中的事情!若是上述博文有什么问题,但愿大神读客指出来,欢迎在线批评!!!***

相关文章
相关标签/搜索