Last active
May 5, 2019 23:51
-
-
Save rubenmromero/cff8ebcbbc924c378e4f2361d62f5b4e to your computer and use it in GitHub Desktop.
Python :: Script to rotate the content of 'mysql.general_log' table to 'mysql.general_log_backup', by executing 'mysql.rds_rotate_general_log' stored procedure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Modules Import | |
# | |
import mysql.connector | |
from mysql.connector import errorcode | |
# | |
# Variables Definition | |
# | |
db_config = { | |
'user': '<user>', | |
'password': '<password>', | |
'host': '<rds_endpoint>', | |
'database': 'mysql', | |
'raise_on_warnings': True, | |
'time_zone': 'UTC', | |
} | |
procedure = 'rds_rotate_general_log' | |
# | |
# Main | |
# | |
# Database connection with error handling | |
try: | |
cnx = mysql.connector.connect(**db_config) | |
except mysql.connector.Error as err: | |
if (err.errno == errorcode.ER_ACCESS_DENIED_ERROR): | |
print 'Something is wrong with your user name or password' | |
elif (err.errno == errorcode.ER_BAD_DB_ERROR): | |
print 'Database does not exist' | |
else: | |
print err | |
else: | |
# Create cursor for query execution | |
cursor = cnx.cursor() | |
# Execute stored procedure that rotates general_log table | |
cursor.callproc('rds_rotate_general_log') | |
# Print execution result | |
# for result in cursor.stored_results(): | |
# print result.fecthall() | |
# Close database connection | |
cnx.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment