天天看点

Postgresql 10 流复制配置过程-实测

我是用的两个NanoPI装的armbian系统,就是一个小型的ubuntu小型系统,真是太完美了,就是芯片温度有些高,完全可以当是做UBUNTU最小系统来使用,要用什么也都可以apt-get就可以搞定,这是后话了(-_-)

所以用了两台小板儿作了这么个postgresql流复制玩,该操作对于普通LINUX同样适用,因为我的Postgresql是下载的源码进行的安装,安装过程很简单,待再另起一个文档吧,等整理完放这上一个链接

我也是初学都,所以在网上参考了好多设置方法,最后多次测试后,终于设置成功了,而且主从切换也测试完成,10版本的在来回切换时会自动同步数据,不需要之前那样自制时间线什么的操作了,写下一是记录,二是有需要的可以参考,谢谢

一 主备机器规划

主机名 | IP | 角色 | 端口

:----:|:----:|:----:|:----:|:----:|:----:

NanoPI-005|10.10.10.205|Master|5432

NanoPI-006|10.10.10.206|Slave|5432

前提:分别在两台主机上安装好pg数据库,我是使用用的源码编译安装的10版本。

二 创建流复制

2.1 设置host

master,slave两节点都要操作。

[[email protected] ~]# vim /etc/hosts

#编辑内容如下:

10.10.10.205 NanoPI-005

10.10.10.206 NanoPI-006

2.2 在主库设置

2.2.1先初始化新数据库

[email protected]:~$initdb -D ~/data/

2.2.2启动数据库并建立同步用户

[email protected]:~$pg_ctl -D ~/data/ -l ~/log/pglog.log start

[email protected]:~$psql

postgres=#create role 同步用的用户名 login replication encrypted password '密码';

CREATE ROLE

postgres=#\q --退出psql

2.2.3配置~/data/pg_hba.conf

添加下面内容

host replication 在数据库里创建的同步用的用户名 备库IP地址或域名/32 trust

2.2.4配置~/data/postgres.conf

查找并修改成以下内容

改监控端口:

Listen_adresses = ‘*’

wal_level = hot_standby /10以后的版本为replica 主从设置为热血模式,流复制必选

max_wal_senders=2 /默认是10也可以 流复制允许连接进程,最好多点,我2没成,设置成10好了

wal_keep_segments =64

max_connections = 100 默认参数,非主从配置相关参数,表示到数据库的连接数

2.2.5重启主库服务,以更新配置

[email protected]:~$pg_ctl -D ~/data/ -l ~/log/pglog.log restart

2.3 在从库设置

2.3.1不需要初始化,直接从主库备份就行,如有DATA直接删掉或改名掉

[email protected]:~$pg_basebackup -h 主库地址10.10.10.205 -p 5432 -U 数据库中创建的同步用的用户名 -F p -P -D ~/data/

可能得输入密码

备注:

-h,主库主机,-p,主库服务端口;

-U,复制用户;

-F,p是默认输出格式,输出数据目录和表空间相同的布局,t表示tar格式输出;

-P,同--progress,显示进度;

-D,输出到指定目录;

2.3.2从库修改配置文件

[email protected]:~$vi ~/data/postgresql.conf

注释掉以下内容

wal_level,

max_wal_senders 

wal_keep_segments等参数

打开以下内容

hot_standby = on   #在备份的同时允许查询

max_standby_streaming_delay = 30s #可选,流复制最大延迟

wal_receiver_status_interval = 10s #可选,从向主报告状态的最大间隔时间

hot_standby_feedback = on #可选,查询冲突时向主反馈

max_connections = 1000 #默认参数,非主从配置相关参数,表示到数据库的连接数,一般从库做主要的读服务时,设置值需要高于主库

2.3.3配置~/data/pg_hba.conf

添加下面内容

host replication 在数据库里创建的同步用的用户名 主库IP地址或域名/32 trust或md5

#在从库中维护的主库IP地址是为了以后切换使用

2.3.4创建恢复文件recovery.conf

[email protected]:~$cp /usr/local/postgres/share/recovery.conf.sample ~/data/recovery.conf

修改文件中的参数

[email protected]:~$vi ~/data/postgresql.conf

recovery_target_timeline = 'latest'

standby_mode = on

primary_conninfo = 'host=主库地址10.10.10.205 port=5432 user=数据库中创建的同步用的用户名 password=密码'

备注:

vim ~/data/recovery.conf    #在做基础备份时,也可通过-R参数在备份结束后自动生产一个recovery.conf文件

standby_mode = on  #指明从库身份

primary_conninfo = 'host=10.10.10.205 port=5432 user=同步用的用户名 password=密码'  #连接到主库信息

recovery_target_timeline = 'latest'     #同步到最新数据

指定触发文件,文件存在时,将触发从库提升为主库,前提是必须设置”standby_mode = on”;如果不设置此参数,也可采用”pg_ctl promote“触发从库切换成主库

#trigger_file = ‘/postgres/data/trigger_activestandby’

因为主库采用的是md5认证,这里需要密码认证。

2.3.5启动从库数据服务

[email protected]:~$pg_ctl -D ~/data/ -l ~/log/pglog.log start

2.4 验证主从配置

2.4.1查看主库sender进程

[email protected]:~$ps -ef|grep postgres

查看有wal sender process 数据库中创建的同步用的用户名 从库地址

2.4.2查看从库sender进程

[email protected]:~$ps -ef|grep postgres

查看有wal receiver process

2.4.3用SQL看主从状态

[email protected]:~$psql

postgres=#select * from pg_stat_replication;

主库会显示sync_state为async 地址是从库地址

从库查状态无记录

2.5 流复制数据同步测试

分别启动master,slave数据库

在master上创建一个数据库和临时表

[email protected]:~$ psql

psql (9.6.1)

Type "help" for help.

postgres=# \password  #创建数据库密码

#创建测试数据库

postgres=# create database test;

CREATE DATABASE

postgres=# \c test

You are now connected to database "test" as user "postgres".

test=# create table tt(id serial not null,name text);

CREATE TABLE

test=# insert into tt(name) values ('china');

INSERT 0 1

在slave上查询刚才创建的表和数据,判定是否有数据同步

[email protected]:~$ psql

psql (9.6.1)

Type "help" for help.

postgres=# \c test

You are now connected to database "test" as user "postgres".

test=# select * from tt;

 id | name  

----+-------

  1 | china

(1 row)

2.6 主从切换方式

2.6.1主库备库状态查看

主库

[email protected]:~$ pg_controldata | grep 'Database cluster state'

Database cluster state:               in production

备库

[email protected]:~$ pg_controldata | grep 'Database cluster state'

Database cluster state:               in archive recovery

2.6.2停主库服务

[email protected]:~$ pg_ctl stop -m fast

waiting for server to shut down.... done

server stopped

[email protected]:~$ pg_controldata | grep 'Database cluster state'

Database cluster state:               shut down

2.6.3激活备库

[email protected]:~$ pg_ctl promote

waiting for server to promote...... done

server promoted

[email protected]:~$ pg_controldata | grep 'Database cluster state'

Database cluster state:               in production

2.6.4测试在激活的备库中写入数据

[email protected]:~$ psql

psql (10.3)

Type "help" for help.

postgres=# \d

        List of relations

 Schema | Name | Type  |  Owner

--------+------+-------+----------

 public | test | table | postgres

(1 row)

postgres=# select * from test;

 id | name  | salary

----+-------+--------

  2 | LiShi |  12000

(1 row)

postgres=# insert into  test values (1,'Hallo',20000);

INSERT 0 1

postgres=# select * from test;

 id | name  | salary

----+-------+--------

  2 | LiShi |  12000

  1 | Hallo |  20000

(2 rows)

postgres=#\q

[email protected]:~$ ll data/recovery*

-rw-r--r-- 1 postgres postgres 5824 Apr 24 14:19 data/recovery.done

#原recovery.conf自动变成了recovery.done

2.6.5模拟此时原主库已修复,需增加文件recovery.conf

vi data/recovery.conf

standby_mode='on'

recovery_target_timeline = 'latest'

primary_conninfo='host=当前主库(也就是以前从库)的IP地址 port=5432 user=同步用的用户名 password=密码'

#例如:primary_conninfo='host=10.10.10.206 port=5432 user=repl password=repl1234'

#保存后启动原主库,此时变为了现备库的状态

[email protected]:~$ pg_ctl -D data -l log/pglog.log start

waiting for server to start.... done

server started

#查看刚启动的原主库状态,已经变成备库

[email protected]:~$ pg_controldata | grep 'Database cluster state'

Database cluster state:               in archive recovery

#查看新备库中数据是否与现主库内容相同,9.6以后的版本应该会自动同步差异

[email protected]:~$ psql

psql (10.3)

Type "help" for help.

postgres=# \d

        List of relations

 Schema | Name | Type  |  Owner

--------+------+-------+----------

 public | test | table | postgres

(1 row)

postgres=# select * from test;

 id | name  | salary

----+-------+--------

  2 | LiShi |  12000

  1 | Hallo |  20000

(2 rows)

继续阅读