[英]How can i listen MySQL database changes in real time with Node.js or PHP
I need to create a system with multiple databases one of this is the master database this database need to replicate only structural changes to other databases for example: 我需要创建一个包含多个数据库的系统,其中之一是主数据库,该数据库只需将结构更改复制到其他数据库,例如:
When I register a new user in the system, the system creates automatically a structural replica of the master database but this database not send the inserted registers or updates to the master database only the master database when is updated send all structural changes to the slave databases, therefore i need to create a script or implement tool to capture the database updates to execute the updates on all slaves in real-time. 当我在系统中注册新用户时,系统会自动创建主数据库的结构副本,但此数据库不会将插入的寄存器或更新发送到主数据库,而只会在更新后将主数据库的所有结构更改发送到主数据库因此,我需要创建一个脚本或实施工具来捕获数据库更新,以实时在所有从属服务器上执行更新。
I sent a question to AWS support and they recommend me to implement a phyton script or integrate another library who allow doing a binlog streaming to replicate these changes to the slave's databases. 我向AWS支持人员发送了一个问题,他们建议我实施phyton脚本或集成另一个库,该库允许进行binlog流传输以将这些更改复制到从属数据库中。
AWS support answer: AWS支持答案:
You can follow this guide here[1], you can skip the Kinesis (AWS Service) part and have your code write directly instead of on putting it in the Kinesis stream.
您可以在此处遵循本指南[1],可以跳过Kinesis(AWS服务)部分,直接编写代码,而不是将其放入Kinesis流中。 You will need to enable binlog on your DB cluster and listen to the log.
您将需要在数据库集群上启用binlog并监听日志。 Depending on the events you can add in logic to perform DB updates on child databases.
根据事件,您可以添加逻辑以对子数据库执行数据库更新。 In order to replicate your master database schema, I would recommend using the mysqldump CLI tool to export the schema of your master database before any child databases need to provision and import that schema.
为了复制您的主数据库模式,我建议使用mysqldump CLI工具导出主数据库的模式,然后再配置任何子数据库。 Then use the binlog script to push changes to your child databases depending on your logic you have written.
然后根据您编写的逻辑,使用binlog脚本将更改推送到子数据库。
[1] https://aws.amazon.com/blogs/database/streaming-changes-in-a-database-with-amazon-kinesis/ [1] https://aws.amazon.com/blogs/database/streaming-changes-in-a-database-with-amazon-kinesis/
I solved my problem integrating Zongji an npm package, Zongji detects changes on binlog and capture the executed query, I did a script using this package to listen to the binlog events and apply these changes to the slave database, here I'll adjunct an example of my script. 我解决了将Zongji集成到npm软件包中的问题,Zongji在binlog上检测到更改并捕获了执行的查询,我使用此软件包做了一个脚本来侦听binlog事件并将这些更改应用于从属数据库,在这里我将举例说明我的剧本。
Zongji repository: https://github.com/nevill/zongji . Zongji资料库: https : //github.com/nevill/zongji 。
var ZongJi = require('zongji'); var mysql = require('mysql'); var query; var connection = mysql.createConnection({ host: '192.168.1.18', port: '3310', user: 'root', password: 'admin' }); var zongji = new ZongJi({ host: '192.168.1.18', port: '3310', user: 'root', password: 'admin' }); zongji.on('binlog', function(evt) { if (evt.query != 'BEGIN') { query = evt.query query = query.replace(/`tuadmin`/g, '`demo`'); connection.query(query, function(error, results, fields) { }); console.log(query); } }); zongji.start({ includeEvents: ['query'] }); process.on('SIGINT', function() { console.log('Got SIGINT.'); zongji.stop(); process.exit(); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.