#!/usr/bin/env python3 import os import mysql.connector from mysql.connector import Error config = { "host": os.environ.get("MYSQL_HOST", "127.0.0.1"), "port": int(os.environ.get("MYSQL_PORT", "3306")), "user": os.environ["MYSQL_USER"], "password": os.environ["MYSQL_PASSWORD"], "database": os.environ["MYSQL_DATABASE"], } connection = None cursor = None try: connection = mysql.connector.connect(**config) cursor = connection.cursor() cursor.execute("SELECT DATABASE(), CURRENT_USER()") database_name, current_account = cursor.fetchone() print(f"Connected to database: {database_name}") print(f"Current MySQL account: {current_account}") cursor.execute( """ SELECT table_name FROM information_schema.tables WHERE table_schema = %s ORDER BY table_name LIMIT 5 """, (config["database"],), ) print("Tables:") for (table_name,) in cursor.fetchall(): print(f" - {table_name}") except Error as exc: raise SystemExit(f"MySQL error: {exc}") from exc finally: if cursor is not None: cursor.close() if connection is not None and connection.is_connected(): connection.close()