MySQL 协议支持

MynaDB 实现了 MySQL 线协议,允许您使用任何兼容 MySQL 的客户端进行连接。这使得与 mysql-cli、DBeaver、Grafana 等现有工具的无缝集成成为可能。

配置

在 config.toml 中启用 MySQL 协议服务器:

toml
[mysql]
# Enable MySQL protocol server
enabled = true

# Port for MySQL connections (default: 3307)
port = 3307

# Bind address
bind = "0.0.0.0"

# Maximum concurrent connections
max_connections = 100

使用 MySQL CLI 连接

bash
# Connect to MynaDB
mysql -h localhost -P 3307 -u root

# Connect to a specific database
mysql -h localhost -P 3307 -u root mydb

# Execute a query directly
mysql -h localhost -P 3307 -u root -e "SELECT * FROM mydb.default.cpu LIMIT 10"

支持的命令

数据库命令

sql
-- List all databases (catalogs)
SHOW DATABASES;

-- Switch to a database
USE mydb;

-- Show current database
SELECT DATABASE();

表命令

sql
-- List all tables in current database
SHOW TABLES;

-- List tables in a specific schema
SHOW TABLES FROM default;

-- Show table schema
DESCRIBE cpu;
DESC cpu;

-- Show create table (schema info)
SHOW CREATE TABLE cpu;

查询命令

sql
-- Standard SQL queries work as expected
SELECT host, AVG(usage) as avg_cpu
FROM cpu
WHERE time > NOW() - INTERVAL 1 HOUR
GROUP BY host
ORDER BY avg_cpu DESC;

-- Fully qualified table names
SELECT * FROM mydb.default.cpu LIMIT 10;

-- Time-based queries
SELECT
  time_bucket('5 minutes', time) as bucket,
  host,
  AVG(usage) as avg_usage
FROM cpu
GROUP BY bucket, host
ORDER BY bucket;

使用图形化工具连接

DBeaver

  1. 创建新连接:数据库 → 新建数据库连接
  2. 选择 "MySQL" 作为数据库类型
  3. 配置连接:
    • 主机:localhost
    • 端口:3307
    • 数据库:mydb(或留空)
    • 用户名:root
    • 密码:(留空或使用已配置的密码)
  4. 点击 "测试连接" 然后 "完成"

Grafana

  1. 添加新数据源
  2. 选择 "MySQL" 作为类型
  3. 配置:
    • 主机:localhost:3307
    • 数据库:mydb
    • 用户:root
  4. 保存并测试
text
Example Grafana Query:

SELECT
  $__timeGroup(time, $__interval) as ts,
  host,
  AVG(usage) as usage
FROM cpu
WHERE $__timeFilter(time)
GROUP BY ts, host
ORDER BY ts

DataGrip / IntelliJ

  1. 文件 → 新建 → 数据源 → MySQL
  2. 配置:
    • 主机:localhost
    • 端口:3307
    • 用户:root
  3. 测试连接并应用

Python 连接

python
import mysql.connector

# Connect to MynaDB
conn = mysql.connector.connect(
    host="localhost",
    port=3307,
    user="root",
    database="mydb"
)

cursor = conn.cursor()

# Execute query
cursor.execute("""
    SELECT host, AVG(usage) as avg_cpu
    FROM cpu
    WHERE time > NOW() - INTERVAL 1 HOUR
    GROUP BY host
""")

# Fetch results
for row in cursor.fetchall():
    print(f"Host: {row[0]}, Avg CPU: {row[1]:.2f}%")

cursor.close()
conn.close()

Node.js 连接

javascript
const mysql = require('mysql2/promise');

async function query() {
  const conn = await mysql.createConnection({
    host: 'localhost',
    port: 3307,
    user: 'root',
    database: 'mydb'
  });

  const [rows] = await conn.execute(`
    SELECT host, AVG(usage) as avg_cpu
    FROM cpu
    WHERE time > NOW() - INTERVAL 1 HOUR
    GROUP BY host
  `);

  console.log(rows);
  await conn.end();
}

query();

支持的 SQL 语法

MynaDB 通过 Apache DataFusion 支持标准 SQL。主要功能包括:

限制

MySQL 协议实现存在以下限制:

认证

MynaDB 支持 MySQL 连接的基本认证:

toml
[mysql]
enabled = true
port = 3307

# Authentication (optional)
[mysql.auth]
enabled = true
username = "mynadb"
password = "secret123"
bash
# Connect with authentication
mysql -h localhost -P 3307 -u mynadb -psecret123