Updates
This commit is contained in:
@@ -1 +0,0 @@
|
||||
export {};
|
||||
@@ -1,102 +0,0 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const mysql_1 = __importDefault(require("mysql"));
|
||||
const child_process_1 = require("child_process");
|
||||
const util_1 = require("util");
|
||||
function getConnection(config) {
|
||||
return mysql_1.default.createConnection(config);
|
||||
}
|
||||
function getMasterStatus(config) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const connection = getConnection(config);
|
||||
connection.query("SHOW MASTER STATUS", (error, results) => {
|
||||
connection.end();
|
||||
if (error)
|
||||
reject(error);
|
||||
else
|
||||
resolve(results[0]);
|
||||
});
|
||||
});
|
||||
}
|
||||
function syncDatabases() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const config = {
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "your_password",
|
||||
};
|
||||
let lastPosition = null; // Track last synced position
|
||||
while (true) {
|
||||
try {
|
||||
// Get current master status
|
||||
const { File, Position } = yield getMasterStatus(config);
|
||||
// Determine start position (use lastPosition or 4 if first run)
|
||||
const startPosition = lastPosition !== null ? lastPosition + 1 : 4;
|
||||
if (startPosition >= Position) {
|
||||
yield new Promise((resolve) => setTimeout(resolve, 5000)); // Wait 5 seconds if no new changes
|
||||
continue;
|
||||
}
|
||||
// Execute mysqlbinlog to get changes
|
||||
const execPromise = (0, util_1.promisify)(child_process_1.exec);
|
||||
const { stdout } = yield execPromise(`mysqlbinlog --database=db_master ${File} --start-position=${startPosition} --stop-position=${Position}`);
|
||||
if (stdout) {
|
||||
const connection = getConnection(Object.assign(Object.assign({}, config), { database: "db_slave" }));
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(stdout, (error) => {
|
||||
connection.end();
|
||||
if (error)
|
||||
reject(error);
|
||||
else {
|
||||
lastPosition = Position;
|
||||
console.log(`Synced up to position ${Position} at ${new Date().toISOString()}`);
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Sync error:", error);
|
||||
}
|
||||
yield new Promise((resolve) => setTimeout(resolve, 5000)); // Check every 5 seconds
|
||||
}
|
||||
});
|
||||
}
|
||||
// Initialize db_slave with db_master data
|
||||
function initializeSlave() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const config = {
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "your_password",
|
||||
};
|
||||
try {
|
||||
yield (0, util_1.promisify)(child_process_1.exec)(`mysqldump -u ${config.user} -p${config.password} db_master > db_master_backup.sql`);
|
||||
yield (0, util_1.promisify)(child_process_1.exec)(`mysql -u ${config.user} -p${config.password} db_slave < db_master_backup.sql`);
|
||||
console.log("Slave initialized with master data");
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Initialization error:", error);
|
||||
}
|
||||
});
|
||||
}
|
||||
// Run the sync process
|
||||
function main() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield initializeSlave();
|
||||
yield syncDatabases();
|
||||
});
|
||||
}
|
||||
main().catch(console.error);
|
||||
Reference in New Issue
Block a user