docker下centos7安装postgresql搭建主从同步(pglogical)

docker下创建centos7 容器

  • docker pull centos -- 抽去latest的 centos 镜像,此处镜像重命名为centos_j1
  • 创建容器 cent_pg1,cent_pg2,映射端口5300,5301
    docker run -d --privileged -v /sys/fs/cgroup:/sys/fs/cgroup -p 5300:5432 --name cent_pg1 centos_j1 /usr/sbin/init docker run -d --privileged -p 5301:5432 --name cent_pg2 centos_j1 /usr/sbin/init
  • 进入容器
    docker exec -it cent_pg1 /bin/bash

install postgresql

postgresl install
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm -- 下载rpm
yum install postgresql96 -- install client
yum install postgresql96-server postgresqyl96-contrib -- install sderver和第三方插件
/usr/pgsql-9.6/bin/postgresql96-setup initdb -- 初始化数据库
systemctl enable postgresql-9.6 --开机自启
systemctl start postgresql-9.6 -- 启动服务
passwd postgres -- 修改postgres password
create database testduliyan;--create database
create user super2 with superuser password '34567';
\c testduliyan --切换数据库
create table duliyan(id int primary key , name varchar(50));

install pglogical

下载地址
yum install pglogical-rhel-1.0-3.noarch.rpm
yum install postgresql96-pglogical

修改配置文件postgresql.conf

listen_addresses ='*'
port=5432
password_encryption = on
wal_level = 'logical'
max_worker_processes = 10 # one per database needed on provider node
max_replication_slots = 10 # one per node needed on provider node
max_wal_senders = 10 # one per node needed on provider node
shared_preload_libraries = 'pglogical'

修改pg_hba.conf

host all all 0.0.0.0/0 md5.
local replication super1 trust
host replication super1 127.0.0.1/32 trust
host replication super1 ::1/128 trust
host replication super1 0.0.0.0/0 trust

创建节点

参考网站
psql -U super1 -h 127.0.0.1 -d testduliyan -W -- login db
CREATE EXTENSION pglogical; -- create 扩展工具
select extname, extowner from pg_extension;

  • provider
    ifconfig 172.17.0.6 super1
    psql -U super1 -h 127.0.0.1 -d testduliyan -W
    select pglogical.create_node(node_name := 'provider1',dsn :='host=172.17.0.6 port=5432 dbname= testduliyan user= super1 password=23456');
    select pglogical.replication_set_add_all_tables('default',ARRAY['public']);
    ---或者自己建变更集
    select pglogical.create_replication_set('mytest'); --create replication set
    ----在变更集内添加表
    select pglogical.replication_set_add_table('mytest','fin_trad_flow', true);
    select pglogical.replication_set_add_table('default','fin_trad_flow');

  • subscriber
    ifconfig 172.17.0.7 super2
    psql -U super2 -h 127.0.0.1 -d testduliyan -W
    select pglogical.create_node(node_name := 'subscriber1',dsn :='host=172.17.0.7 port=5432 dbname=testduliyan user=super2 password=34567 ');
    select pglogical.create_subscription(subscription_name :='subscription1', provider_dsn := 'host=172.17.0.6 port =5432 dbname=testduliyan user=super1 password=23456');
    ---对于自己建的变更集如何设置订阅
    select pglogical.create_subscription(subscription_name :='subscription1', provider_dsn := 'host=172.17.0.9 port =5432 dbname=postgres user=postgres password=123456',replication_sets := '{fin_trad_flow}' );

测试

provider
insert into duliyan values(1,'aa') ;
subscriber
select * from duliyan;
subscriber 有数据,同步成功

注意: pglogical设置的表必须包含主键或者unique key。

你可能感兴趣的:(docker下centos7安装postgresql搭建主从同步(pglogical))