{"library":"mysql-mimic","title":"MySQL Mimic","description":"mysql-mimic is a pure Python implementation of the MySQL server protocol. It enables developers to create fake MySQL servers to intercept connections, log queries, serve custom data, or test database clients without requiring a real MySQL instance. The current version, 3.0.2, primarily features an asynchronous API and aims for robust protocol emulation. Releases generally follow major breaking changes or significant feature additions, with major versions appearing every 2-3 years.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install mysql-mimic"],"cli":null},"imports":["from mysql_mimic import MySQLServer","from mysql_mimic.auth import Authenticator","from mysql_mimic.ssl import SSLContextProvider"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import asyncio\nimport logging\nimport os\nfrom mysql_mimic import MySQLServer\nfrom mysql_mimic.auth import Authenticator\n\nlogging.basicConfig(level=logging.INFO)\n\n# Define a test password via an environment variable for security best practices\n# In a real scenario, this would be a secure, complex password.\nMIMIC_PASSWORD = os.environ.get('MIMIC_TEST_PASSWORD', 'super-secret-password')\nMIMIC_HOST = os.environ.get('MIMIC_HOST', '127.0.0.1')\nMIMIC_PORT = int(os.environ.get('MIMIC_PORT', 3307)) # Use a non-standard port\n\nclass MyAuthenticator(Authenticator):\n    async def authenticate(self, username, password, database):\n        logging.info(f\"Auth attempt: user='{username}', db='{database}'\")\n        if password == MIMIC_PASSWORD:\n            logging.info(f\"Authentication successful for '{username}'\")\n            return True\n        logging.warning(f\"Authentication failed for '{username}' with provided password.\")\n        return False\n\nclass MyServer(MySQLServer):\n    async def on_query(self, query: str, database: str):\n        logging.info(f\"Query received on '{database}': '{query}'\")\n        # Return a list of tuples, mimicking a result set\n        if query.lower().startswith(\"select version()\"):\n            return [(\"3.0.2\",)] # Mimic current version of mysql-mimic\n        elif query.lower().startswith(\"select 'hello world'\"):\n            return [(\"hello world from mysql-mimic!\",)]\n        else:\n            return [(f\"You queried: '{query}'\",)]\n\n# This is the simplest way to run for a quickstart in an async context\n# Make sure to set MIMIC_TEST_PASSWORD in your environment before running, e.g.:\n# export MIMIC_TEST_PASSWORD=\"super-secret-password\"\n# python your_script.py\nserver = MyServer(authenticator=MyAuthenticator(), host=MIMIC_HOST, port=MIMIC_PORT)\nlogging.info(f\"Starting MySQL Mimic server on {MIMIC_HOST}:{MIMIC_PORT}\")\nserver.serve_forever_async()\n\n# To test from your terminal:\n# mysql -h 127.0.0.1 -P 3307 -u anyuser -p'super-secret-password' -e \"SELECT 'Hello World';\"","lang":"python","description":"This quickstart sets up a basic MySQL Mimic server that listens on port 3307 (or `MIMIC_PORT` env var). It uses a custom authenticator to allow any username with a pre-defined password (`MIMIC_TEST_PASSWORD` env var or 'super-secret-password'). The `on_query` method demonstrates how to respond to client queries with a list of tuples, mimicking a database result set. Remember to set the environment variable for the password before running.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}