2 Star 8 Fork 5

小老虎/go-mysql-postgres

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

go-mysql-postgres 是通过解析binlog日志增量更新到postgresql数据库的一个服务

##安装

##如何使用

  • 在postgresql数据库上创建对应同步的表
  • 配置river.toml 文件,可以参考./etc/river.toml
  • 配置好mysql源,[Source]
  • 配置好mysql->postgresql 映射表和同步规则
  • 开启 ./bin/go-mysql-postgres -config=./etc/river.toml
  • var 目录下配置mysql-binlog 信息
  • var/master.info
  • bin_name="mysql-bin.000001"
  • bin_pos=515

##注意事项

  • binlog 格式必须是row。
  • binlog row image 必须是full。
  • 由于mysql和pg数据库alter 语法有点不一样。所以先修改pg数据库表结构。
  • mysql 表必须要有主键。
  • mysql 账号要有读取binlog的权限

Source

在go-mysql-postgres,你必须要定义好同步的表

[[source]]
schema = "test"
tables = ["t1", t2]

[[source]]
schema = "test_1"
tables = ["t3", t4]

schema是mysql库名,tables 是要同步的表

如果你要同步所有库下面的表,你可以用 *

[[source]]
schema = "test"
tables = ["*"]

Target

由于pg db 相当于 mysql的实例因此定义pg目标是可以取别名pg_name。用于rule同步到哪个db下schema
[[target]]
pg_name = "sync_db1"
pg_host = "192.168.1.10"
pg_port = 5432
pg_user = "postgres"
pg_pass = "postgres"
pg_dbname = "db1"

Rule

[[rule]]
schema = "db1"
table = "t1"
pg_schema = "public"
pg_table = "t1"
#skip_actions available values ["insert", "delete","update"]
#skip_actions = ["delete"]
#
#skip_alter_actions available values ["ADD", "DROP","ALTER"]
skip_alter_actions = ["ALTER","DROP","ADD"]
data_routers=[
{field_filters={type="0"},target={data_source="sync_db1",schema_name="public",table_name="t1"}},
{field_filters={type="1"},target={data_source="sync_db1",schema_name="public",table_name="t1"}},
{field_filters={type="3"},target={data_source="sync_db1",schema_name="public",table_name="t2"}}
]
一个完整配置rule
如果你只有一个target,参考river.toml配置,可以不用配置pg_name,如果你有多个配置源,那就要配置pg_name
如果你要做冷热分离处理,可以过滤掉delete操作
由于mysql alter语法和pg alter语法稍有区别,所以建议你配置过滤。
要改表结构,先修改pg数据库的,然后在修改mysql库中的表结构
如果你的表中有个字段是status,希望status 更改后,映射到另一个表,可以添加data_routers
他的原理是先删除在插入

1、添加自动映射主键
2、添加正则表达式同步表
3、添加对json格式的支持 
4、添加对字段类型转换 varchar->array 解决mysql find_in_set 查询慢的问题

var

在var目录下有2个文件,一个是app.pid 一个是master.info
bin_name = "mysql-bin.1"
bin_pos = 1

这是同步binlog日志信息。

监控

http://ip:12800/stat
可以通过这个URL 监控服务是否正常
在部署该服务上安装 statsd
配置文件
{
  graphitePort: 2013   #graphite端口号
, graphiteHost: "xxx"  #graphite主机ip
, address: "127.0.0.1"
, mgmt_address: "127.0.0.1"
, deleteGauges: true
, port: 8125
, backends: [ "./backends/graphite" ]
, flushInterval: 30000
, graphite:{globalSuffix: "ip"} #go-mysql-postgres主机ip
}
The MIT License (MIT) Copyright (c) 2015 siddontang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

通过解析mysql binlog 日志实时同步到postgresql数据库中 展开 收起
README
MIT
取消

发行版 (1)

全部
4年前

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/tangjunhu/go-mysql-postgres.git
git@gitee.com:tangjunhu/go-mysql-postgres.git
tangjunhu
go-mysql-postgres
go-mysql-postgres
master

搜索帮助