|
| 1 | +## PostgreSQL 最佳实践 - 逻辑增量复制(MySQL <-> PgSQL <-> PgSQL) |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2016-05-26 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , RDS , alidecode , 逻辑复制 , 逻辑订阅 , Kafka |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +alidecode是RDS PG提供的一个逻辑复制插件,使用它,可以将RDS PG通过逻辑复制的方式,把数据同步到线下的PostgreSQL。 |
| 16 | + |
| 17 | +还可以将数据从PG同步到其他的数据处理平台,例如Kafka。 |
| 18 | + |
| 19 | +同时还支持将MySQL的数据同步到PostgreSQL。(使用SQL管理函数创建逻辑复制的SLOT,调用alidecode 函数,函数会从WAL日志解析出REDO,从而实现实时的数据变更拽取。) |
| 20 | + |
| 21 | +你还可用dbsync来实现平滑升级PostgreSQL的大版本,是不是很酷呢。 |
| 22 | + |
| 23 | +mysql2pgsql |
| 24 | + |
| 25 | +https://help.aliyun.com/document_detail/35458.html |
| 26 | + |
| 27 | +pgsql2pgsql |
| 28 | + |
| 29 | +https://help.aliyun.com/document_detail/35459.html |
| 30 | + |
| 31 | +源码 |
| 32 | + |
| 33 | +https://github.com/aliyun/rds_dbsync |
| 34 | + |
| 35 | +下面是简单的使用方法。 |
| 36 | + |
| 37 | +## 准备工作 |
| 38 | +提交工单,开放用户的replication角色。(目前RDS用户根账号已经默认带了replication权限,这个步骤可以忽略) |
| 39 | + |
| 40 | +``` |
| 41 | +postgres=# alter role digoal replication; |
| 42 | +ALTER ROLE |
| 43 | +``` |
| 44 | + |
| 45 | +阿里云的RDS PG需要将主备的pg_hba.conf进行修正,增加replication的条目。 |
| 46 | + |
| 47 | + |
| 48 | +## 例子 |
| 49 | + |
| 50 | +``` |
| 51 | +$ vi $PGDATA/pg_hba.conf |
| 52 | +host replication digoal 0.0.0.0/0 md5 |
| 53 | +``` |
| 54 | + |
| 55 | +阿里云的RDS PG需要调整主备的postgresql.conf,将wal_level改成logical。 |
| 56 | + |
| 57 | +并重启主备数据库,所以用户要开通此功能,需要重启实例哦 |
| 58 | + |
| 59 | +``` |
| 60 | +wal_level = logical |
| 61 | +``` |
| 62 | + |
| 63 | +用户需要在RDS管控平台,配置白名单,允许alidecode客户端所在的主机连接RDS数据库。 |
| 64 | + |
| 65 | + |
| 66 | +下载alidecode客户端。 |
| 67 | + |
| 68 | +安装postgresql, mysql。(需要用到头文件) |
| 69 | + |
| 70 | +如果你不需要将mysql的数据同步到PG,则不需要编译mysql的部分。 |
| 71 | + |
| 72 | +在Makefile和dbsync.h中注释掉mysql的部分即可。 |
| 73 | + |
| 74 | +编译前,你需要配置一下pgsync.cpp,这里需要配置三个连接串。 |
| 75 | + |
| 76 | +src对应RDS的连接串。 |
| 77 | + |
| 78 | +local对应的是一个中间库,它用来记录任务信息,记录全量同步时的增量数据(全量同步数据时,并行的接收xlog,接收的XLOG转义成SQL存在中间库)。 |
| 79 | + |
| 80 | +desc对应目标库,即数据要同步到这个库。 |
| 81 | + |
| 82 | +``` |
| 83 | +$ vi dbsync.cpp |
| 84 | + src = (char *)"host=digoal_111.pg.rds.aliyuncs.com port=3433 dbname=db1 user=digoal password=digoal"; |
| 85 | + local = (char *)"host=127.0.0.1 port=1925 dbname=db2 user=postgres password=postgres"; |
| 86 | + desc = (char *)"host=127.0.0.1 port=1925 dbname=db1 user=postgres password=postgres"; |
| 87 | +``` |
| 88 | + |
| 89 | +编译 |
| 90 | + |
| 91 | +``` |
| 92 | +$ make |
| 93 | +``` |
| 94 | + |
| 95 | +alidecode不负责DDL的同步,所以DDL需要用户自己操作 |
| 96 | + |
| 97 | +例子 |
| 98 | + |
| 99 | +``` |
| 100 | +/home/dege.zzz/pgsql9.5/bin/pg_dump -F p -s --no-privileges --no-tablespaces --no-owner -h digoal_111.pg.rds.aliyuncs.com -p 3433 -U digoal db1 | psql db1 -f - |
| 101 | +``` |
| 102 | + |
| 103 | +同步数据,执行dbsync就可以了 |
| 104 | + |
| 105 | +``` |
| 106 | +./dbsync |
| 107 | +full sync start 2016-05-26 15:35:42.336903, end 2016-05-26 15:35:42.699032 restart decoder sync |
| 108 | +decoder sync start 2016-05-26 15:35:42.337482 |
| 109 | +decoder slot rds_logical_sync_slot exist |
| 110 | +starting logical decoding sync thread |
| 111 | +starting decoder apply thread |
| 112 | +pg_recvlogical: starting log streaming at 0/0 (slot rds_logical_sync_slot) |
| 113 | +pg_recvlogical: confirming recv up to 0/0, flush to 0/0 (slot rds_logical_sync_slot) |
| 114 | +``` |
| 115 | + |
| 116 | +元数据记录在db2,如果失败要重新来过的话,建议清除它,同时清除目标库的已同步数据,然后重新调用dbsync。 |
| 117 | + |
| 118 | +``` |
| 119 | +db2=# \dt |
| 120 | + List of relations |
| 121 | + Schema | Name | Type | Owner |
| 122 | +--------+----------------+-------+---------- |
| 123 | + public | db_sync_status | table | postgres |
| 124 | + public | sync_sqls | table | postgres |
| 125 | +(2 rows) |
| 126 | + |
| 127 | +db2=# drop table db_sync_status ; |
| 128 | +DROP TABLE |
| 129 | +db2=# drop table sync_sqls ; |
| 130 | +DROP TABLE |
| 131 | +``` |
| 132 | + |
| 133 | +压测rds |
| 134 | + |
| 135 | +``` |
| 136 | +pgbench -M prepared -n -r -P 1 -c 80 -j 80 -T 100 -h digoal_111o.pg.rds.aliyuncs.com -p 3433 -U digoal db1 |
| 137 | +``` |
| 138 | + |
| 139 | +可以看到同步的过程 |
| 140 | + |
| 141 | +``` |
| 142 | +pg_recvlogical: confirming recv up to 1/4EE3F08, flush to 1/4EE3F08 (slot rds_logical_sync_slot) |
| 143 | +pg_recvlogical: confirming recv up to 1/4F8BA20, flush to 1/4F8BA20 (slot rds_logical_sync_slot) |
| 144 | +pg_recvlogical: confirming recv up to 1/5025228, flush to 1/5025228 (slot rds_logical_sync_slot) |
| 145 | +pg_recvlogical: confirming recv up to 1/50C6E68, flush to 1/50C6E68 (slot rds_logical_sync_slot) |
| 146 | +pg_recvlogical: confirming recv up to 1/51578A0, flush to 1/51578A0 (slot rds_logical_sync_slot) |
| 147 | +pg_recvlogical: confirming recv up to 1/51E7CF8, flush to 1/51E7CF8 (slot rds_logical_sync_slot) |
| 148 | +``` |
| 149 | + |
| 150 | +压测完后,查看数据是否一致 |
| 151 | + |
| 152 | +``` |
| 153 | +psql -h 127.0.0.1 db1 |
| 154 | +db1=# select sum(hashtext(t.*::text)) from pgbench_accounts t; |
| 155 | + sum |
| 156 | +--------------- |
| 157 | + -582104340143 |
| 158 | +(1 row) |
| 159 | + |
| 160 | +psql -h digoal_111o.pg.rds.aliyuncs.com -p 3433 -U digoal db1 |
| 161 | +psql (9.6beta1, server 9.4.1) |
| 162 | +Type "help" for help. |
| 163 | + |
| 164 | +db1=> select sum(hashtext(t.*::text)) from pgbench_accounts t; |
| 165 | + sum |
| 166 | +--------------- |
| 167 | + -582104340143 |
| 168 | +(1 row) |
| 169 | +``` |
| 170 | + |
| 171 | +db_sync工具下载地址 |
| 172 | + |
| 173 | +https://github.com/aliyun/rds_dbsync |
| 174 | + |
| 175 | +## 参考 |
| 176 | + |
| 177 | +https://github.com/aliyun/rds_dbsync |
0 commit comments