postgres主从配置
运维开发技术交流群欢迎大家加入一起学习(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=#
***测试没问题哈,这个也是意料中的事情!如果上述博文有什么问题,希望大神读客指出来,欢迎在线批评!!!***
推荐阅读
-
acer aspire 4520 phpini中的php-520配置指令详解
-
php.ini 配置文件的深入解析
-
高配置数据库!戴尔T610送PNY 8GB优盘
-
Surge配置生成器 anti surge surge tank 倩碧moisture surge
-
实战在Ubuntu上配置Nginx+PHP+MySQL环境
-
nginx+tomcat配置https最简单方案
-
解读phpMyAdmin配置的具体方法
-
国际化资源文件配置
-
WINDOWS下php5.2.4+mysql6.0+apache2.2.4+ZendOptimizer-3.3.0配置
-
eclipse 运行php时,运行配置,和调试配置的设置