A python implementation of the mysql server protocol
Project description
MySQL-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
- mysql_clear_password
- The client sends passwords to the server as clear text, without hashing or encryption.
- This is typically used as the client plugin for a custom server plugin. As such, MySQL-mimic provides an abstract class,
mysql_mimic.auth.AbstractClearPasswordAuthPlugin
, which can be extended. - example
- mysql_no_login
- The server prevents clients from directly authenticating as an account. See the documentation for relevant use cases.
- authentication_kerberos
- Kerberos uses tickets together with symmetric-key cryptography, enabling authentication without sending passwords over the network. Kerberos authentication supports userless and passwordless scenarios.
By default, a session naively accepts whatever username the client provides.
Plugins are provided to the server by implementing mysql_mimic.IdentityProvider
, which configures all available plugins and a callback for fetching users.
Custom plugins can be created by extending mysql_mimic.auth.AuthPlugin
.
Development
You can install dependencies with make deps
.
You can format your code with make format
.
You can lint with make lint
.
You can check type annotations with make types
.
You can run tests with make test
. This will build a coverage report in ./htmlcov/index.html
.
You can run all the checks with make check
.
You can build a pip package with make build
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mysql-mimic-2.5.7.tar.gz
.
File metadata
- Download URL: mysql-mimic-2.5.7.tar.gz
- Upload date:
- Size: 52.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11c1cc387dce6c6ee72759ed048bf249bf3d51af9b20fbfa1e79208807583e0f |
|
MD5 | 054d9899fc6605ac4a45a2565d65861b |
|
BLAKE2b-256 | da1df216617d6f042698a51e9377ca94ffdb9c7485c12cbee317257f0124c343 |
File details
Details for the file mysql_mimic-2.5.7-py3-none-any.whl
.
File metadata
- Download URL: mysql_mimic-2.5.7-py3-none-any.whl
- Upload date:
- Size: 46.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4b88cd48109428a2cc221f935725a5ff25fc23651c7ebb670ba4c14b35590a1 |
|
MD5 | 3a626e2a2bc174f1a5a34a14d200a36d |
|
BLAKE2b-256 | 2040dc111963785614d1fcdcbe8ed1bd25345a0ea12100aeaa731504756ffd56 |