Erlo

postgres主从配置

2019-05-24 12:00:19 发布   166 浏览  
页面报错/反馈
收藏 点赞

运维开发技术交流群欢迎大家加入一起学习(QQ:722381733)

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

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

主:192.168.254.21

从:192.168.254.22

 

主配置(192.168.254.21)

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

 

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

 

  2、启动服务

[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 ~]$ 

  3、进入postgres数据库,创建用户并授权

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

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

  4、检查所以库用户后退出

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 ~]$ 

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

[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]$ 

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

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

  7、打开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

  8、保存退出后重启主服务器上的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)

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

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

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

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

  3、拷贝主数据库中的数据,注意指定的目录,如果新建了目录就指定到新建的目录。

[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)***

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

[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]$ 

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

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

  6、编辑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    #如果有错误的数据复制,是否向主进行反馈

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

[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]$ 

 

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

 

测试

  主:1、psql   #进入数据器

      2、l   #查看所有库

      3、create database test;   #新建库

      4、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=# 

    

  从:1、psql   #进入数据库
     2、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=# 

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

 

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认