A python implementation of the mysql server protocol
Maintained. Last release 8 weeks ago, commits in 1 of the last 26 weeks. Weaker on commit activity.
pip install mysql-mimicMySQL-Mimic
Pure-python implementation of the MySQL server wire protocol.
This can be used to create applications that act as a MySQL server.
Installation
pip install mysql-mimic
Usage
A minimal use case might look like this:
import asyncio
from mysql_mimic import MysqlServer, Session
class MySession(Session):
async def query(self, expression, sql, attrs):
print(f"Parsed abstract syntax tree: {expression}")
print(f"Original SQL string: {sql}")
print(f"Query attributes: {sql}")
print(f"Currently authenticated user: {self.username}")
print(f"Currently selected database: {self.database}")
return [("a", 1), ("b", 2)], ["col1", "col2"]
async def schema(self):
# Optionally provide the database schema.
# This is used to serve INFORMATION_SCHEMA and SHOW queries.
return {
"table": {
"col1": "TEXT",
"col2": "INT",
}
}
if __name__ == "__main__":
server = MysqlServer(session_factory=MySession)
asyncio.run(server.serve_forever())
Using sqlglot, the abstract Session class handles queries to metadata, variables, etc. that many MySQL clients expect.
To bypass this default behavior, you can implement the mysql_mimic.session.BaseSession interface.
See examples for more examples.
Authentication
MySQL-mimic has built in support for several standard MySQL authentication plugins:
- mysql_native_password
- The client sends hashed passwords to the server, and the server stores hashed passwords. See the documentation for more details on how this works.
- example