《童虎学习笔记》10分钟精通PostgreSQL双主方案bucardo

  本文章配套视频 https://www.ixigua.com/7077056019024904717?id=7074382473295200776
本专栏全部文章 https://blog.csdn.net/tonghu_note/category_11713514.html
总目录 https://blog.csdn.net/tonghu_note/article/details/124333034

我的dou音 aa10246666, 看配套视频


一、场景

node1 pg 14.2 10.211.55.9,
node2 pg 14.2 10.211.55.4
bucardo 5.6.0 10.211.55.6

                        搭建 node1 的 d1 与 node2 的 d2 的双主同步


二、安装及配置

node1、node2节点安装好 PG 14.2

bucardo节点不用先装PG ,它会在安装bucardo时默认安装PG 9.6

1、在node1节点操作:

创建数据库、表、用户

create database d1;
create table t12(id int primary key);
create user bucardo superuser PASSWORD '111';

修改配置文件

postgresql.conf

listen_addresses = '*'

pg_hba.conf

host    all             bucardo         10.211.55.6/32          trust

重启pg

2、在node2节点操作:

创建数据库、表、用户

create database d2;
create table t12(id int primary key);
create user bucardo superuser PASSWORD '111';

修改配置文件

postgresql.conf

listen_addresses = '*'

pg_hba.conf

host    all             bucardo         10.211.55.6/32          trust

重启pg

3、在bucardo节点操作:

apt-get install bucardo

新建管理员用户

CREATE user bucardo superuser PASSWORD 'bucardo';

新建数据库

create database bucardo;

修改pg_hba文件,添加如下内容到local   all    all     peer这一行的上面

local   all             bucardo                                 trust

重启或reload配置文件

root用户下执行如下命令

bucardo install

root@localhost:~# bucardo install
This will install the bucardo database into an existing Postgres cluster.
Postgres must have been compiled with Perl support,
and you must connect as a superuser

Current connection settings:
1. Host:           localhost
2. Port:           5432
3. User:           bucardo
4. Database:       bucardo
5. PID directory:  /var/run/bucardo
Enter a number to change it, P to proceed, or Q to quit: 1

敲1后回车,目的是将Host改为,使连接数据库时走socket文件

Change the host to: 

Changed host to:
Current connection settings:
1. Host:          
2. Port:           5432
3. User:           bucardo
4. Database:       bucardo
5. PID directory:  /var/run/bucardo
Enter a number to change it, P to proceed, or Q to quit: P

敲P继续

Attempting to create and populate the bucardo database and schema
Database creation is complete

Updated configuration setting "piddir"
Installation is now complete.
If you see errors or need help, please email [email protected]

You may want to check over the configuration variables next, by running:
bucardo show all
Change any setting by using: bucardo set foo=bar

4. 在root用户下查看 bucardo 配置

bucardo show all


三、搭建 bucardo 多主复制(在bucardo服务器root用户下操作)

1. 在db1和db2上创建用户 bucardo

bucardo add db b_db1 dbhost=10.211.55.9 dbport=5432 dbuser=bucardo dbname=d1
bucardo add db b_db2 dbhost=10.211.55.4 dbport=5432 dbuser=bucardo dbname=d2

2. 先做 d1 -> d2 的同步

bucardo add all tables db=b_db1 --relgroup=b_repgrp1 --verbose
bucardo add sync b_sync1 relgroup=b_repgrp1 dbs=b_db1,b_db2

3. 再做 d2 -> d1 的同步

bucardo add all tables db=b_db2 --relgroup=b_repgrp2 --verbose
bucardo add sync b_sync2 relgroup=b_repgrp2 dbs=b_db2,b_db1

root用户下操作
4. 启动 bucardo,对应日志存储在 /var/log/bucardo/log.bucardo  (官方说是在当前目录下是错误的)

bucardo start

root用户下操作
5. 都配置好了,检查一下

bucardo list syncs
bucardo list dbgroups
bucardo list dbs
bucardo list tables
bucardo list sequences
bucardo list relgroups
bucardo status
bucardo status


四、测试双主同步(在bucardo服务器的postgres用户下操作)

1. 先看下数据情况,表是新建的,所以没有记录

psql -h 10.211.55.9 -p 5432 -U bucardo -d d1 -At -c 'select * from t12'
psql -h 10.211.55.4 -p 5432 -U bucardo -d d2 -At -c 'select * from t12'

2. 修改 node1的d1 数据,可同步到 node2的d2

psql -h 10.211.55.9 -p 5432 -U bucardo -d d1 -c 'insert into t12 select 1'

psql -h 10.211.55.9 -p 5432 -U bucardo -d d1 -At -c 'select * from t12'
sleep 1
psql -h 10.211.55.4 -p 5432 -U bucardo -d d2 -At -c 'select * from t12'

3. 修改 node2的d2 数据,可同步到 node1的d1

psql -h 10.211.55.4 -p 5432 -U bucardo -d d2 -c 'insert into t12 select 2'

psql -h 10.211.55.4 -p 5432 -U bucardo -d d2 -At -c 'select * from t12'

sleep 1

psql -h 10.211.55.9 -p 5432 -U bucardo -d d1 -At -c 'select * from t12'

你可能感兴趣的:(debian,运维,postgresql,数据库)