我如何使用 Node.js 或 PHP 实时收听 MySQL 数据库更改
How can i listen MySQL database changes in real time with Node.js or PHP
我需要创建一个包含多个数据库的系统,其中一个是 master 数据库,该数据库只需要将结构更改复制到其他数据库,例如:
当我在系统中注册新用户时,系统会自动创建 master 数据库的结构副本,但该数据库不会将插入的寄存器或更新发送到 master 数据库,只有 master 数据库在更新时发送所有结构对从属数据库的更改,因此我需要创建一个脚本或实现工具来捕获数据库更新以实时对所有从属数据库执行更新。
我向 AWS 支持部门发送了一个问题,他们建议我实施一个 phyton 脚本或集成另一个允许执行 binlog 流式传输以将这些更改复制到从属数据库的库。
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. You will need to enable binlog on your DB cluster and listen to the log. 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. Then use the binlog script to push changes to your child databases depending on your logic you have written.
[1] https://aws.amazon.com/blogs/database/streaming-changes-in-a-database-with-amazon-kinesis/
我解决了我的问题集成宗机一个npm包,宗机检测binlog上的变化并捕获执行的查询,我用这个包做了一个脚本来监听binlog事件并将这些更改应用到从数据库,这里我将附上我的脚本示例。
综合资料库: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();
});
我需要创建一个包含多个数据库的系统,其中一个是 master 数据库,该数据库只需要将结构更改复制到其他数据库,例如:
当我在系统中注册新用户时,系统会自动创建 master 数据库的结构副本,但该数据库不会将插入的寄存器或更新发送到 master 数据库,只有 master 数据库在更新时发送所有结构对从属数据库的更改,因此我需要创建一个脚本或实现工具来捕获数据库更新以实时对所有从属数据库执行更新。
我向 AWS 支持部门发送了一个问题,他们建议我实施一个 phyton 脚本或集成另一个允许执行 binlog 流式传输以将这些更改复制到从属数据库的库。
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. You will need to enable binlog on your DB cluster and listen to the log. 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. Then use the binlog script to push changes to your child databases depending on your logic you have written.
[1] https://aws.amazon.com/blogs/database/streaming-changes-in-a-database-with-amazon-kinesis/
我解决了我的问题集成宗机一个npm包,宗机检测binlog上的变化并捕获执行的查询,我用这个包做了一个脚本来监听binlog事件并将这些更改应用到从数据库,这里我将附上我的脚本示例。
综合资料库: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();
});