Skip to main content

MySQL 服务器 API

¥MySQL Server API

服务器

¥Server

  • createServer() - 创建服务器实例

    ¥createServer() - creates server instance

  • Server.listen - 监听端口/unix 套接字(与 net.Server.listen 相同的参数)

    ¥Server.listen - listen port / unix socket (same arguments as net.Server.listen)

事件

¥Events

  • connect

    • 新的传入连接。

      ¥new incoming connection.


连接

¥Connection

  • serverHandshake({ serverVersion, protocolVersion, connectionId, statusFlags, characterSet, capabilityFlags })

    • 发送服务器握手初始化数据包,等待握手响应并开始监听命令

      ¥send server handshake initialisation packet, wait handshake response and start listening for commands

  • writeOk({ affectedRows: num, insertId: num })

  • writeEof(warnings, statusFlags)

    • 发送 EOF 数据包

      ¥send EOF packet

  • writeTextResult(rows, fields)

    • 将查询结果写入客户端。行和字段的格式与 connection.query 回调中的格式相同。

      ¥write query result to client. Rows and fields are in the same format as in connection.query callback.

  • writeColumns(fields)

    • 写入字段 + EOF 数据包。

      ¥write fields + EOF packets.

  • writeTextRow(row)

    • 将值的数组(不是哈希!)写入结果行

      ¥write array (not hash!) of values as result row

  • TODO:二进制协议

    ¥TODO: binary protocol

事件

¥Events

服务器收到的每个命令包都将作为带有以下参数的数据包事件触发:

¥Every command packet received by the server will be emitted as a packet event with the parameters:

  • 数据包:数据包

    ¥packet: Packet

    • 数据包本身

      ¥The packet itself

  • 已知命令:boolean

    ¥knownCommand: boolean

    • 服务器是否知道此命令

      ¥is this command known to the server

  • *commandCode:number

    • 解析的命令代码(第一个字节)

      ¥the parsed command code (first byte)

此外,还会为从客户端接收到的 commands 触发特殊事件。如果没有监听器,将调用回退行为。

¥In addition special events are emitted for commands received from the client. If no listener is present a fallback behavior will be invoked.

  • quit()

    • 默认:关闭连接

      ¥Default: close the connection

  • init_db(schemaName: string)

    • 默认:返回 OK

      ¥Default: return OK

  • query(sql: string)

    • 请将监听器附加到此处。默认:返回 HA_ERR_INTERNAL_ERROR

      ¥Please attach a listener to this. Default: return HA_ERR_INTERNAL_ERROR

  • field_list(table: string, fields: string)

    • 默认:返回 ER_WARN_DEPRECATED_SYNTAX

      ¥Default: return ER_WARN_DEPRECATED_SYNTAX

  • ping() - 默认:返回 OK

    ¥ping() - Default: return OK