postgres - 设置master slave standby 主从服务器 -

访问量: 89

参考 https://linuxconfig.org/how-to-create-a-hot-standby-with-postgresql

1. 先安装 postgres

2. 配置主服务器, (MASTER)

修改配置文件:

/etc/postgresql/9.5/main/pg_hba.conf

增加下面这一行:

host replication postgres 39.140.xx.yy md5

想测试postgres的配置文件是否正确,没有nginx -t 这样的方法。 需要

postgres=# SELECT pg_reload_conf();
 pg_reload_conf
----------------
 t
(1 row)

postgres=# 

如果没报错,就说明没问题。 否则会报错。

3. 继续修改配置文件

wal_level = hot_standby
synchronous_commit = local
archive_mode = on
archive_command = 'cp %p /var/lib/postgresql/9.5/main/archive/%f'
listen_addresses = '*'
max_wal_senders = 2
wal_keep_segments = 10
hot_standby = on
# seems useless
synchronous_standby_names = 'pgmaster001'

4. 重启( 记得不能使用reload,  也不能使用postgres用户,要使用root用户的权限 )

$ sudo service postgresql stop

$ sudo service postgresql start 

记得多看看  tail /var/log/postgresql/post..main.log -f

也使用 service postgresql status 来查看状态

5. 配置slave 服务器

先把slave关掉。

wal_level = hot_standby
synchronous_commit = local
listen_addresses = '*'
max_wal_senders = 2
wal_keep_segments = 10
hot_standby = on
# seems useless
synchronous_standby_names = 'pgslave001'

然后,

$ cd /var/lib/postgresql/9.5

$ mv main main_0419

$ mkdir main  

$ chmod 700 main 

$ chown postgres:postgres main

运行备份数据的命令:

postgres@database:/root$ pg_basebackup -h 52.82.xx.yy -D /var/lib/postgresql/9.5/main -U postgres -v -P
Password: 
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
15944876/15944876 kB (100%), 1/1 tablespace                                         
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
pg_basebackup: base backup completed

搞定后,编辑新文件:

$ vim /var/lib/postgresql/9.5/main/recovery.conf

standby_mode = 'on'
primary_conninfo = 'host=52.82.xx.yy port=5432 user=postgres password=postgres application_name=pgslave001'
restore_command = 'cp /var/lib/postgresql/9.5/main/archive/%f %p'
trigger_file = '/tmp/postgresql.trigger.5432'

chmod 600 recovery.conf

有时候需要 ulimit -s 7000 这样的设置

然后重启 postgres.  记得  tail /var/log/postgresxx.log

使用该slave

pg_ctl promote

订阅/RSS Feed

Subscribe

分类/category