Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenSrv - MySQL (ePHPm fork)

This is an ePHPm-org fork of datafuselabs/opensrv's opensrv-mysql crate, forked from the published 0.7.0 tag. The sole functional change is a dependency migration from tokio-rustls 0.25 (rustls 0.22) to tokio-rustls 0.26 (rustls >= 0.23.45), pinned to the aws-lc-rs crypto provider. See FORK.md for the rationale (RUSTSEC-2026-0285) and the exact diff. All credit for the crate belongs to the original Datafuse Labs authors; see NOTICE.

Bindings for emulating a MySQL/MariaDB server.

When developing new databases or caching layers, it can be immensely useful to test your system using existing applications. However, this often requires significant work modifying applications to use your database over the existing ones. This crate solves that problem by acting as a MySQL server, and delegating operations such as querying and query execution to user-defined logic.

Usage

To start, implement AsyncMysqlShim for your backend, and create a AsyncMysqlIntermediary over an instance of your backend and a connection stream. The appropriate methods will be called on your backend whenever a client issues a QUERY, PREPARE, or EXECUTE command, and you will have a chance to respond appropriately. For example, to write a shim that always responds to all commands with a "no results" reply:

use std::io;
use tokio::io::AsyncWrite;

use opensrv_mysql::*;
use tokio::net::TcpListener;

struct Backend;

#[async_trait::async_trait]
impl<W: AsyncWrite + Send + Unpin> AsyncMysqlShim<W> for Backend {
    type Error = io::Error;

    async fn on_prepare<'a>(
        &'a mut self,
        _: &'a str,
        info: StatementMetaWriter<'a, W>,
    ) -> io::Result<()> {
        info.reply(42, &[], &[]).await
    }

    async fn on_execute<'a>(
        &'a mut self,
        _: u32,
        _: opensrv_mysql::ParamParser<'a>,
        results: QueryResultWriter<'a, W>,
    ) -> io::Result<()> {
        results.completed(OkResponse::default()).await
    }

    async fn on_close(&mut self, _: u32) {}

    async fn on_query<'a>(
        &'a mut self,
        sql: &'a str,
        results: QueryResultWriter<'a, W>,
    ) -> io::Result<()> {
        println!("execute sql {:?}", sql);
        results.start(&[]).await?.finish().await
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let listener = TcpListener::bind("0.0.0.0:3306").await?;

    loop {
        let (stream, _) = listener.accept().await?;
        let (r, w) = stream.into_split();
        tokio::spawn(async move { AsyncMysqlIntermediary::run_on(Backend, r, w).await });
    }
}

This example can be exected with:

cargo run --example=serve_one

More examples can be found here.

Getting help

Submit issues for bug report or asking questions in discussion.

Credits

This project is a branch of jonhoo/msql-srv and focuses on providing asynchronous support.

License

Licensed under Apache License, Version 2.0.

About

ePHPm fork of datafuselabs/opensrv opensrv-mysql 0.7.0: tokio-rustls 0.26 / rustls >=0.23.45 on aws-lc-rs (RUSTSEC-2026-0285). Stopgap; see FORK.md.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages