Reputation: 2199
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 support answer:
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/
Upvotes: 1
Views: 6629
Reputation: 2199
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 repository: 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();
});
Upvotes: 5