Skip to content

Instantly share code, notes, and snippets.

@utdrmac
Last active November 23, 2023 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save utdrmac/f1e9a05e37c934e8da68d0de92c66602 to your computer and use it in GitHub Desktop.
Save utdrmac/f1e9a05e37c934e8da68d0de92c66602 to your computer and use it in GitHub Desktop.
Quick-n-dirty script for watching RDS replication and skipping on specific error codes
import sys
import mysql.connector
from mysql.connector.errors import InterfaceError
from time import sleep
mydb = mysql.connector.connect(host="", user="", password="", database="mysql")
cursor = mydb.cursor(dictionary=True)
while True:
sleepTime = 0.5
try:
print("Checking replication...")
cursor.execute("SHOW REPLICA STATUS")
ss = cursor.fetchone()
lastErNo = ss["Last_SQL_Errno"]
if lastErNo in [1032, 1062, 1396, 1451]:
print("Duplicate key/Not found/Missing user... Skipping")
cursor.callproc("rds_skip_repl_error")
elif lastErNo == 0:
sleepTime = 5
print(f"SBM: {ss['Seconds_Behind_Master']}")
else:
print(f"Last error: {ss['Last_SQL_Errno']}")
sleep(sleepTime)
except InterfaceError as e:
print(e)
except KeyboardInterrupt:
print("Shuting down...")
cursor.close()
mydb.close()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment