Skip to content

Conversation

@JanJakes
Copy link
Member

@JanJakes JanJakes commented Oct 20, 2025

Implements a basic MySQL proxy that bridges the MySQL wire protocol to the SQLite driver, based on adamziel/mysql-sqlite-network-proxy#1.

This enables the usage of phpMyAdmin, Adminer, and other tools on top of SQLite.

The PR includes tests for PDO and mysqli connections and query commands.

See also the MySQL proxy package README.

CLI usage:

$ php bin/wp-mysql-proxy.php [--port <port>] [--database <path/to/db.sqlite>] [--log-level <log_level>]

Options:
  -h, --help              Show this help message and exit.
  -p, --port=<port>       The port to listen on. Default: 3306
  -d, --database=<path>   The path to the SQLite database file. Default: :memory:
  -l, --log-level=<level> The log level to use. One of 'error', 'warning', 'info', 'debug'. Default: info

PHP usage:

use WP_MySQL_Proxy\MySQL_Proxy;
use WP_MySQL_Proxy\Adapter\SQLite_Adapter;

require_once __DIR__ . '/vendor/autoload.php';

$proxy = new MySQL_Proxy(
	new SQLite_Adapter( $db_path ),
	array( 'port' => $port, 'log_level' => $log_level )
);
$proxy->start();

@JanJakes JanJakes changed the title WIP MySQL Server proxy WIP MySQL Server for SQLite Oct 20, 2025
@akirk
Copy link
Member

akirk commented Oct 21, 2025

This is great! Just a little note, there is a typo in the directory packages/mysq-server-php

@JanJakes
Copy link
Member Author

This is great! Just a little note, there is a typo in the directory packages/mysq-server-php

@akirk Ah, thanks for catching that!

I'm also thinking about whether to keep the -php suffix or drop it, as it's likely that all/most packages in the repo will be PHP anyway, and it doesn't make much sense to include that suffix everywhere. Or maybe it's more like a "proxy", I don't know. When we make the driver API PDO-like, this package will be a bit like "MySQL server on PDO client".

I guess in this repo, the naming will be generally hard 🙂

@JanJakes JanJakes changed the base branch from develop to column-meta-statements October 21, 2025 16:43
@JanJakes JanJakes force-pushed the column-meta-statements branch 2 times, most recently from 32462e5 to 91bc3e8 Compare October 23, 2025 07:32
Base automatically changed from column-meta-statements to develop October 23, 2025 07:33
@JanJakes JanJakes force-pushed the mysql-server branch 12 times, most recently from 008f29a to 2049cf0 Compare November 3, 2025 15:58
@JanJakes JanJakes changed the title WIP MySQL Server for SQLite MySQL Proxy for SQLite Nov 3, 2025
@JanJakes JanJakes marked this pull request as ready for review November 3, 2025 16:06
@JanJakes JanJakes requested a review from adamziel November 3, 2025 16:06
@JanJakes
Copy link
Member Author

JanJakes commented Nov 3, 2025

@adamziel I think this is ready for an initial review. The code needs more polishing, and we'll need some more functionality (prepared statements for mysqli, setting username and password, etc.), but I guess it's in a usable initial state.

public function start() {
$this->socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
socket_set_option( $this->socket, SOL_SOCKET, SO_REUSEADDR, 1 );
socket_bind( $this->socket, '0.0.0.0', $this->port );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: socket_bind(): Unable to bind address [48]: Address already in use in /Users/cloudnik/www/Automattic/core/plugins/sqlite-database-integration/packages/wp-mysql-proxy/src/class-mysql-proxy.php on line 22
MySQL PHP Proxy listening on port 3306...

Let's exit(1) on any and all errors

@adamziel
Copy link
Collaborator

adamziel commented Nov 3, 2025

Not a blocker for this PR, but something about the CLI client derails it:

> mysql -h 127.0.0.1 -P 3307                                                                                                                                                                                            ERROR 2027 (HY000): Malformed packet

If I send a password, it's fine:

> mysql -h 127.0.0.1 -P 3307 -p -u root                                                                                                                                                                             (base)
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.

@adamziel
Copy link
Collaborator

adamziel commented Nov 3, 2025

Hey, look at me using MySQL 🔥

> mysql -h 127.0.0.1 -P 3307 -p -u root                                                                                                                                                                             (base)
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 945
Server version: 5.7.30-php-mysql-server MySQL Community Server - GPL

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create table books (id int auto_increment primary key, title varchar(255));
Query OK, 0 rows affected (0,00 sec)

mysql> insert into books (title) values ('How to train your dragon');
Query OK, 1 row affected (0,01 sec)

mysql> select * from books;
+----+--------------------------+
| id | title                    |
+----+--------------------------+
|  1 | How to train your dragon |
+----+--------------------------+
1 row in set (0,00 sec)

@JanJakes
Copy link
Member Author

@adamziel I fully refactored the MySQL_Proxy class — code simplification, error handling, logging, etc. I also listed and documented all constants from the MySQL_Protocol as per the https://github.com/mysql/mysql-server/ repo.

What remains is:

  • Refactor and document the MySQL_Protocol class probably a bit more.
  • Refactor and document the MySQL_Session class.
  • Handle more authentication flows and add tests for them (make username/password configurable, allow no password, etc.).

It may be better to get this in in some form and continue the above points in separate pull requests. At least the authentication revamp deserves its own PR, I think.

@akirk TablePlus now seems to work! (You still need to provide some username and password.)

@akirk
Copy link
Member

akirk commented Nov 13, 2025

I can confirm that I can now connect with a username and password in TablePlus! Very nice! It is still a bit confusing that if you don't enter username and password you receive a "malformed packet" error. Not pressing but establishing to display a more appropriate error would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants