Skip to content

Instantly share code, notes, and snippets.

@jonathanhle
Created January 31, 2019 00:55
Show Gist options
  • Save jonathanhle/8217c6ca47fc9fcfbc588b3ba47d0ea6 to your computer and use it in GitHub Desktop.
Save jonathanhle/8217c6ca47fc9fcfbc588b3ba47d0ea6 to your computer and use it in GitHub Desktop.
kill old mysql sessions with sqlalchemy
from sqlalchemy import create_engine
engine = create_engine('mysql+mysqldb://USERNAME:[email protected]/DBNAME', pool_recycle=3600) # connect to server
old_sessions = engine.execute("select id from information_schema.processlist where user not in ('user1','user2','user3')").fetchall()
for session in old_sessions:
print("Killing session id " + str(session[0]))
engine.execute("CALL mysql.rds_kill({session_id});".format(session_id=session[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment